blob: 155ce34b37c50b75ab19007b2a94c3395e62f8f8 [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 Rossumc3880681995-02-19 15:54:18 +000051/* Increment by one for each incompatible change */
52/* MPW swaps CR and LF, so their value is incorporated as well */
53#define MAGIC (0x999903L ^ (('\n'^10L)<<16) ^ (('\r'^13L)<<8))
Guido van Rossum3ddee711991-12-16 13:06:34 +000054
Guido van Rossum1ae940a1995-01-02 19:04:15 +000055object *import_modules; /* This becomes sys.modules */
Guido van Rossum3f5da241990-12-20 15:06:42 +000056
Guido van Rossum66f1fa81991-04-03 19:03:52 +000057
Guido van Rossum1ae940a1995-01-02 19:04:15 +000058/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059
60void
61initimport()
62{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000063 if (import_modules != NULL)
64 fatal("duplicate initimport() call");
65 if ((import_modules = newdictobject()) == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +000066 fatal("no mem for dictionary of modules");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067}
68
Guido van Rossum3f5da241990-12-20 15:06:42 +000069
Guido van Rossum1ae940a1995-01-02 19:04:15 +000070/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +000071
Guido van Rossum3f5da241990-12-20 15:06:42 +000072void
73doneimport()
74{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000075 if (import_modules != NULL) {
Guido van Rossum0de81bf1995-01-26 00:41:28 +000076 object *tmp = import_modules;
77 import_modules = NULL;
78 /* This deletes all modules from sys.modules.
79 When a module is deallocated, it in turn clears its dictionary,
80 thus hopefully breaking any circular references between modules
81 and between a module's dictionary and its functions.
82 Note that "import" will fail while we are cleaning up.
83 */
84 mappingclear(tmp);
85 DECREF(tmp);
Guido van Rossum3f5da241990-12-20 15:06:42 +000086 }
Guido van Rossum8d15b5d1990-10-26 14:58:58 +000087}
Guido van Rossum7f133ed1991-02-19 12:23:57 +000088
89
Guido van Rossum1ae940a1995-01-02 19:04:15 +000090/* Helper for pythonrun.c -- return magic number */
91
92long
93get_pyc_magic()
94{
95 return MAGIC;
96}
97
98
99/* Helper for sysmodule.c -- return modules dictionary */
100
101object *
102get_modules()
103{
104 return import_modules;
105}
106
107
108/* Get the module object corresponding to a module name.
109 First check the modules dictionary if there's one there,
110 if not, create a new one and insert in in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000111 Because the former action is most common, THIS DOES NOT RETURN A
112 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000113
114object *
115add_module(name)
116 char *name;
117{
118 object *m;
119
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000120 if (import_modules == NULL) {
121 err_setstr(SystemError, "sys.modules has been deleted");
122 return NULL;
123 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000124 if ((m = dictlookup(import_modules, name)) != NULL &&
125 is_moduleobject(m))
126 return m;
127 m = newmoduleobject(name);
128 if (m == NULL)
129 return NULL;
130 if (dictinsert(import_modules, name, m) != 0) {
131 DECREF(m);
132 return NULL;
133 }
134 DECREF(m); /* Yes, it still exists, in modules! */
135
136 return m;
137}
138
139
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000140/* Execute a code object in a module and return the module object
141 WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000142
Jack Jansen9c96a921995-02-15 22:57:06 +0000143object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000144exec_code_module(name, co)
145 char *name;
Jack Jansen9c96a921995-02-15 22:57:06 +0000146 object *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000147{
148 object *m, *d, *v;
149
150 m = add_module(name);
151 if (m == NULL)
152 return NULL;
153 d = getmoduledict(m);
Guido van Rossum6135a871995-01-09 17:53:26 +0000154 if (dictlookup(d, "__builtins__") == NULL) {
Guido van Rossum94390ec1995-01-12 11:37:57 +0000155 if (dictinsert(d, "__builtins__", getbuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000156 return NULL;
157 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000158 v = eval_code((codeobject *)co, d, d, d, (object *)NULL);
159 if (v == NULL)
160 return NULL;
161 DECREF(v);
162 INCREF(m);
163
164 return m;
165}
166
167
168/* Given a pathname for a Python source file, fill a buffer with the
169 pathname for the corresponding compiled file. Return the pathname
170 for the compiled file, or NULL if there's no space in the buffer.
171 Doesn't set an exception. */
172
173static char *
174make_compiled_pathname(pathname, buf, buflen)
175 char *pathname;
176 char *buf;
177 int buflen;
178{
179 int len;
180
181 len = strlen(pathname);
182 if (len+2 > buflen)
183 return NULL;
184 strcpy(buf, pathname);
185 strcpy(buf+len, "c");
186
187 return buf;
188}
189
190
191/* Given a pathname for a Python source file, its time of last
192 modification, and a pathname for a compiled file, check whether the
193 compiled file represents the same version of the source. If so,
194 return a FILE pointer for the compiled file, positioned just after
195 the header; if not, return NULL.
196 Doesn't set an exception. */
197
198static FILE *
199check_compiled_module(pathname, mtime, cpathname)
200 char *pathname;
201 long mtime;
202 char *cpathname;
203{
204 FILE *fp;
205 long magic;
206 long pyc_mtime;
207
208 fp = fopen(cpathname, "rb");
209 if (fp == NULL)
210 return NULL;
211 magic = rd_long(fp);
212 if (magic != MAGIC) {
213 if (verbose)
214 fprintf(stderr, "# %s has bad magic\n", cpathname);
215 fclose(fp);
216 return NULL;
217 }
218 pyc_mtime = rd_long(fp);
219 if (pyc_mtime != mtime) {
220 if (verbose)
221 fprintf(stderr, "# %s has bad mtime\n", cpathname);
222 fclose(fp);
223 return NULL;
224 }
225 if (verbose)
226 fprintf(stderr, "# %s matches %s\n", cpathname, pathname);
227 return fp;
228}
229
230
231/* Read a code object from a file and check it for validity */
232
233static codeobject *
234read_compiled_module(fp)
235 FILE *fp;
236{
237 object *co;
238
239 co = rd_object(fp);
240 /* Ugly: rd_object() may return NULL with or without error */
241 if (co == NULL || !is_codeobject(co)) {
242 if (!err_occurred())
243 err_setstr(ImportError,
244 "Non-code object in .pyc file");
245 XDECREF(co);
246 return NULL;
247 }
248 return (codeobject *)co;
249}
250
251
252/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000253 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000254
255static object *
256load_compiled_module(name, cpathname, fp)
257 char *name;
258 char *cpathname;
259 FILE *fp;
260{
261 long magic;
262 codeobject *co;
263 object *m;
264
265 magic = rd_long(fp);
266 if (magic != MAGIC) {
267 err_setstr(ImportError, "Bad magic number in .pyc file");
268 return NULL;
269 }
270 (void) rd_long(fp);
271 co = read_compiled_module(fp);
272 if (co == NULL)
273 return NULL;
274 if (verbose)
275 fprintf(stderr, "import %s # precompiled from %s\n",
276 name, cpathname);
Jack Jansen9c96a921995-02-15 22:57:06 +0000277 m = exec_code_module(name, (object *)co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000278 DECREF(co);
279
280 return m;
281}
282
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000283/* Parse a source file and return the corresponding code object */
284
285static codeobject *
286parse_source_module(pathname, fp)
287 char *pathname;
288 FILE *fp;
289{
290 codeobject *co;
291 node *n;
292
293 n = parse_file(fp, pathname, file_input);
294 if (n == NULL)
295 return NULL;
296 co = compile(n, pathname);
297 freetree(n);
298
299 return co;
300}
301
302
303/* Write a compiled module to a file, placing the time of last
304 modification of its source into the header.
305 Errors are ignored, if a write error occurs an attempt is made to
306 remove the file. */
307
308static void
309write_compiled_module(co, cpathname, mtime)
310 codeobject *co;
311 char *cpathname;
312 long mtime;
313{
314 FILE *fp;
315
316 fp = fopen(cpathname, "wb");
317 if (fp == NULL) {
318 if (verbose)
319 fprintf(stderr,
320 "# can't create %s\n", cpathname);
321 return;
322 }
323 wr_long(MAGIC, fp);
324 /* First write a 0 for mtime */
325 wr_long(0L, fp);
326 wr_object((object *)co, fp);
327 if (ferror(fp)) {
328 if (verbose)
329 fprintf(stderr, "# can't write %s\n", cpathname);
330 /* Don't keep partial file */
331 fclose(fp);
332 (void) unlink(cpathname);
333 return;
334 }
335 /* Now write the true mtime */
336 fseek(fp, 4L, 0);
337 wr_long(mtime, fp);
338 fflush(fp);
339 fclose(fp);
340 if (verbose)
341 fprintf(stderr, "# wrote %s\n", cpathname);
342#ifdef macintosh
343 setfiletype(cpathname, 'PYTH', 'PYC ');
344#endif
345}
346
347
348/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000349 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
350 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000351
352static object *
353load_source_module(name, pathname, fp)
354 char *name;
355 char *pathname;
356 FILE *fp;
357{
358 long mtime;
359 FILE *fpc;
360 char buf[MAXPATHLEN+1];
361 char *cpathname;
362 codeobject *co;
363 object *m;
364
365 mtime = getmtime(pathname);
366 cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1);
367 if (cpathname != NULL &&
368 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
369 co = read_compiled_module(fpc);
370 fclose(fpc);
371 if (co == NULL)
372 return NULL;
373 if (verbose)
374 fprintf(stderr, "import %s # precompiled from %s\n",
375 name, cpathname);
376 }
377 else {
378 co = parse_source_module(pathname, fp);
379 if (co == NULL)
380 return NULL;
381 if (verbose)
382 fprintf(stderr, "import %s # from %s\n",
383 name, pathname);
384 write_compiled_module(co, cpathname, mtime);
385 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000386 m = exec_code_module(name, (object *)co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000387 DECREF(co);
388
389 return m;
390}
391
392
393/* Search the path (default sys.path) for a module. Return the
394 corresponding filedescr struct, and (via return arguments) the
395 pathname and an open file. Return NULL if the module is not found. */
396
397static struct filedescr *
398find_module(name, path, buf, buflen, p_fp)
399 char *name;
400 object *path;
401 /* Output parameters: */
402 char *buf;
403 int buflen;
404 FILE **p_fp;
405{
406 int i, npath, len, namelen;
407 struct filedescr *fdp;
408 FILE *fp;
409
410 if (path == NULL)
411 path = sysget("path");
412 if (path == NULL || !is_listobject(path)) {
413 err_setstr(ImportError,
414 "module search path must be list of directory names");
415 return NULL;
416 }
417 npath = getlistsize(path);
418 namelen = strlen(name);
419 for (i = 0; i < npath; i++) {
420 object *v = getlistitem(path, i);
421 if (!is_stringobject(v))
422 continue;
423 len = getstringsize(v);
424 if (len + 2 + namelen + import_maxsuffixsize >= buflen)
425 continue; /* Too long */
426 strcpy(buf, getstringvalue(v));
427 if (strlen(buf) != len)
428 continue; /* v contains '\0' */
Jack Jansen9c96a921995-02-15 22:57:06 +0000429#ifdef macintosh
430 if ( PyMac_FindResourceModule(name, buf) ) {
431 static struct filedescr resfiledescr = { "", "", PY_RESOURCE};
432
433 return &resfiledescr;
434 }
435#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000436 if (len > 0 && buf[len-1] != SEP)
437 buf[len++] = SEP;
438 strcpy(buf+len, name);
439 len += namelen;
440 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
441 strcpy(buf+len, fdp->suffix);
442 if (verbose > 1)
443 fprintf(stderr, "# trying %s\n", buf);
444 fp = fopen(buf, fdp->mode);
445 if (fp != NULL)
446 break;
447 }
448 if (fp != NULL)
449 break;
450 }
451 if (fp == NULL) {
452 char buf[256];
453 sprintf(buf, "No module named %.200s", name);
454 err_setstr(ImportError, buf);
455 return NULL;
456 }
457
458 *p_fp = fp;
459 return fdp;
460}
461
462
463/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000464 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000465
466static object *
467load_module(name)
468 char *name;
469{
470 char buf[MAXPATHLEN+1];
471 struct filedescr *fdp;
472 FILE *fp = NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000473 object *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000474
475 fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp);
476 if (fdp == NULL)
477 return NULL;
478
479 switch (fdp->type) {
480
481 case PY_SOURCE:
482 m = load_source_module(name, buf, fp);
483 break;
484
485 case PY_COMPILED:
486 m = load_compiled_module(name, buf, fp);
487 break;
488
489 case C_EXTENSION:
490 m = load_dynamic_module(name, buf);
491 break;
492
Jack Jansen9c96a921995-02-15 22:57:06 +0000493#ifdef macintosh
494 case PY_RESOURCE:
495 m = PyMac_LoadResourceModule(name, buf);
496 break;
497#endif
498
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000499 default:
500 err_setstr(SystemError,
501 "find_module returned unexpected result");
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000502 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000503
504 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000505 if ( fp )
506 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000507
508 return m;
509}
510
511
512/* Initialize a built-in module.
513 Return 1 for succes, 0 if the module is not found, and -1 with
514 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000515
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000516static int
517init_builtin(name)
518 char *name;
519{
520 int i;
521 for (i = 0; inittab[i].name != NULL; i++) {
522 if (strcmp(name, inittab[i].name) == 0) {
Guido van Rossum74e6a111994-08-29 12:54:38 +0000523 if (inittab[i].initfunc == NULL) {
524 err_setstr(ImportError,
525 "cannot re-init internal module");
526 return -1;
527 }
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +0000528 if (verbose)
529 fprintf(stderr, "import %s # builtin\n",
530 name);
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000531 (*inittab[i].initfunc)();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000532 if (err_occurred())
533 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000534 return 1;
535 }
536 }
537 return 0;
538}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000539
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000540
541/* Initialize a frozen module.
542 Return 1 for succes, 0 if the module is not found, and -1 with
543 an exception set if the initialization failed. */
544
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000545extern struct frozen {
546 char *name;
547 char *code;
548 int size;
549} frozen_modules[];
550
Guido van Rossum0b344901995-02-07 15:35:27 +0000551/* This function is also used from frozenmain.c */
552
553int
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000554init_frozen(name)
555 char *name;
556{
557 struct frozen *p;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000558 object *co;
559 object *m;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000560 for (p = frozen_modules; ; p++) {
561 if (p->name == NULL)
562 return 0;
563 if (strcmp(p->name, name) == 0)
564 break;
565 }
566 if (verbose)
567 fprintf(stderr, "import %s # frozen\n", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000568 co = rds_object(p->code, p->size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000569 if (co == NULL)
570 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000571 if (!is_codeobject(co)) {
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000572 DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000573 err_setstr(SystemError, "frozen object is not a code object");
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000574 return -1;
575 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000576 m = exec_code_module(name, co);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000577 DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000578 if (m == NULL)
579 return -1;
580 DECREF(m);
581 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000582}
Guido van Rossum74e6a111994-08-29 12:54:38 +0000583
584
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000585/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000586 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +0000587
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000588object *
589import_module(name)
590 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000591{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000592 object *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000593
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000594 if (import_modules == NULL) {
595 err_setstr(SystemError, "sys.modules has been deleted");
596 return NULL;
597 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000598 if ((m = dictlookup(import_modules, name)) != NULL) {
599 INCREF(m);
600 }
601 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000602 int i;
603 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
604 if (i < 0)
605 return NULL;
606 if ((m = dictlookup(import_modules, name)) == NULL) {
607 if (err_occurred() == NULL)
608 err_setstr(SystemError,
609 "built-in module not initialized properly");
610 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000611 else
612 INCREF(m);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000613 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000614 else
615 m = load_module(name);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000616 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000617
618 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000619}
620
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000621
622/* Re-import a module of any kind and return its module object, WITH
623 INCREMENTED REFERENCE COUNT */
624
625object *
626reload_module(m)
627 object *m;
628{
629 char *name;
630 int i;
631
632 if (m == NULL || !is_moduleobject(m)) {
633 err_setstr(TypeError, "reload() argument must be module");
634 return NULL;
635 }
636 name = getmodulename(m);
637 if (name == NULL)
638 return NULL;
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000639 if (import_modules == NULL) {
640 err_setstr(SystemError, "sys.modules has been deleted");
641 return NULL;
642 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000643 if (m != dictlookup(import_modules, name)) {
644 err_setstr(ImportError, "reload() module not in sys.modules");
645 return NULL;
646 }
647 /* Check for built-in and frozen modules */
648 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
649 if (i < 0)
650 return NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000651 INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000652 }
653 else
654 m = load_module(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000655 return m;
656}
657
658
659/* Module 'imp' provides Python access to the primitives used for
660 importing modules.
661*/
662
663static object *
664imp_get_magic(self, args)
665 object *self;
666 object *args;
667{
668 char buf[4];
669
670 if (!newgetargs(args, ""))
671 return NULL;
672 buf[0] = (MAGIC >> 0) & 0xff;
673 buf[1] = (MAGIC >> 8) & 0xff;
Guido van Rossum90f0e071995-01-30 12:53:06 +0000674 buf[2] = (MAGIC >> 16) & 0xff;
675 buf[3] = (MAGIC >> 24) & 0xff;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000676
677 return newsizedstringobject(buf, 4);
678}
679
680static object *
681imp_get_suffixes(self, args)
682 object *self;
683 object *args;
684{
685 object *list;
686 struct filedescr *fdp;
687
688 if (!newgetargs(args, ""))
689 return NULL;
690 list = newlistobject(0);
691 if (list == NULL)
692 return NULL;
693 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
694 object *item = mkvalue("ssi",
695 fdp->suffix, fdp->mode, fdp->type);
696 if (item == NULL) {
697 DECREF(list);
698 return NULL;
699 }
700 if (addlistitem(list, item) < 0) {
701 DECREF(list);
702 DECREF(item);
703 return NULL;
704 }
705 DECREF(item);
706 }
707 return list;
708}
709
710static object *
711imp_find_module(self, args)
712 object *self;
713 object *args;
714{
715 extern int fclose PROTO((FILE *));
716 char *name;
717 object *path = NULL;
718 object *fob, *ret;
719 struct filedescr *fdp;
720 char pathname[MAXPATHLEN+1];
721 FILE *fp;
722 if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
723 return NULL;
724 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
725 if (fdp == NULL)
726 return NULL;
727 fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
728 if (fob == NULL) {
729 fclose(fp);
730 return NULL;
731 }
732 ret = mkvalue("Os(ssi)",
733 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
734 DECREF(fob);
735 return ret;
736}
737
738static object *
739imp_init_builtin(self, args)
740 object *self;
741 object *args;
742{
743 char *name;
744 int ret;
745 object *m;
746 if (!newgetargs(args, "s", &name))
747 return NULL;
748 ret = init_builtin(name);
749 if (ret < 0)
750 return NULL;
751 if (ret == 0) {
752 INCREF(None);
753 return None;
754 }
755 m = add_module(name);
756 XINCREF(m);
757 return m;
758}
759
760static object *
761imp_init_frozen(self, args)
762 object *self;
763 object *args;
764{
765 char *name;
766 int ret;
767 object *m;
768 if (!newgetargs(args, "s", &name))
769 return NULL;
770 ret = init_frozen(name);
771 if (ret < 0)
772 return NULL;
773 if (ret == 0) {
774 INCREF(None);
775 return None;
776 }
777 m = add_module(name);
778 XINCREF(m);
779 return m;
780}
781
782static object *
783imp_is_builtin(self, args)
784 object *self;
785 object *args;
786{
787 int i;
788 char *name;
789 if (!newgetargs(args, "s", &name))
790 return NULL;
791 for (i = 0; inittab[i].name != NULL; i++) {
792 if (strcmp(name, inittab[i].name) == 0) {
793 if (inittab[i].initfunc == NULL)
794 return newintobject(-1);
795 else
796 return newintobject(1);
797 }
798 }
799 return newintobject(0);
800}
801
802static object *
803imp_is_frozen(self, args)
804 object *self;
805 object *args;
806{
807 struct frozen *p;
808 char *name;
809 if (!newgetargs(args, "s", &name))
810 return NULL;
811 for (p = frozen_modules; ; p++) {
812 if (p->name == NULL)
813 break;
814 if (strcmp(p->name, name) == 0)
815 return newintobject(1);
816 }
817 return newintobject(0);
818}
819
820static FILE *
821get_file(pathname, fob, mode)
822 char *pathname;
823 object *fob;
824 char *mode;
825{
826 FILE *fp;
827 if (fob == NULL) {
828 fp = fopen(pathname, mode);
829 if (fp == NULL)
830 err_errno(IOError);
831 }
832 else {
833 fp = getfilefile(fob);
834 if (fp == NULL)
835 err_setstr(ValueError, "bad/closed file object");
836 }
837 return fp;
838}
839
840static object *
841imp_load_compiled(self, args)
842 object *self;
843 object *args;
844{
845 char *name;
846 char *pathname;
847 object *fob = NULL;
848 object *m;
849 FILE *fp;
850 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
851 return NULL;
852 fp = get_file(pathname, fob, "rb");
853 if (fp == NULL)
854 return NULL;
855 m = load_compiled_module(name, pathname, fp);
856 if (fob == NULL)
857 fclose(fp);
858 return m;
859}
860
861static object *
862imp_load_dynamic(self, args)
863 object *self;
864 object *args;
865{
866 char *name;
867 char *pathname;
868 object *dummy;
869 if (!newgetargs(args, "ss|O", &name, &pathname, &dummy))
870 return NULL;
871 return load_dynamic_module(name, pathname);
872}
873
874static object *
875imp_load_source(self, args)
876 object *self;
877 object *args;
878{
879 char *name;
880 char *pathname;
881 object *fob = NULL;
882 object *m;
883 FILE *fp;
884 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
885 return NULL;
886 fp = get_file(pathname, fob, "r");
887 if (fp == NULL)
888 return NULL;
889 m = load_source_module(name, pathname, fp);
890 if (fob == NULL)
891 fclose(fp);
892 return m;
893}
894
Jack Jansen9c96a921995-02-15 22:57:06 +0000895#ifdef macintosh
896static object *
897imp_load_resource(self, args)
898 object *self;
899 object *args;
900{
901 char *name;
902 char *pathname;
903 object *m;
904
905 if (!newgetargs(args, "ss", &name, &pathname))
906 return NULL;
907 m = PyMac_LoadResourceModule(name, pathname);
908 return m;
909}
910#endif /* macintosh */
911
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000912static object *
913imp_new_module(self, args)
914 object *self;
915 object *args;
916{
917 char *name;
918 if (!newgetargs(args, "s", &name))
919 return NULL;
920 return newmoduleobject(name);
921}
922
923static struct methodlist imp_methods[] = {
924 {"get_magic", imp_get_magic, 1},
925 {"get_suffixes", imp_get_suffixes, 1},
926 {"find_module", imp_find_module, 1},
927 {"init_builtin", imp_init_builtin, 1},
928 {"init_frozen", imp_init_frozen, 1},
929 {"is_builtin", imp_is_builtin, 1},
930 {"is_frozen", imp_is_frozen, 1},
931 {"load_compiled", imp_load_compiled, 1},
932 {"load_dynamic", imp_load_dynamic, 1},
933 {"load_source", imp_load_source, 1},
934 {"new_module", imp_new_module, 1},
Jack Jansen9c96a921995-02-15 22:57:06 +0000935#ifdef macintosh
936 {"load_resource", imp_load_resource, 1},
937#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000938 {NULL, NULL} /* sentinel */
939};
940
941void
942initimp()
943{
944 object *m, *d, *v;
945
946 m = initmodule("imp", imp_methods);
947 d = getmoduledict(m);
948
949 v = newintobject(SEARCH_ERROR);
950 dictinsert(d, "SEARCH_ERROR", v);
951 XDECREF(v);
952
953 v = newintobject(PY_SOURCE);
954 dictinsert(d, "PY_SOURCE", v);
955 XDECREF(v);
956
957 v = newintobject(PY_COMPILED);
958 dictinsert(d, "PY_COMPILED", v);
959 XDECREF(v);
960
961 v = newintobject(C_EXTENSION);
962 dictinsert(d, "C_EXTENSION", v);
963 XDECREF(v);
964
965 if (err_occurred())
966 fatal("imp module initialization failed");
967}