import.c revision 6c849697fd0085ea4415b7ef5bdef30e734092b2
1/***********************************************************
2Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
3Amsterdam, The Netherlands.
4
5                        All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* Module definition and import implementation */
26
27#include "allobjects.h"
28
29#include "node.h"
30#include "token.h"
31#include "graminit.h"
32#include "import.h"
33#include "errcode.h"
34#include "sysmodule.h"
35#include "pythonrun.h"
36#include "marshal.h"
37#include "compile.h"
38#include "eval.h"
39#include "osdefs.h"
40
41extern int verbose; /* Defined in pythonrun.c */
42
43extern long getmtime(); /* In getmtime.c */
44
45#ifdef DEBUG
46#define D(x) x
47#else
48#define D(x)
49#endif
50
51/* Explanation of some of the the various #defines used by dynamic linking...
52
53   symbol	-- defined for:
54
55   DYNAMIC_LINK -- any kind of dynamic linking
56   USE_RLD	-- NeXT dynamic linking
57   USE_DL	-- Jack's dl for IRIX 4 or GNU dld with emulation for Jack's dl
58   USE_SHLIB	-- SunOS or IRIX 5 (SVR4?) shared libraries
59   _AIX		-- AIX style dynamic linking
60   NT		-- NT style dynamic linking (using DLLs)
61   _DL_FUNCPTR_DEFINED	-- if the typedef dl_funcptr has been defined
62   WITH_MAC_DL	-- Mac dynamic linking (highly experimental)
63   SHORT_EXT	-- short extension for dynamic module, e.g. ".so"
64   LONG_EXT	-- long extension, e.g. "module.so"
65   hpux		-- HP-UX Dynamic Linking - defined by the compiler
66
67   (The other WITH_* symbols are used only once, to set the
68   appropriate symbols.)
69*/
70
71/* Configure dynamic linking */
72
73#ifdef hpux
74#define DYNAMIC_LINK
75#include <errno.h>
76typedef void (*dl_funcptr)();
77#define _DL_FUNCPTR_DEFINED 1
78#define SHORT_EXT ".sl"
79#define LONG_EXT "module.sl"
80#endif
81
82#ifdef NT
83#define DYNAMIC_LINK
84#include <windows.h>
85typedef FARPROC dl_funcptr;
86#define _DL_FUNCPTR_DEFINED
87#define SHORT_EXT ".dll"
88#define LONG_EXT "module.dll"
89#endif
90
91#if defined(NeXT) || defined(WITH_RLD)
92#define DYNAMIC_LINK
93#define USE_RLD
94#endif
95
96#ifdef WITH_SGI_DL
97#define DYNAMIC_LINK
98#define USE_DL
99#endif
100
101#ifdef WITH_DL_DLD
102#define DYNAMIC_LINK
103#define USE_DL
104#endif
105
106#ifdef WITH_MAC_DL
107#define DYNAMIC_LINK
108#endif
109
110#if !defined(DYNAMIC_LINK) && defined(HAVE_DLFCN_H) && defined(HAVE_DLOPEN)
111#define DYNAMIC_LINK
112#define USE_SHLIB
113#endif
114
115#ifdef _AIX
116#define DYNAMIC_LINK
117#include <sys/ldr.h>
118typedef void (*dl_funcptr)();
119#define _DL_FUNCPTR_DEFINED
120static void aix_loaderror(char *name);
121#endif
122
123#ifdef DYNAMIC_LINK
124
125#ifdef USE_SHLIB
126#include <dlfcn.h>
127#ifndef _DL_FUNCPTR_DEFINED
128typedef void (*dl_funcptr)();
129#endif
130#ifndef RTLD_LAZY
131#define RTLD_LAZY 1
132#endif
133#define SHORT_EXT ".so"
134#define LONG_EXT "module.so"
135#endif /* USE_SHLIB */
136
137#if defined(USE_DL) || defined(hpux)
138#include "dl.h"
139#endif
140
141#ifdef WITH_MAC_DL
142#include "dynamic_load.h"
143#endif
144
145#ifdef USE_RLD
146#include <mach-o/rld.h>
147#define FUNCNAME_PATTERN "_init%s"
148#ifndef _DL_FUNCPTR_DEFINED
149typedef void (*dl_funcptr)();
150#endif
151#endif /* USE_RLD */
152
153extern char *getprogramname();
154
155#ifndef FUNCNAME_PATTERN
156#if defined(__hp9000s300)
157#define FUNCNAME_PATTERN "_init%s"
158#else
159#define FUNCNAME_PATTERN "init%s"
160#endif
161#endif
162
163#if !defined(SHORT_EXT) && !defined(LONG_EXT)
164#define SHORT_EXT ".o"
165#define LONG_EXT "module.o"
166#endif /* !SHORT_EXT && !LONG_EXT */
167
168#endif /* DYNAMIC_LINK */
169
170/* Max length of module suffix searched for -- accommodates "module.so" */
171#ifndef MAXSUFFIXSIZE
172#define MAXSUFFIXSIZE 10
173#endif
174
175/* Magic word to reject .pyc files generated by other Python versions */
176#define MAGIC 0x999903L /* Increment by one for each incompatible change */
177
178static object *modules;
179
180/* Forward */
181static int init_builtin PROTO((char *));
182
183/* Helper for reading .pyc files */
184
185long
186get_pyc_magic()
187{
188	return MAGIC;
189}
190
191/* Initialization */
192
193void
194initimport()
195{
196	if ((modules = newdictobject()) == NULL)
197		fatal("no mem for dictionary of modules");
198}
199
200object *
201get_modules()
202{
203	return modules;
204}
205
206object *
207add_module(name)
208	char *name;
209{
210	object *m;
211	if ((m = dictlookup(modules, name)) != NULL && is_moduleobject(m))
212		return m;
213	m = newmoduleobject(name);
214	if (m == NULL)
215		return NULL;
216	if (dictinsert(modules, name, m) != 0) {
217		DECREF(m);
218		return NULL;
219	}
220	DECREF(m); /* Yes, it still exists, in modules! */
221	return m;
222}
223
224enum filetype {SEARCH_ERROR, PY_SOURCE, PY_COMPILED, C_EXTENSION};
225
226static struct filedescr {
227	char *suffix;
228	char *mode;
229	enum filetype type;
230} filetab[] = {
231#ifdef DYNAMIC_LINK
232#ifdef SHORT_EXT
233	{SHORT_EXT, "rb", C_EXTENSION},
234#endif /* !SHORT_EXT */
235#ifdef LONG_EXT
236	{LONG_EXT, "rb", C_EXTENSION},
237#endif /* !LONG_EXT */
238#endif /* DYNAMIC_LINK */
239	{".py", "r", PY_SOURCE},
240	{".pyc", "rb", PY_COMPILED},
241	{0, 0}
242};
243
244#ifdef DYNAMIC_LINK
245static object *
246load_dynamic_module(name, namebuf, m, m_ret)
247	char *name;
248	char *namebuf;
249	object *m;
250	object **m_ret;
251{
252	char funcname[258];
253	dl_funcptr p = NULL;
254	if (m != NULL) {
255		err_setstr(ImportError,
256			   "cannot reload dynamically loaded module");
257		return NULL;
258	}
259	sprintf(funcname, FUNCNAME_PATTERN, name);
260#ifdef WITH_MAC_DL
261	{
262		object *v = dynamic_load(namebuf);
263		if (v == NULL)
264			return NULL;
265	}
266#else /* !WITH_MAC_DL */
267#ifdef USE_SHLIB
268	{
269#ifdef RTLD_NOW
270		/* RTLD_NOW: resolve externals now
271		   (i.e. core dump now if some are missing) */
272		void *handle = dlopen(namebuf, RTLD_NOW);
273#else
274		void *handle;
275		if (verbose)
276			printf("dlopen(\"%s\", %d);\n", namebuf, RTLD_LAZY);
277		handle = dlopen(namebuf, RTLD_LAZY);
278#endif /* RTLD_NOW */
279		if (handle == NULL) {
280			err_setstr(ImportError, dlerror());
281			return NULL;
282		}
283		p = (dl_funcptr) dlsym(handle, funcname);
284	}
285#endif /* USE_SHLIB */
286#ifdef _AIX
287	p = (dl_funcptr) load(namebuf, 1, 0);
288	if (p == NULL) {
289		aix_loaderror(namebuf);
290		return NULL;
291	}
292#endif /* _AIX */
293#ifdef NT
294	{
295		HINSTANCE hDLL;
296		hDLL = LoadLibrary(namebuf);
297		if (hDLL==NULL){
298			char errBuf[64];
299			sprintf(errBuf, "DLL load failed with error code %d",
300				GetLastError());
301			err_setstr(ImportError, errBuf);
302		return NULL;
303		}
304		p = GetProcAddress(hDLL, funcname);
305	}
306#endif /* NT */
307#ifdef USE_DL
308	p =  dl_loadmod(getprogramname(), namebuf, funcname);
309#endif /* USE_DL */
310#ifdef USE_RLD
311	{
312		NXStream *errorStream;
313		struct mach_header *new_header;
314		const char *filenames[2];
315		long ret;
316		unsigned long ptr;
317
318		errorStream = NXOpenMemory(NULL, 0, NX_WRITEONLY);
319		filenames[0] = namebuf;
320		filenames[1] = NULL;
321		ret = rld_load(errorStream, &new_header,
322				filenames, NULL);
323
324		/* extract the error messages for the exception */
325		if(!ret) {
326			char *streamBuf;
327			int len, maxLen;
328
329			NXPutc(errorStream, (char)0);
330
331			NXGetMemoryBuffer(errorStream,
332				&streamBuf, &len, &maxLen);
333			err_setstr(ImportError, streamBuf);
334		}
335
336		if(ret && rld_lookup(errorStream, funcname, &ptr))
337			p = (dl_funcptr) ptr;
338
339		NXCloseMemory(errorStream, NX_FREEBUFFER);
340
341		if(!ret)
342			return NULL;
343	}
344#endif /* USE_RLD */
345#ifdef hpux
346	{
347		shl_t lib;
348		int flags;
349
350		flags = BIND_DEFERRED;
351		if (verbose)
352                {
353                        flags = BIND_IMMEDIATE | BIND_NONFATAL | BIND_VERBOSE;
354                        printf("shl_load %s\n",namebuf);
355                }
356                lib = shl_load(namebuf, flags, 0);
357                if (lib == NULL)
358                {
359                        char buf[256];
360                        if (verbose)
361                                perror(namebuf);
362                        sprintf(buf, "Failed to load %.200s", namebuf);
363                        err_setstr(ImportError, buf);
364                        return NULL;
365                }
366                if (verbose)
367                        printf("shl_findsym %s\n", funcname);
368                shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p);
369                if (p == NULL && verbose)
370                        perror(funcname);
371	}
372#endif hpux
373	if (p == NULL) {
374		err_setstr(ImportError,
375		   "dynamic module does not define init function");
376		return NULL;
377	}
378	(*p)();
379
380#endif /* !WITH_MAC_DL */
381	*m_ret = m = dictlookup(modules, name);
382	if (m == NULL) {
383		if (err_occurred() == NULL)
384			err_setstr(SystemError,
385				   "dynamic module not initialized properly");
386		return NULL;
387	}
388	if (verbose)
389		fprintf(stderr,
390			"import %s # dynamically loaded from %s\n",
391			name, namebuf);
392	INCREF(None);
393	return None;
394}
395#endif /* DYNAMIC_LINK */
396
397static object *
398get_module(m, name, m_ret)
399	/*module*/object *m;
400	char *name;
401	object **m_ret;
402{
403	int err, npath, i, len, namelen;
404	long magic;
405	long mtime, pyc_mtime;
406	char namebuf[MAXPATHLEN+1];
407	struct filedescr *fdp;
408	FILE *fp = NULL, *fpc = NULL;
409	node *n = NULL;
410	object *path, *v, *d;
411	codeobject *co = NULL;
412
413	path = sysget("path");
414	if (path == NULL || !is_listobject(path)) {
415		err_setstr(ImportError,
416			   "sys.path must be list of directory names");
417		return NULL;
418	}
419	npath = getlistsize(path);
420	namelen = strlen(name);
421	for (i = 0; i < npath; i++) {
422		v = getlistitem(path, i);
423		if (!is_stringobject(v))
424			continue;
425		len = getstringsize(v);
426		if (len + 1 + namelen + MAXSUFFIXSIZE >= MAXPATHLEN)
427			continue; /* Too long */
428		strcpy(namebuf, getstringvalue(v));
429		if (strlen(namebuf) != len)
430			continue; /* v contains '\0' */
431		if (len > 0 && namebuf[len-1] != SEP)
432			namebuf[len++] = SEP;
433		strcpy(namebuf+len, name);
434		len += namelen;
435		for (fdp = filetab; fdp->suffix != NULL; fdp++) {
436			strcpy(namebuf+len, fdp->suffix);
437			if (verbose > 1)
438				fprintf(stderr, "# trying %s\n", namebuf);
439			fp = fopen(namebuf, fdp->mode);
440			if (fp != NULL)
441				break;
442		}
443		if (fp != NULL)
444			break;
445	}
446	if (fp == NULL) {
447		sprintf(namebuf, "No module named %.200s", name);
448		err_setstr(ImportError, namebuf);
449		return NULL;
450	}
451
452	switch (fdp->type) {
453
454	case PY_SOURCE:
455		mtime = getmtime(namebuf);
456		len = strlen(namebuf);
457		strcpy(namebuf + len, "c");
458		fpc = fopen(namebuf, "rb");
459		if (fpc != NULL) {
460			magic = rd_long(fpc);
461			if (magic != MAGIC) {
462				if (verbose)
463					fprintf(stderr,
464						"# %s has bad magic\n",
465						namebuf);
466			}
467			else {
468				pyc_mtime = rd_long(fpc);
469				if (pyc_mtime != mtime) {
470					if (verbose)
471						fprintf(stderr,
472						  "# %s has bad mtime\n",
473						  namebuf);
474				}
475				else {
476					fclose(fp);
477					fp = fpc;
478					if (verbose)
479					   fprintf(stderr,
480					     "# %s matches %s.py\n",
481						   namebuf, name);
482					goto use_compiled;
483				}
484			}
485			fclose(fpc);
486		}
487		namebuf[len] = '\0';
488		n = parse_file(fp, namebuf, file_input);
489		fclose(fp);
490		if (n == NULL)
491			return NULL;
492		co = compile(n, namebuf);
493		freetree(n);
494		if (co == NULL)
495			return NULL;
496		if (verbose)
497			fprintf(stderr,
498				"import %s # from %s\n", name, namebuf);
499		/* Now write the code object to the ".pyc" file */
500		strcpy(namebuf + len, "c");
501		fpc = fopen(namebuf, "wb");
502#ifdef macintosh
503		setfiletype(namebuf, 'PYTH', 'PYC ');
504#endif
505		if (fpc == NULL) {
506			if (verbose)
507				fprintf(stderr,
508					"# can't create %s\n", namebuf);
509		}
510		else {
511			wr_long(MAGIC, fpc);
512			/* First write a 0 for mtime */
513			wr_long(0L, fpc);
514			wr_object((object *)co, fpc);
515			if (ferror(fpc)) {
516				if (verbose)
517					fprintf(stderr,
518						"# can't write %s\n", namebuf);
519				/* Don't keep partial file */
520				fclose(fpc);
521				(void) unlink(namebuf);
522			}
523			else {
524				/* Now write the true mtime */
525				fseek(fpc, 4L, 0);
526				wr_long(mtime, fpc);
527				fflush(fpc);
528				fclose(fpc);
529				if (verbose)
530					fprintf(stderr,
531						"# wrote %s\n", namebuf);
532			}
533		}
534		break;
535
536	case PY_COMPILED:
537		if (verbose)
538			fprintf(stderr, "# %s without %s.py\n",
539				namebuf, name);
540		magic = rd_long(fp);
541		if (magic != MAGIC) {
542			err_setstr(ImportError,
543				   "Bad magic number in .pyc file");
544			return NULL;
545		}
546		(void) rd_long(fp);
547	use_compiled:
548		v = rd_object(fp);
549		fclose(fp);
550		if (v == NULL || !is_codeobject(v)) {
551			XDECREF(v);
552			err_setstr(ImportError,
553				   "Bad code object in .pyc file");
554			return NULL;
555		}
556		co = (codeobject *)v;
557		if (verbose)
558			fprintf(stderr,
559				"import %s # precompiled from %s\n",
560				name, namebuf);
561		break;
562
563#ifdef DYNAMIC_LINK
564	case C_EXTENSION:
565		fclose(fp);
566		return load_dynamic_module(name, namebuf, m, m_ret);
567#endif /* DYNAMIC_LINK */
568
569	default:
570		fclose(fp);
571		err_setstr(SystemError,
572			   "search loop returned unexpected result");
573		return NULL;
574
575	}
576
577	/* We get here for either PY_SOURCE or PY_COMPILED */
578	if (m == NULL) {
579		m = add_module(name);
580		if (m == NULL) {
581			freetree(n);
582			return NULL;
583		}
584		*m_ret = m;
585	}
586	d = getmoduledict(m);
587	v = eval_code(co, d, d, d, (object *)NULL);
588	DECREF(co);
589	return v;
590}
591
592static object *
593load_module(name)
594	char *name;
595{
596	object *m, *v;
597	v = get_module((object *)NULL, name, &m);
598	if (v == NULL)
599		return NULL;
600	DECREF(v);
601	return m;
602}
603
604object *
605import_module(name)
606	char *name;
607{
608	object *m;
609	int n;
610	if ((m = dictlookup(modules, name)) == NULL) {
611		if ((n = init_builtin(name)) || (n = init_frozen(name))) {
612			if (n < 0)
613				return NULL;
614			if ((m = dictlookup(modules, name)) == NULL) {
615				if (err_occurred() == NULL)
616					err_setstr(SystemError,
617				   "builtin module not initialized properly");
618			}
619		}
620		else {
621			m = load_module(name);
622		}
623	}
624	return m;
625}
626
627object *
628reload_module(m)
629	object *m;
630{
631	char *name;
632	int i;
633	if (m == NULL || !is_moduleobject(m)) {
634		err_setstr(TypeError, "reload() argument must be module");
635		return NULL;
636	}
637	name = getmodulename(m);
638	if (name == NULL)
639		return NULL;
640	/* Check for built-in modules */
641	for (i = 0; inittab[i].name != NULL; i++) {
642		if (strcmp(name, inittab[i].name) == 0) {
643			err_setstr(ImportError,
644				   "cannot reload built-in module");
645			return NULL;
646		}
647	}
648	/* Check for frozen modules */
649	if ((i = init_frozen(name)) != 0) {
650		if (i < 0)
651			return NULL;
652		INCREF(None);
653		return None;
654	}
655	return get_module(m, name, (object **)NULL);
656}
657
658void
659doneimport()
660{
661	if (modules != NULL) {
662		int pos;
663		object *modname, *module;
664		/* Explicitly erase all modules; this is the safest way
665		   to get rid of at least *some* circular dependencies */
666		pos = 0;
667		while (mappinggetnext(modules, &pos, &modname, &module)) {
668			if (is_moduleobject(module)) {
669				object *dict;
670				dict = getmoduledict(module);
671				if (dict != NULL && is_dictobject(dict))
672					mappingclear(dict);
673			}
674		}
675		mappingclear(modules);
676	}
677	DECREF(modules);
678	modules = NULL;
679}
680
681
682/* Initialize built-in modules when first imported */
683
684static int
685init_builtin(name)
686	char *name;
687{
688	int i;
689	for (i = 0; inittab[i].name != NULL; i++) {
690		if (strcmp(name, inittab[i].name) == 0) {
691			if (inittab[i].initfunc == NULL) {
692				err_setstr(ImportError,
693					   "cannot re-init internal module");
694				return -1;
695			}
696			if (verbose)
697				fprintf(stderr, "import %s # builtin\n",
698					name);
699			(*inittab[i].initfunc)();
700			return 1;
701		}
702	}
703	return 0;
704}
705
706extern struct frozen {
707	char *name;
708	char *code;
709	int size;
710} frozen_modules[];
711
712int
713init_frozen(name)
714	char *name;
715{
716	struct frozen *p;
717	codeobject *co;
718	object *m, *d, *v;
719	for (p = frozen_modules; ; p++) {
720		if (p->name == NULL)
721			return 0;
722		if (strcmp(p->name, name) == 0)
723			break;
724	}
725	if (verbose)
726		fprintf(stderr, "import %s # frozen\n", name);
727	co = (codeobject *) rds_object(p->code, p->size);
728	if (co == NULL)
729		return -1;
730	if ((m = add_module(name)) == NULL ||
731	    (d = getmoduledict(m)) == NULL ||
732	    (v = eval_code(co, d, d, d, (object*)NULL)) == NULL) {
733		DECREF(co);
734		return -1;
735	}
736	DECREF(co);
737	DECREF(v);
738	return 1;
739}
740
741
742#ifdef _AIX
743
744#include <ctype.h>	/* for isdigit()	*/
745#include <errno.h>	/* for global errno	*/
746#include <string.h>	/* for strerror()	*/
747
748void aix_loaderror(char *namebuf)
749{
750
751	char *message[8], errbuf[1024];
752	int i,j;
753
754	struct errtab {
755		int errno;
756		char *errstr;
757	} load_errtab[] = {
758		{L_ERROR_TOOMANY,	"to many errors, rest skipped."},
759		{L_ERROR_NOLIB,		"can't load library:"},
760		{L_ERROR_UNDEF,		"can't find symbol in library:"},
761		{L_ERROR_RLDBAD,
762		 "RLD index out of range or bad relocation type:"},
763		{L_ERROR_FORMAT,	"not a valid, executable xcoff file:"},
764		{L_ERROR_MEMBER,
765		 "file not an archive or does not contain requested member:"},
766		{L_ERROR_TYPE,		"symbol table mismatch:"},
767		{L_ERROR_ALIGN,		"text allignment in file is wrong."},
768		{L_ERROR_SYSTEM,	"System error:"},
769		{L_ERROR_ERRNO,		NULL}
770	};
771
772#define LOAD_ERRTAB_LEN	(sizeof(load_errtab)/sizeof(load_errtab[0]))
773#define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1)
774
775	sprintf(errbuf, " from module %.200s ", namebuf);
776
777	if (!loadquery(1, &message[0], sizeof(message)))
778		ERRBUF_APPEND(strerror(errno));
779	for(i = 0; message[i] && *message[i]; i++) {
780		int nerr = atoi(message[i]);
781		for (j=0; j<LOAD_ERRTAB_LEN ; j++) {
782		    if (nerr == load_errtab[i].errno && load_errtab[i].errstr)
783			ERRBUF_APPEND(load_errtab[i].errstr);
784		}
785		while (isdigit(*message[i])) message[i]++ ;
786		ERRBUF_APPEND(message[i]);
787		ERRBUF_APPEND("\n");
788	}
789	errbuf[strlen(errbuf)-1] = '\0';	/* trim off last newline */
790	err_setstr(ImportError, errbuf);
791	return;
792}
793
794#endif /* _AIX */
795