blob: 12a2c308de9a7d2407eb93d7d87cab7642de9a97 [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 Rossum40f470f1996-05-23 22:51:04 +000029/* XXX Some of the following are duplicate with allobjects.h... */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031#include "token.h"
32#include "graminit.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033#include "import.h"
34#include "errcode.h"
35#include "sysmodule.h"
Guido van Rossum6135a871995-01-09 17:53:26 +000036#include "bltinmodule.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000037#include "pythonrun.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000038#include "marshal.h"
39#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000040#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000041#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000042#include "importdl.h"
Jack Jansen9c96a921995-02-15 22:57:06 +000043#ifdef macintosh
Jack Jansen614cf811995-07-28 11:28:14 +000044/* 'argument' is a grammar symbol, but also used in some mac header files */
45#undef argument
Jack Jansen9c96a921995-02-15 22:57:06 +000046#include "macglue.h"
47#endif
Guido van Rossumc405b7b1991-06-04 19:39:42 +000048
Guido van Rossum74e6a111994-08-29 12:54:38 +000049extern long getmtime(); /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000050
Guido van Rossum6c849691994-09-26 15:47:17 +000051/* Magic word to reject .pyc files generated by other Python versions */
Guido van Rossum7faeab31995-07-07 22:50:36 +000052/* Change for each incompatible change */
53/* The value of CR and LF is incorporated so if you ever read or write
54 a .pyc file in text mode the magic number will be wrong; also, the
55 Apple MPW compiler swaps their values, botching string constants */
56/* XXX Perhaps the magic number should be frozen and a version field
57 added to the .pyc file header? */
Guido van Rossum681d79a1995-07-18 14:51:37 +000058#define MAGIC (11913 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000059
Guido van Rossum1ae940a1995-01-02 19:04:15 +000060object *import_modules; /* This becomes sys.modules */
Guido van Rossum3f5da241990-12-20 15:06:42 +000061
Guido van Rossum66f1fa81991-04-03 19:03:52 +000062
Guido van Rossum1ae940a1995-01-02 19:04:15 +000063/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064
65void
66initimport()
67{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000068 if (import_modules != NULL)
69 fatal("duplicate initimport() call");
70 if ((import_modules = newdictobject()) == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +000071 fatal("no mem for dictionary of modules");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072}
73
Guido van Rossum3f5da241990-12-20 15:06:42 +000074
Guido van Rossum1ae940a1995-01-02 19:04:15 +000075/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +000076
Guido van Rossum3f5da241990-12-20 15:06:42 +000077void
78doneimport()
79{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000080 if (import_modules != NULL) {
Guido van Rossum0de81bf1995-01-26 00:41:28 +000081 object *tmp = import_modules;
82 import_modules = NULL;
83 /* This deletes all modules from sys.modules.
84 When a module is deallocated, it in turn clears its dictionary,
85 thus hopefully breaking any circular references between modules
86 and between a module's dictionary and its functions.
87 Note that "import" will fail while we are cleaning up.
88 */
89 mappingclear(tmp);
90 DECREF(tmp);
Guido van Rossum3f5da241990-12-20 15:06:42 +000091 }
Guido van Rossum8d15b5d1990-10-26 14:58:58 +000092}
Guido van Rossum7f133ed1991-02-19 12:23:57 +000093
94
Guido van Rossum1ae940a1995-01-02 19:04:15 +000095/* Helper for pythonrun.c -- return magic number */
96
97long
98get_pyc_magic()
99{
100 return MAGIC;
101}
102
103
104/* Helper for sysmodule.c -- return modules dictionary */
105
106object *
107get_modules()
108{
109 return import_modules;
110}
111
112
113/* Get the module object corresponding to a module name.
114 First check the modules dictionary if there's one there,
115 if not, create a new one and insert in in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000116 Because the former action is most common, THIS DOES NOT RETURN A
117 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000118
119object *
120add_module(name)
121 char *name;
122{
123 object *m;
124
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000125 if (import_modules == NULL) {
126 err_setstr(SystemError, "sys.modules has been deleted");
127 return NULL;
128 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000129 if ((m = dictlookup(import_modules, name)) != NULL &&
130 is_moduleobject(m))
131 return m;
132 m = newmoduleobject(name);
133 if (m == NULL)
134 return NULL;
135 if (dictinsert(import_modules, name, m) != 0) {
136 DECREF(m);
137 return NULL;
138 }
139 DECREF(m); /* Yes, it still exists, in modules! */
140
141 return m;
142}
143
144
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000145/* Execute a code object in a module and return the module object
146 WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000147
Jack Jansen9c96a921995-02-15 22:57:06 +0000148object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000149exec_code_module(name, co)
150 char *name;
Jack Jansen9c96a921995-02-15 22:57:06 +0000151 object *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000152{
153 object *m, *d, *v;
154
155 m = add_module(name);
156 if (m == NULL)
157 return NULL;
158 d = getmoduledict(m);
Guido van Rossum6135a871995-01-09 17:53:26 +0000159 if (dictlookup(d, "__builtins__") == NULL) {
Guido van Rossum94390ec1995-01-12 11:37:57 +0000160 if (dictinsert(d, "__builtins__", getbuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000161 return NULL;
162 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000163 /* Remember the filename as the __file__ attribute */
164 if (dictinsert(d, "__file__", ((codeobject *)co)->co_filename) != 0)
165 err_clear(); /* Not important enough to report */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000166 v = eval_code((codeobject *)co, d, d); /* XXX owner? */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000167 if (v == NULL)
168 return NULL;
169 DECREF(v);
170 INCREF(m);
171
172 return m;
173}
174
175
176/* Given a pathname for a Python source file, fill a buffer with the
177 pathname for the corresponding compiled file. Return the pathname
178 for the compiled file, or NULL if there's no space in the buffer.
179 Doesn't set an exception. */
180
181static char *
182make_compiled_pathname(pathname, buf, buflen)
183 char *pathname;
184 char *buf;
185 int buflen;
186{
187 int len;
188
189 len = strlen(pathname);
190 if (len+2 > buflen)
191 return NULL;
192 strcpy(buf, pathname);
193 strcpy(buf+len, "c");
194
195 return buf;
196}
197
198
199/* Given a pathname for a Python source file, its time of last
200 modification, and a pathname for a compiled file, check whether the
201 compiled file represents the same version of the source. If so,
202 return a FILE pointer for the compiled file, positioned just after
203 the header; if not, return NULL.
204 Doesn't set an exception. */
205
206static FILE *
207check_compiled_module(pathname, mtime, cpathname)
208 char *pathname;
209 long mtime;
210 char *cpathname;
211{
212 FILE *fp;
213 long magic;
214 long pyc_mtime;
215
216 fp = fopen(cpathname, "rb");
217 if (fp == NULL)
218 return NULL;
219 magic = rd_long(fp);
220 if (magic != MAGIC) {
221 if (verbose)
222 fprintf(stderr, "# %s has bad magic\n", cpathname);
223 fclose(fp);
224 return NULL;
225 }
226 pyc_mtime = rd_long(fp);
227 if (pyc_mtime != mtime) {
228 if (verbose)
229 fprintf(stderr, "# %s has bad mtime\n", cpathname);
230 fclose(fp);
231 return NULL;
232 }
233 if (verbose)
234 fprintf(stderr, "# %s matches %s\n", cpathname, pathname);
235 return fp;
236}
237
238
239/* Read a code object from a file and check it for validity */
240
241static codeobject *
242read_compiled_module(fp)
243 FILE *fp;
244{
245 object *co;
246
247 co = rd_object(fp);
248 /* Ugly: rd_object() may return NULL with or without error */
249 if (co == NULL || !is_codeobject(co)) {
250 if (!err_occurred())
251 err_setstr(ImportError,
252 "Non-code object in .pyc file");
253 XDECREF(co);
254 return NULL;
255 }
256 return (codeobject *)co;
257}
258
259
260/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000261 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000262
263static object *
264load_compiled_module(name, cpathname, fp)
265 char *name;
266 char *cpathname;
267 FILE *fp;
268{
269 long magic;
270 codeobject *co;
271 object *m;
272
273 magic = rd_long(fp);
274 if (magic != MAGIC) {
275 err_setstr(ImportError, "Bad magic number in .pyc file");
276 return NULL;
277 }
278 (void) rd_long(fp);
279 co = read_compiled_module(fp);
280 if (co == NULL)
281 return NULL;
282 if (verbose)
283 fprintf(stderr, "import %s # precompiled from %s\n",
284 name, cpathname);
Jack Jansen9c96a921995-02-15 22:57:06 +0000285 m = exec_code_module(name, (object *)co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000286 DECREF(co);
287
288 return m;
289}
290
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000291/* Parse a source file and return the corresponding code object */
292
293static codeobject *
294parse_source_module(pathname, fp)
295 char *pathname;
296 FILE *fp;
297{
298 codeobject *co;
299 node *n;
300
301 n = parse_file(fp, pathname, file_input);
302 if (n == NULL)
303 return NULL;
304 co = compile(n, pathname);
305 freetree(n);
306
307 return co;
308}
309
310
311/* Write a compiled module to a file, placing the time of last
312 modification of its source into the header.
313 Errors are ignored, if a write error occurs an attempt is made to
314 remove the file. */
315
316static void
317write_compiled_module(co, cpathname, mtime)
318 codeobject *co;
319 char *cpathname;
320 long mtime;
321{
322 FILE *fp;
323
324 fp = fopen(cpathname, "wb");
325 if (fp == NULL) {
326 if (verbose)
327 fprintf(stderr,
328 "# can't create %s\n", cpathname);
329 return;
330 }
331 wr_long(MAGIC, fp);
332 /* First write a 0 for mtime */
333 wr_long(0L, fp);
334 wr_object((object *)co, fp);
335 if (ferror(fp)) {
336 if (verbose)
337 fprintf(stderr, "# can't write %s\n", cpathname);
338 /* Don't keep partial file */
339 fclose(fp);
340 (void) unlink(cpathname);
341 return;
342 }
343 /* Now write the true mtime */
344 fseek(fp, 4L, 0);
345 wr_long(mtime, fp);
346 fflush(fp);
347 fclose(fp);
348 if (verbose)
349 fprintf(stderr, "# wrote %s\n", cpathname);
350#ifdef macintosh
Guido van Rossumbe1a6e21996-02-21 15:29:20 +0000351 setfiletype(cpathname, 'Pyth', 'PYC ');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000352#endif
353}
354
355
356/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000357 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
358 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000359
360static object *
361load_source_module(name, pathname, fp)
362 char *name;
363 char *pathname;
364 FILE *fp;
365{
366 long mtime;
367 FILE *fpc;
368 char buf[MAXPATHLEN+1];
369 char *cpathname;
370 codeobject *co;
371 object *m;
372
373 mtime = getmtime(pathname);
374 cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1);
375 if (cpathname != NULL &&
376 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
377 co = read_compiled_module(fpc);
378 fclose(fpc);
379 if (co == NULL)
380 return NULL;
381 if (verbose)
382 fprintf(stderr, "import %s # precompiled from %s\n",
383 name, cpathname);
384 }
385 else {
386 co = parse_source_module(pathname, fp);
387 if (co == NULL)
388 return NULL;
389 if (verbose)
390 fprintf(stderr, "import %s # from %s\n",
391 name, pathname);
392 write_compiled_module(co, cpathname, mtime);
393 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000394 m = exec_code_module(name, (object *)co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000395 DECREF(co);
396
397 return m;
398}
399
400
401/* Search the path (default sys.path) for a module. Return the
402 corresponding filedescr struct, and (via return arguments) the
403 pathname and an open file. Return NULL if the module is not found. */
404
405static struct filedescr *
406find_module(name, path, buf, buflen, p_fp)
407 char *name;
408 object *path;
409 /* Output parameters: */
410 char *buf;
411 int buflen;
412 FILE **p_fp;
413{
414 int i, npath, len, namelen;
415 struct filedescr *fdp;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000416 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000417
Guido van Rossuma5a3db71996-04-09 02:39:59 +0000418#ifdef NT
419 if ((fp=PyWin_FindRegisteredModule(name, &fdp, buf, buflen))!=NULL) {
420 *p_fp = fp;
421 return fdp;
422 }
423#endif
424
425
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000426 if (path == NULL)
427 path = sysget("path");
428 if (path == NULL || !is_listobject(path)) {
429 err_setstr(ImportError,
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000430 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000431 return NULL;
432 }
433 npath = getlistsize(path);
434 namelen = strlen(name);
435 for (i = 0; i < npath; i++) {
436 object *v = getlistitem(path, i);
437 if (!is_stringobject(v))
438 continue;
439 len = getstringsize(v);
440 if (len + 2 + namelen + import_maxsuffixsize >= buflen)
441 continue; /* Too long */
442 strcpy(buf, getstringvalue(v));
443 if (strlen(buf) != len)
444 continue; /* v contains '\0' */
Jack Jansen9c96a921995-02-15 22:57:06 +0000445#ifdef macintosh
446 if ( PyMac_FindResourceModule(name, buf) ) {
447 static struct filedescr resfiledescr = { "", "", PY_RESOURCE};
448
449 return &resfiledescr;
450 }
451#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000452 if (len > 0 && buf[len-1] != SEP)
453 buf[len++] = SEP;
Guido van Rossum40f470f1996-05-23 22:51:04 +0000454#ifdef IMPORT_8x3_NAMES
455 /* see if we are searching in directory dos_8x3 */
456 if (len > 7 && !strncmp(buf + len - 8, "dos_8x3", 7)){
457 int j;
458 char ch; /* limit name to eight lower-case characters */
459 for (j = 0; (ch = name[j]) && j < 8; j++)
460 if (isupper(ch))
461 buf[len++] = tolower(ch);
462 else
463 buf[len++] = ch;
464 }
Guido van Rossum0e41c8c1996-06-20 14:18:34 +0000465 else /* Not in dos_8x3, use the full name */
466#else
467 {
Guido van Rossum40f470f1996-05-23 22:51:04 +0000468 strcpy(buf+len, name);
469 len += namelen;
470 }
Guido van Rossum40f470f1996-05-23 22:51:04 +0000471#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000472 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
473 strcpy(buf+len, fdp->suffix);
474 if (verbose > 1)
475 fprintf(stderr, "# trying %s\n", buf);
476 fp = fopen(buf, fdp->mode);
477 if (fp != NULL)
478 break;
479 }
480 if (fp != NULL)
481 break;
482 }
483 if (fp == NULL) {
484 char buf[256];
485 sprintf(buf, "No module named %.200s", name);
486 err_setstr(ImportError, buf);
487 return NULL;
488 }
489
490 *p_fp = fp;
491 return fdp;
492}
493
494
495/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000496 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000497
498static object *
499load_module(name)
500 char *name;
501{
502 char buf[MAXPATHLEN+1];
503 struct filedescr *fdp;
504 FILE *fp = NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000505 object *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000506
507 fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp);
508 if (fdp == NULL)
509 return NULL;
510
511 switch (fdp->type) {
512
513 case PY_SOURCE:
514 m = load_source_module(name, buf, fp);
515 break;
516
517 case PY_COMPILED:
518 m = load_compiled_module(name, buf, fp);
519 break;
520
521 case C_EXTENSION:
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000522 m = load_dynamic_module(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000523 break;
524
Jack Jansen9c96a921995-02-15 22:57:06 +0000525#ifdef macintosh
526 case PY_RESOURCE:
527 m = PyMac_LoadResourceModule(name, buf);
528 break;
529#endif
530
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000531 default:
532 err_setstr(SystemError,
533 "find_module returned unexpected result");
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000534 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000535
536 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000537 if ( fp )
538 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000539
540 return m;
541}
542
543
544/* Initialize a built-in module.
545 Return 1 for succes, 0 if the module is not found, and -1 with
546 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000547
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000548static int
549init_builtin(name)
550 char *name;
551{
552 int i;
553 for (i = 0; inittab[i].name != NULL; i++) {
554 if (strcmp(name, inittab[i].name) == 0) {
Guido van Rossum74e6a111994-08-29 12:54:38 +0000555 if (inittab[i].initfunc == NULL) {
556 err_setstr(ImportError,
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000557 "Cannot re-init internal module");
Guido van Rossum74e6a111994-08-29 12:54:38 +0000558 return -1;
559 }
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +0000560 if (verbose)
561 fprintf(stderr, "import %s # builtin\n",
562 name);
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000563 (*inittab[i].initfunc)();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000564 if (err_occurred())
565 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000566 return 1;
567 }
568 }
569 return 0;
570}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000571
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000572
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000573/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000574
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000575static struct _frozen *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000576find_frozen(name)
577 char *name;
578{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000579 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000580
581 for (p = frozen_modules; ; p++) {
582 if (p->name == NULL)
583 return NULL;
584 if (strcmp(p->name, name) == 0)
585 break;
586 }
587 return p;
588}
589
590static object *
591get_frozen_object(name)
592 char *name;
593{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000594 struct _frozen *p = find_frozen(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000595
596 if (p == NULL) {
597 err_setstr(ImportError, "No such frozen object");
598 return NULL;
599 }
600 return rds_object(p->code, p->size);
601}
602
603/* Initialize a frozen module.
604 Return 1 for succes, 0 if the module is not found, and -1 with
605 an exception set if the initialization failed.
606 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +0000607
608int
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000609init_frozen(name)
610 char *name;
611{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000612 struct _frozen *p = find_frozen(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000613 object *co;
614 object *m;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000615
616 if (p == NULL)
617 return 0;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000618 if (verbose)
619 fprintf(stderr, "import %s # frozen\n", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000620 co = rds_object(p->code, p->size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000621 if (co == NULL)
622 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000623 if (!is_codeobject(co)) {
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000624 DECREF(co);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000625 err_setstr(TypeError, "frozen object is not a code object");
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000626 return -1;
627 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000628 m = exec_code_module(name, co);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000629 DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000630 if (m == NULL)
631 return -1;
632 DECREF(m);
633 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000634}
Guido van Rossum74e6a111994-08-29 12:54:38 +0000635
636
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000637/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000638 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +0000639
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000640object *
641import_module(name)
642 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000643{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000644 object *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000645
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000646 if (import_modules == NULL) {
647 err_setstr(SystemError, "sys.modules has been deleted");
648 return NULL;
649 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000650 if ((m = dictlookup(import_modules, name)) != NULL) {
651 INCREF(m);
652 }
653 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000654 int i;
655 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
656 if (i < 0)
657 return NULL;
658 if ((m = dictlookup(import_modules, name)) == NULL) {
659 if (err_occurred() == NULL)
660 err_setstr(SystemError,
661 "built-in module not initialized properly");
662 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000663 else
664 INCREF(m);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000665 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000666 else
667 m = load_module(name);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000668 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000669
670 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000671}
672
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000673
674/* Re-import a module of any kind and return its module object, WITH
675 INCREMENTED REFERENCE COUNT */
676
677object *
678reload_module(m)
679 object *m;
680{
681 char *name;
682 int i;
683
684 if (m == NULL || !is_moduleobject(m)) {
685 err_setstr(TypeError, "reload() argument must be module");
686 return NULL;
687 }
688 name = getmodulename(m);
689 if (name == NULL)
690 return NULL;
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000691 if (import_modules == NULL) {
692 err_setstr(SystemError, "sys.modules has been deleted");
693 return NULL;
694 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000695 if (m != dictlookup(import_modules, name)) {
696 err_setstr(ImportError, "reload() module not in sys.modules");
697 return NULL;
698 }
699 /* Check for built-in and frozen modules */
700 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
701 if (i < 0)
702 return NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000703 INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000704 }
705 else
706 m = load_module(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000707 return m;
708}
709
710
711/* Module 'imp' provides Python access to the primitives used for
712 importing modules.
713*/
714
715static object *
716imp_get_magic(self, args)
717 object *self;
718 object *args;
719{
720 char buf[4];
721
722 if (!newgetargs(args, ""))
723 return NULL;
724 buf[0] = (MAGIC >> 0) & 0xff;
725 buf[1] = (MAGIC >> 8) & 0xff;
Guido van Rossum90f0e071995-01-30 12:53:06 +0000726 buf[2] = (MAGIC >> 16) & 0xff;
727 buf[3] = (MAGIC >> 24) & 0xff;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000728
729 return newsizedstringobject(buf, 4);
730}
731
732static object *
733imp_get_suffixes(self, args)
734 object *self;
735 object *args;
736{
737 object *list;
738 struct filedescr *fdp;
739
740 if (!newgetargs(args, ""))
741 return NULL;
742 list = newlistobject(0);
743 if (list == NULL)
744 return NULL;
745 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
746 object *item = mkvalue("ssi",
747 fdp->suffix, fdp->mode, fdp->type);
748 if (item == NULL) {
749 DECREF(list);
750 return NULL;
751 }
752 if (addlistitem(list, item) < 0) {
753 DECREF(list);
754 DECREF(item);
755 return NULL;
756 }
757 DECREF(item);
758 }
759 return list;
760}
761
762static object *
763imp_find_module(self, args)
764 object *self;
765 object *args;
766{
767 extern int fclose PROTO((FILE *));
768 char *name;
769 object *path = NULL;
770 object *fob, *ret;
771 struct filedescr *fdp;
772 char pathname[MAXPATHLEN+1];
773 FILE *fp;
774 if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
775 return NULL;
776 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
777 if (fdp == NULL)
778 return NULL;
779 fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
780 if (fob == NULL) {
781 fclose(fp);
782 return NULL;
783 }
784 ret = mkvalue("Os(ssi)",
785 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
786 DECREF(fob);
787 return ret;
788}
789
790static object *
791imp_init_builtin(self, args)
792 object *self;
793 object *args;
794{
795 char *name;
796 int ret;
797 object *m;
798 if (!newgetargs(args, "s", &name))
799 return NULL;
800 ret = init_builtin(name);
801 if (ret < 0)
802 return NULL;
803 if (ret == 0) {
804 INCREF(None);
805 return None;
806 }
807 m = add_module(name);
808 XINCREF(m);
809 return m;
810}
811
812static object *
813imp_init_frozen(self, args)
814 object *self;
815 object *args;
816{
817 char *name;
818 int ret;
819 object *m;
820 if (!newgetargs(args, "s", &name))
821 return NULL;
822 ret = init_frozen(name);
823 if (ret < 0)
824 return NULL;
825 if (ret == 0) {
826 INCREF(None);
827 return None;
828 }
829 m = add_module(name);
830 XINCREF(m);
831 return m;
832}
833
834static object *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000835imp_get_frozen_object(self, args)
836 object *self;
837 object *args;
838{
839 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +0000840
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000841 if (!newgetargs(args, "s", &name))
842 return NULL;
843 return get_frozen_object(name);
844}
845
846static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000847imp_is_builtin(self, args)
848 object *self;
849 object *args;
850{
851 int i;
852 char *name;
853 if (!newgetargs(args, "s", &name))
854 return NULL;
855 for (i = 0; inittab[i].name != NULL; i++) {
856 if (strcmp(name, inittab[i].name) == 0) {
857 if (inittab[i].initfunc == NULL)
858 return newintobject(-1);
859 else
860 return newintobject(1);
861 }
862 }
863 return newintobject(0);
864}
865
866static object *
867imp_is_frozen(self, args)
868 object *self;
869 object *args;
870{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000871 struct _frozen *p;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000872 char *name;
873 if (!newgetargs(args, "s", &name))
874 return NULL;
875 for (p = frozen_modules; ; p++) {
876 if (p->name == NULL)
877 break;
878 if (strcmp(p->name, name) == 0)
879 return newintobject(1);
880 }
881 return newintobject(0);
882}
883
884static FILE *
885get_file(pathname, fob, mode)
886 char *pathname;
887 object *fob;
888 char *mode;
889{
890 FILE *fp;
891 if (fob == NULL) {
892 fp = fopen(pathname, mode);
893 if (fp == NULL)
894 err_errno(IOError);
895 }
896 else {
897 fp = getfilefile(fob);
898 if (fp == NULL)
899 err_setstr(ValueError, "bad/closed file object");
900 }
901 return fp;
902}
903
904static object *
905imp_load_compiled(self, args)
906 object *self;
907 object *args;
908{
909 char *name;
910 char *pathname;
911 object *fob = NULL;
912 object *m;
913 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000914 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000915 return NULL;
916 fp = get_file(pathname, fob, "rb");
917 if (fp == NULL)
918 return NULL;
919 m = load_compiled_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000920 return m;
921}
922
923static object *
924imp_load_dynamic(self, args)
925 object *self;
926 object *args;
927{
928 char *name;
929 char *pathname;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000930 object *fob = NULL;
931 object *m;
932 FILE *fp = NULL;
933 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000934 return NULL;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000935 if (fob)
936 fp = get_file(pathname, fob, "r");
937 m = load_dynamic_module(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +0000938 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000939}
940
941static object *
942imp_load_source(self, args)
943 object *self;
944 object *args;
945{
946 char *name;
947 char *pathname;
948 object *fob = NULL;
949 object *m;
950 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000951 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000952 return NULL;
953 fp = get_file(pathname, fob, "r");
954 if (fp == NULL)
955 return NULL;
956 m = load_source_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000957 return m;
958}
959
Jack Jansen9c96a921995-02-15 22:57:06 +0000960#ifdef macintosh
961static object *
962imp_load_resource(self, args)
963 object *self;
964 object *args;
965{
966 char *name;
967 char *pathname;
968 object *m;
969
970 if (!newgetargs(args, "ss", &name, &pathname))
971 return NULL;
972 m = PyMac_LoadResourceModule(name, pathname);
973 return m;
974}
975#endif /* macintosh */
976
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000977static object *
978imp_new_module(self, args)
979 object *self;
980 object *args;
981{
982 char *name;
983 if (!newgetargs(args, "s", &name))
984 return NULL;
985 return newmoduleobject(name);
986}
987
988static struct methodlist imp_methods[] = {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000989 {"get_frozen_object", imp_get_frozen_object, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000990 {"get_magic", imp_get_magic, 1},
991 {"get_suffixes", imp_get_suffixes, 1},
992 {"find_module", imp_find_module, 1},
993 {"init_builtin", imp_init_builtin, 1},
994 {"init_frozen", imp_init_frozen, 1},
995 {"is_builtin", imp_is_builtin, 1},
996 {"is_frozen", imp_is_frozen, 1},
997 {"load_compiled", imp_load_compiled, 1},
998 {"load_dynamic", imp_load_dynamic, 1},
999 {"load_source", imp_load_source, 1},
1000 {"new_module", imp_new_module, 1},
Jack Jansen9c96a921995-02-15 22:57:06 +00001001#ifdef macintosh
1002 {"load_resource", imp_load_resource, 1},
1003#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001004 {NULL, NULL} /* sentinel */
1005};
1006
1007void
1008initimp()
1009{
1010 object *m, *d, *v;
1011
1012 m = initmodule("imp", imp_methods);
1013 d = getmoduledict(m);
1014
1015 v = newintobject(SEARCH_ERROR);
1016 dictinsert(d, "SEARCH_ERROR", v);
1017 XDECREF(v);
1018
1019 v = newintobject(PY_SOURCE);
1020 dictinsert(d, "PY_SOURCE", v);
1021 XDECREF(v);
1022
1023 v = newintobject(PY_COMPILED);
1024 dictinsert(d, "PY_COMPILED", v);
1025 XDECREF(v);
1026
1027 v = newintobject(C_EXTENSION);
1028 dictinsert(d, "C_EXTENSION", v);
1029 XDECREF(v);
1030
Jack Jansenae12e191995-06-18 20:06:44 +00001031#ifdef macintosh
1032 v = newintobject(PY_RESOURCE);
1033 dictinsert(d, "PY_RESOURCE", v);
1034 XDECREF(v);
1035#endif
1036
1037
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001038 if (err_occurred())
1039 fatal("imp module initialization failed");
1040}