blob: 4239e1230f7354d8781a6596c12dbf9ed6b03728 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
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
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Module definition and import implementation */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000029#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030#include "token.h"
31#include "graminit.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032#include "import.h"
33#include "errcode.h"
34#include "sysmodule.h"
Guido van Rossum6135a871995-01-09 17:53:26 +000035#include "bltinmodule.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000036#include "pythonrun.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000037#include "marshal.h"
38#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000039#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000040#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000041#include "importdl.h"
Jack Jansen9c96a921995-02-15 22:57:06 +000042#ifdef macintosh
43#include "macglue.h"
44#endif
Guido van Rossumc405b7b1991-06-04 19:39:42 +000045
Guido van Rossum74e6a111994-08-29 12:54:38 +000046extern int verbose; /* Defined in pythonrun.c */
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +000047
Guido van Rossum74e6a111994-08-29 12:54:38 +000048extern long getmtime(); /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000049
Guido van Rossum6c849691994-09-26 15:47:17 +000050/* Magic word to reject .pyc files generated by other Python versions */
Guido van Rossum7faeab31995-07-07 22:50:36 +000051/* Change for each incompatible change */
52/* The value of CR and LF is incorporated so if you ever read or write
53 a .pyc file in text mode the magic number will be wrong; also, the
54 Apple MPW compiler swaps their values, botching string constants */
55/* XXX Perhaps the magic number should be frozen and a version field
56 added to the .pyc file header? */
57#define MAGIC (0x4127L | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000058
Guido van Rossum1ae940a1995-01-02 19:04:15 +000059object *import_modules; /* This becomes sys.modules */
Guido van Rossum3f5da241990-12-20 15:06:42 +000060
Guido van Rossum66f1fa81991-04-03 19:03:52 +000061
Guido van Rossum1ae940a1995-01-02 19:04:15 +000062/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063
64void
65initimport()
66{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000067 if (import_modules != NULL)
68 fatal("duplicate initimport() call");
69 if ((import_modules = newdictobject()) == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +000070 fatal("no mem for dictionary of modules");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000071}
72
Guido van Rossum3f5da241990-12-20 15:06:42 +000073
Guido van Rossum1ae940a1995-01-02 19:04:15 +000074/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +000075
Guido van Rossum3f5da241990-12-20 15:06:42 +000076void
77doneimport()
78{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000079 if (import_modules != NULL) {
Guido van Rossum0de81bf1995-01-26 00:41:28 +000080 object *tmp = import_modules;
81 import_modules = NULL;
82 /* This deletes all modules from sys.modules.
83 When a module is deallocated, it in turn clears its dictionary,
84 thus hopefully breaking any circular references between modules
85 and between a module's dictionary and its functions.
86 Note that "import" will fail while we are cleaning up.
87 */
88 mappingclear(tmp);
89 DECREF(tmp);
Guido van Rossum3f5da241990-12-20 15:06:42 +000090 }
Guido van Rossum8d15b5d1990-10-26 14:58:58 +000091}
Guido van Rossum7f133ed1991-02-19 12:23:57 +000092
93
Guido van Rossum1ae940a1995-01-02 19:04:15 +000094/* Helper for pythonrun.c -- return magic number */
95
96long
97get_pyc_magic()
98{
99 return MAGIC;
100}
101
102
103/* Helper for sysmodule.c -- return modules dictionary */
104
105object *
106get_modules()
107{
108 return import_modules;
109}
110
111
112/* Get the module object corresponding to a module name.
113 First check the modules dictionary if there's one there,
114 if not, create a new one and insert in in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000115 Because the former action is most common, THIS DOES NOT RETURN A
116 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000117
118object *
119add_module(name)
120 char *name;
121{
122 object *m;
123
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000124 if (import_modules == NULL) {
125 err_setstr(SystemError, "sys.modules has been deleted");
126 return NULL;
127 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000128 if ((m = dictlookup(import_modules, name)) != NULL &&
129 is_moduleobject(m))
130 return m;
131 m = newmoduleobject(name);
132 if (m == NULL)
133 return NULL;
134 if (dictinsert(import_modules, name, m) != 0) {
135 DECREF(m);
136 return NULL;
137 }
138 DECREF(m); /* Yes, it still exists, in modules! */
139
140 return m;
141}
142
143
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000144/* Execute a code object in a module and return the module object
145 WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000146
Jack Jansen9c96a921995-02-15 22:57:06 +0000147object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000148exec_code_module(name, co)
149 char *name;
Jack Jansen9c96a921995-02-15 22:57:06 +0000150 object *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000151{
152 object *m, *d, *v;
153
154 m = add_module(name);
155 if (m == NULL)
156 return NULL;
157 d = getmoduledict(m);
Guido van Rossum6135a871995-01-09 17:53:26 +0000158 if (dictlookup(d, "__builtins__") == NULL) {
Guido van Rossum94390ec1995-01-12 11:37:57 +0000159 if (dictinsert(d, "__builtins__", getbuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000160 return NULL;
161 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000162 v = eval_code((codeobject *)co, d, d, d, (object *)NULL);
163 if (v == NULL)
164 return NULL;
165 DECREF(v);
166 INCREF(m);
167
168 return m;
169}
170
171
172/* Given a pathname for a Python source file, fill a buffer with the
173 pathname for the corresponding compiled file. Return the pathname
174 for the compiled file, or NULL if there's no space in the buffer.
175 Doesn't set an exception. */
176
177static char *
178make_compiled_pathname(pathname, buf, buflen)
179 char *pathname;
180 char *buf;
181 int buflen;
182{
183 int len;
184
185 len = strlen(pathname);
186 if (len+2 > buflen)
187 return NULL;
188 strcpy(buf, pathname);
189 strcpy(buf+len, "c");
190
191 return buf;
192}
193
194
195/* Given a pathname for a Python source file, its time of last
196 modification, and a pathname for a compiled file, check whether the
197 compiled file represents the same version of the source. If so,
198 return a FILE pointer for the compiled file, positioned just after
199 the header; if not, return NULL.
200 Doesn't set an exception. */
201
202static FILE *
203check_compiled_module(pathname, mtime, cpathname)
204 char *pathname;
205 long mtime;
206 char *cpathname;
207{
208 FILE *fp;
209 long magic;
210 long pyc_mtime;
211
212 fp = fopen(cpathname, "rb");
213 if (fp == NULL)
214 return NULL;
215 magic = rd_long(fp);
216 if (magic != MAGIC) {
217 if (verbose)
218 fprintf(stderr, "# %s has bad magic\n", cpathname);
219 fclose(fp);
220 return NULL;
221 }
222 pyc_mtime = rd_long(fp);
223 if (pyc_mtime != mtime) {
224 if (verbose)
225 fprintf(stderr, "# %s has bad mtime\n", cpathname);
226 fclose(fp);
227 return NULL;
228 }
229 if (verbose)
230 fprintf(stderr, "# %s matches %s\n", cpathname, pathname);
231 return fp;
232}
233
234
235/* Read a code object from a file and check it for validity */
236
237static codeobject *
238read_compiled_module(fp)
239 FILE *fp;
240{
241 object *co;
242
243 co = rd_object(fp);
244 /* Ugly: rd_object() may return NULL with or without error */
245 if (co == NULL || !is_codeobject(co)) {
246 if (!err_occurred())
247 err_setstr(ImportError,
248 "Non-code object in .pyc file");
249 XDECREF(co);
250 return NULL;
251 }
252 return (codeobject *)co;
253}
254
255
256/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000257 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000258
259static object *
260load_compiled_module(name, cpathname, fp)
261 char *name;
262 char *cpathname;
263 FILE *fp;
264{
265 long magic;
266 codeobject *co;
267 object *m;
268
269 magic = rd_long(fp);
270 if (magic != MAGIC) {
271 err_setstr(ImportError, "Bad magic number in .pyc file");
272 return NULL;
273 }
274 (void) rd_long(fp);
275 co = read_compiled_module(fp);
276 if (co == NULL)
277 return NULL;
278 if (verbose)
279 fprintf(stderr, "import %s # precompiled from %s\n",
280 name, cpathname);
Jack Jansen9c96a921995-02-15 22:57:06 +0000281 m = exec_code_module(name, (object *)co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000282 DECREF(co);
283
284 return m;
285}
286
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000287/* Parse a source file and return the corresponding code object */
288
289static codeobject *
290parse_source_module(pathname, fp)
291 char *pathname;
292 FILE *fp;
293{
294 codeobject *co;
295 node *n;
296
297 n = parse_file(fp, pathname, file_input);
298 if (n == NULL)
299 return NULL;
300 co = compile(n, pathname);
301 freetree(n);
302
303 return co;
304}
305
306
307/* Write a compiled module to a file, placing the time of last
308 modification of its source into the header.
309 Errors are ignored, if a write error occurs an attempt is made to
310 remove the file. */
311
312static void
313write_compiled_module(co, cpathname, mtime)
314 codeobject *co;
315 char *cpathname;
316 long mtime;
317{
318 FILE *fp;
319
320 fp = fopen(cpathname, "wb");
321 if (fp == NULL) {
322 if (verbose)
323 fprintf(stderr,
324 "# can't create %s\n", cpathname);
325 return;
326 }
327 wr_long(MAGIC, fp);
328 /* First write a 0 for mtime */
329 wr_long(0L, fp);
330 wr_object((object *)co, fp);
331 if (ferror(fp)) {
332 if (verbose)
333 fprintf(stderr, "# can't write %s\n", cpathname);
334 /* Don't keep partial file */
335 fclose(fp);
336 (void) unlink(cpathname);
337 return;
338 }
339 /* Now write the true mtime */
340 fseek(fp, 4L, 0);
341 wr_long(mtime, fp);
342 fflush(fp);
343 fclose(fp);
344 if (verbose)
345 fprintf(stderr, "# wrote %s\n", cpathname);
346#ifdef macintosh
347 setfiletype(cpathname, 'PYTH', 'PYC ');
348#endif
349}
350
351
352/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000353 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
354 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000355
356static object *
357load_source_module(name, pathname, fp)
358 char *name;
359 char *pathname;
360 FILE *fp;
361{
362 long mtime;
363 FILE *fpc;
364 char buf[MAXPATHLEN+1];
365 char *cpathname;
366 codeobject *co;
367 object *m;
368
369 mtime = getmtime(pathname);
370 cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1);
371 if (cpathname != NULL &&
372 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
373 co = read_compiled_module(fpc);
374 fclose(fpc);
375 if (co == NULL)
376 return NULL;
377 if (verbose)
378 fprintf(stderr, "import %s # precompiled from %s\n",
379 name, cpathname);
380 }
381 else {
382 co = parse_source_module(pathname, fp);
383 if (co == NULL)
384 return NULL;
385 if (verbose)
386 fprintf(stderr, "import %s # from %s\n",
387 name, pathname);
388 write_compiled_module(co, cpathname, mtime);
389 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000390 m = exec_code_module(name, (object *)co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000391 DECREF(co);
392
393 return m;
394}
395
396
397/* Search the path (default sys.path) for a module. Return the
398 corresponding filedescr struct, and (via return arguments) the
399 pathname and an open file. Return NULL if the module is not found. */
400
401static struct filedescr *
402find_module(name, path, buf, buflen, p_fp)
403 char *name;
404 object *path;
405 /* Output parameters: */
406 char *buf;
407 int buflen;
408 FILE **p_fp;
409{
410 int i, npath, len, namelen;
411 struct filedescr *fdp;
412 FILE *fp;
413
414 if (path == NULL)
415 path = sysget("path");
416 if (path == NULL || !is_listobject(path)) {
417 err_setstr(ImportError,
418 "module search path must be list of directory names");
419 return NULL;
420 }
421 npath = getlistsize(path);
422 namelen = strlen(name);
423 for (i = 0; i < npath; i++) {
424 object *v = getlistitem(path, i);
425 if (!is_stringobject(v))
426 continue;
427 len = getstringsize(v);
428 if (len + 2 + namelen + import_maxsuffixsize >= buflen)
429 continue; /* Too long */
430 strcpy(buf, getstringvalue(v));
431 if (strlen(buf) != len)
432 continue; /* v contains '\0' */
Jack Jansen9c96a921995-02-15 22:57:06 +0000433#ifdef macintosh
434 if ( PyMac_FindResourceModule(name, buf) ) {
435 static struct filedescr resfiledescr = { "", "", PY_RESOURCE};
436
437 return &resfiledescr;
438 }
439#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000440 if (len > 0 && buf[len-1] != SEP)
441 buf[len++] = SEP;
442 strcpy(buf+len, name);
443 len += namelen;
444 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
445 strcpy(buf+len, fdp->suffix);
446 if (verbose > 1)
447 fprintf(stderr, "# trying %s\n", buf);
448 fp = fopen(buf, fdp->mode);
449 if (fp != NULL)
450 break;
451 }
452 if (fp != NULL)
453 break;
454 }
455 if (fp == NULL) {
456 char buf[256];
457 sprintf(buf, "No module named %.200s", name);
458 err_setstr(ImportError, buf);
459 return NULL;
460 }
461
462 *p_fp = fp;
463 return fdp;
464}
465
466
467/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000468 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000469
470static object *
471load_module(name)
472 char *name;
473{
474 char buf[MAXPATHLEN+1];
475 struct filedescr *fdp;
476 FILE *fp = NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000477 object *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000478
479 fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp);
480 if (fdp == NULL)
481 return NULL;
482
483 switch (fdp->type) {
484
485 case PY_SOURCE:
486 m = load_source_module(name, buf, fp);
487 break;
488
489 case PY_COMPILED:
490 m = load_compiled_module(name, buf, fp);
491 break;
492
493 case C_EXTENSION:
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000494 m = load_dynamic_module(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000495 break;
496
Jack Jansen9c96a921995-02-15 22:57:06 +0000497#ifdef macintosh
498 case PY_RESOURCE:
499 m = PyMac_LoadResourceModule(name, buf);
500 break;
501#endif
502
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000503 default:
504 err_setstr(SystemError,
505 "find_module returned unexpected result");
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000506 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000507
508 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000509 if ( fp )
510 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000511
512 return m;
513}
514
515
516/* Initialize a built-in module.
517 Return 1 for succes, 0 if the module is not found, and -1 with
518 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000519
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000520static int
521init_builtin(name)
522 char *name;
523{
524 int i;
525 for (i = 0; inittab[i].name != NULL; i++) {
526 if (strcmp(name, inittab[i].name) == 0) {
Guido van Rossum74e6a111994-08-29 12:54:38 +0000527 if (inittab[i].initfunc == NULL) {
528 err_setstr(ImportError,
529 "cannot re-init internal module");
530 return -1;
531 }
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +0000532 if (verbose)
533 fprintf(stderr, "import %s # builtin\n",
534 name);
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000535 (*inittab[i].initfunc)();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000536 if (err_occurred())
537 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000538 return 1;
539 }
540 }
541 return 0;
542}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000543
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000544
545/* Initialize a frozen module.
546 Return 1 for succes, 0 if the module is not found, and -1 with
547 an exception set if the initialization failed. */
548
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000549extern struct frozen {
550 char *name;
551 char *code;
552 int size;
553} frozen_modules[];
554
Guido van Rossum0b344901995-02-07 15:35:27 +0000555/* This function is also used from frozenmain.c */
556
557int
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000558init_frozen(name)
559 char *name;
560{
561 struct frozen *p;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000562 object *co;
563 object *m;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000564 for (p = frozen_modules; ; p++) {
565 if (p->name == NULL)
566 return 0;
567 if (strcmp(p->name, name) == 0)
568 break;
569 }
570 if (verbose)
571 fprintf(stderr, "import %s # frozen\n", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000572 co = rds_object(p->code, p->size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000573 if (co == NULL)
574 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000575 if (!is_codeobject(co)) {
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000576 DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000577 err_setstr(SystemError, "frozen object is not a code object");
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000578 return -1;
579 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000580 m = exec_code_module(name, co);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000581 DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000582 if (m == NULL)
583 return -1;
584 DECREF(m);
585 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000586}
Guido van Rossum74e6a111994-08-29 12:54:38 +0000587
588
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000589/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000590 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +0000591
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000592object *
593import_module(name)
594 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000595{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000596 object *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000597
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000598 if (import_modules == NULL) {
599 err_setstr(SystemError, "sys.modules has been deleted");
600 return NULL;
601 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000602 if ((m = dictlookup(import_modules, name)) != NULL) {
603 INCREF(m);
604 }
605 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000606 int i;
607 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
608 if (i < 0)
609 return NULL;
610 if ((m = dictlookup(import_modules, name)) == NULL) {
611 if (err_occurred() == NULL)
612 err_setstr(SystemError,
613 "built-in module not initialized properly");
614 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000615 else
616 INCREF(m);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000617 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000618 else
619 m = load_module(name);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000620 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000621
622 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000623}
624
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000625
626/* Re-import a module of any kind and return its module object, WITH
627 INCREMENTED REFERENCE COUNT */
628
629object *
630reload_module(m)
631 object *m;
632{
633 char *name;
634 int i;
635
636 if (m == NULL || !is_moduleobject(m)) {
637 err_setstr(TypeError, "reload() argument must be module");
638 return NULL;
639 }
640 name = getmodulename(m);
641 if (name == NULL)
642 return NULL;
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000643 if (import_modules == NULL) {
644 err_setstr(SystemError, "sys.modules has been deleted");
645 return NULL;
646 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000647 if (m != dictlookup(import_modules, name)) {
648 err_setstr(ImportError, "reload() module not in sys.modules");
649 return NULL;
650 }
651 /* Check for built-in and frozen modules */
652 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
653 if (i < 0)
654 return NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000655 INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000656 }
657 else
658 m = load_module(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000659 return m;
660}
661
662
663/* Module 'imp' provides Python access to the primitives used for
664 importing modules.
665*/
666
667static object *
668imp_get_magic(self, args)
669 object *self;
670 object *args;
671{
672 char buf[4];
673
674 if (!newgetargs(args, ""))
675 return NULL;
676 buf[0] = (MAGIC >> 0) & 0xff;
677 buf[1] = (MAGIC >> 8) & 0xff;
Guido van Rossum90f0e071995-01-30 12:53:06 +0000678 buf[2] = (MAGIC >> 16) & 0xff;
679 buf[3] = (MAGIC >> 24) & 0xff;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000680
681 return newsizedstringobject(buf, 4);
682}
683
684static object *
685imp_get_suffixes(self, args)
686 object *self;
687 object *args;
688{
689 object *list;
690 struct filedescr *fdp;
691
692 if (!newgetargs(args, ""))
693 return NULL;
694 list = newlistobject(0);
695 if (list == NULL)
696 return NULL;
697 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
698 object *item = mkvalue("ssi",
699 fdp->suffix, fdp->mode, fdp->type);
700 if (item == NULL) {
701 DECREF(list);
702 return NULL;
703 }
704 if (addlistitem(list, item) < 0) {
705 DECREF(list);
706 DECREF(item);
707 return NULL;
708 }
709 DECREF(item);
710 }
711 return list;
712}
713
714static object *
715imp_find_module(self, args)
716 object *self;
717 object *args;
718{
719 extern int fclose PROTO((FILE *));
720 char *name;
721 object *path = NULL;
722 object *fob, *ret;
723 struct filedescr *fdp;
724 char pathname[MAXPATHLEN+1];
725 FILE *fp;
726 if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
727 return NULL;
728 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
729 if (fdp == NULL)
730 return NULL;
731 fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
732 if (fob == NULL) {
733 fclose(fp);
734 return NULL;
735 }
736 ret = mkvalue("Os(ssi)",
737 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
738 DECREF(fob);
739 return ret;
740}
741
742static object *
743imp_init_builtin(self, args)
744 object *self;
745 object *args;
746{
747 char *name;
748 int ret;
749 object *m;
750 if (!newgetargs(args, "s", &name))
751 return NULL;
752 ret = init_builtin(name);
753 if (ret < 0)
754 return NULL;
755 if (ret == 0) {
756 INCREF(None);
757 return None;
758 }
759 m = add_module(name);
760 XINCREF(m);
761 return m;
762}
763
764static object *
765imp_init_frozen(self, args)
766 object *self;
767 object *args;
768{
769 char *name;
770 int ret;
771 object *m;
772 if (!newgetargs(args, "s", &name))
773 return NULL;
774 ret = init_frozen(name);
775 if (ret < 0)
776 return NULL;
777 if (ret == 0) {
778 INCREF(None);
779 return None;
780 }
781 m = add_module(name);
782 XINCREF(m);
783 return m;
784}
785
786static object *
787imp_is_builtin(self, args)
788 object *self;
789 object *args;
790{
791 int i;
792 char *name;
793 if (!newgetargs(args, "s", &name))
794 return NULL;
795 for (i = 0; inittab[i].name != NULL; i++) {
796 if (strcmp(name, inittab[i].name) == 0) {
797 if (inittab[i].initfunc == NULL)
798 return newintobject(-1);
799 else
800 return newintobject(1);
801 }
802 }
803 return newintobject(0);
804}
805
806static object *
807imp_is_frozen(self, args)
808 object *self;
809 object *args;
810{
811 struct frozen *p;
812 char *name;
813 if (!newgetargs(args, "s", &name))
814 return NULL;
815 for (p = frozen_modules; ; p++) {
816 if (p->name == NULL)
817 break;
818 if (strcmp(p->name, name) == 0)
819 return newintobject(1);
820 }
821 return newintobject(0);
822}
823
824static FILE *
825get_file(pathname, fob, mode)
826 char *pathname;
827 object *fob;
828 char *mode;
829{
830 FILE *fp;
831 if (fob == NULL) {
832 fp = fopen(pathname, mode);
833 if (fp == NULL)
834 err_errno(IOError);
835 }
836 else {
837 fp = getfilefile(fob);
838 if (fp == NULL)
839 err_setstr(ValueError, "bad/closed file object");
840 }
841 return fp;
842}
843
844static object *
845imp_load_compiled(self, args)
846 object *self;
847 object *args;
848{
849 char *name;
850 char *pathname;
851 object *fob = NULL;
852 object *m;
853 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000854 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000855 return NULL;
856 fp = get_file(pathname, fob, "rb");
857 if (fp == NULL)
858 return NULL;
859 m = load_compiled_module(name, pathname, fp);
860 if (fob == NULL)
861 fclose(fp);
862 return m;
863}
864
865static object *
866imp_load_dynamic(self, args)
867 object *self;
868 object *args;
869{
870 char *name;
871 char *pathname;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000872 object *fob = NULL;
873 object *m;
874 FILE *fp = NULL;
875 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000876 return NULL;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000877 if (fob)
878 fp = get_file(pathname, fob, "r");
879 m = load_dynamic_module(name, pathname, fp);
880 if (fob == NULL)
881 fclose(fp);
882 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000883}
884
885static object *
886imp_load_source(self, args)
887 object *self;
888 object *args;
889{
890 char *name;
891 char *pathname;
892 object *fob = NULL;
893 object *m;
894 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000895 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000896 return NULL;
897 fp = get_file(pathname, fob, "r");
898 if (fp == NULL)
899 return NULL;
900 m = load_source_module(name, pathname, fp);
901 if (fob == NULL)
902 fclose(fp);
903 return m;
904}
905
Jack Jansen9c96a921995-02-15 22:57:06 +0000906#ifdef macintosh
907static object *
908imp_load_resource(self, args)
909 object *self;
910 object *args;
911{
912 char *name;
913 char *pathname;
914 object *m;
915
916 if (!newgetargs(args, "ss", &name, &pathname))
917 return NULL;
918 m = PyMac_LoadResourceModule(name, pathname);
919 return m;
920}
921#endif /* macintosh */
922
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000923static object *
924imp_new_module(self, args)
925 object *self;
926 object *args;
927{
928 char *name;
929 if (!newgetargs(args, "s", &name))
930 return NULL;
931 return newmoduleobject(name);
932}
933
934static struct methodlist imp_methods[] = {
935 {"get_magic", imp_get_magic, 1},
936 {"get_suffixes", imp_get_suffixes, 1},
937 {"find_module", imp_find_module, 1},
938 {"init_builtin", imp_init_builtin, 1},
939 {"init_frozen", imp_init_frozen, 1},
940 {"is_builtin", imp_is_builtin, 1},
941 {"is_frozen", imp_is_frozen, 1},
942 {"load_compiled", imp_load_compiled, 1},
943 {"load_dynamic", imp_load_dynamic, 1},
944 {"load_source", imp_load_source, 1},
945 {"new_module", imp_new_module, 1},
Jack Jansen9c96a921995-02-15 22:57:06 +0000946#ifdef macintosh
947 {"load_resource", imp_load_resource, 1},
948#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000949 {NULL, NULL} /* sentinel */
950};
951
952void
953initimp()
954{
955 object *m, *d, *v;
956
957 m = initmodule("imp", imp_methods);
958 d = getmoduledict(m);
959
960 v = newintobject(SEARCH_ERROR);
961 dictinsert(d, "SEARCH_ERROR", v);
962 XDECREF(v);
963
964 v = newintobject(PY_SOURCE);
965 dictinsert(d, "PY_SOURCE", v);
966 XDECREF(v);
967
968 v = newintobject(PY_COMPILED);
969 dictinsert(d, "PY_COMPILED", v);
970 XDECREF(v);
971
972 v = newintobject(C_EXTENSION);
973 dictinsert(d, "C_EXTENSION", v);
974 XDECREF(v);
975
Jack Jansenae12e191995-06-18 20:06:44 +0000976#ifdef macintosh
977 v = newintobject(PY_RESOURCE);
978 dictinsert(d, "PY_RESOURCE", v);
979 XDECREF(v);
980#endif
981
982
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000983 if (err_occurred())
984 fatal("imp module initialization failed");
985}