blob: a3803a20f9033808191fcb9ea4303a5ea0979394 [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 }
465 else{ /* Not in dos_8x3, use the full name */
466 strcpy(buf+len, name);
467 len += namelen;
468 }
469#else
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000470 strcpy(buf+len, name);
471 len += namelen;
Guido van Rossum40f470f1996-05-23 22:51:04 +0000472#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000473 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
474 strcpy(buf+len, fdp->suffix);
475 if (verbose > 1)
476 fprintf(stderr, "# trying %s\n", buf);
477 fp = fopen(buf, fdp->mode);
478 if (fp != NULL)
479 break;
480 }
481 if (fp != NULL)
482 break;
483 }
484 if (fp == NULL) {
485 char buf[256];
486 sprintf(buf, "No module named %.200s", name);
487 err_setstr(ImportError, buf);
488 return NULL;
489 }
490
491 *p_fp = fp;
492 return fdp;
493}
494
495
496/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000497 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000498
499static object *
500load_module(name)
501 char *name;
502{
503 char buf[MAXPATHLEN+1];
504 struct filedescr *fdp;
505 FILE *fp = NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000506 object *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000507
508 fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp);
509 if (fdp == NULL)
510 return NULL;
511
512 switch (fdp->type) {
513
514 case PY_SOURCE:
515 m = load_source_module(name, buf, fp);
516 break;
517
518 case PY_COMPILED:
519 m = load_compiled_module(name, buf, fp);
520 break;
521
522 case C_EXTENSION:
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000523 m = load_dynamic_module(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000524 break;
525
Jack Jansen9c96a921995-02-15 22:57:06 +0000526#ifdef macintosh
527 case PY_RESOURCE:
528 m = PyMac_LoadResourceModule(name, buf);
529 break;
530#endif
531
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000532 default:
533 err_setstr(SystemError,
534 "find_module returned unexpected result");
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000535 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000536
537 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000538 if ( fp )
539 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000540
541 return m;
542}
543
544
545/* Initialize a built-in module.
546 Return 1 for succes, 0 if the module is not found, and -1 with
547 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000548
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000549static int
550init_builtin(name)
551 char *name;
552{
553 int i;
554 for (i = 0; inittab[i].name != NULL; i++) {
555 if (strcmp(name, inittab[i].name) == 0) {
Guido van Rossum74e6a111994-08-29 12:54:38 +0000556 if (inittab[i].initfunc == NULL) {
557 err_setstr(ImportError,
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000558 "Cannot re-init internal module");
Guido van Rossum74e6a111994-08-29 12:54:38 +0000559 return -1;
560 }
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +0000561 if (verbose)
562 fprintf(stderr, "import %s # builtin\n",
563 name);
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000564 (*inittab[i].initfunc)();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000565 if (err_occurred())
566 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000567 return 1;
568 }
569 }
570 return 0;
571}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000572
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000573
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000574/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000575
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000576static struct _frozen *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000577find_frozen(name)
578 char *name;
579{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000580 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000581
582 for (p = frozen_modules; ; p++) {
583 if (p->name == NULL)
584 return NULL;
585 if (strcmp(p->name, name) == 0)
586 break;
587 }
588 return p;
589}
590
591static object *
592get_frozen_object(name)
593 char *name;
594{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000595 struct _frozen *p = find_frozen(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000596
597 if (p == NULL) {
598 err_setstr(ImportError, "No such frozen object");
599 return NULL;
600 }
601 return rds_object(p->code, p->size);
602}
603
604/* Initialize a frozen module.
605 Return 1 for succes, 0 if the module is not found, and -1 with
606 an exception set if the initialization failed.
607 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +0000608
609int
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000610init_frozen(name)
611 char *name;
612{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000613 struct _frozen *p = find_frozen(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000614 object *co;
615 object *m;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000616
617 if (p == NULL)
618 return 0;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000619 if (verbose)
620 fprintf(stderr, "import %s # frozen\n", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000621 co = rds_object(p->code, p->size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000622 if (co == NULL)
623 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000624 if (!is_codeobject(co)) {
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000625 DECREF(co);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000626 err_setstr(TypeError, "frozen object is not a code object");
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000627 return -1;
628 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000629 m = exec_code_module(name, co);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000630 DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000631 if (m == NULL)
632 return -1;
633 DECREF(m);
634 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000635}
Guido van Rossum74e6a111994-08-29 12:54:38 +0000636
637
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000638/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000639 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +0000640
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000641object *
642import_module(name)
643 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000644{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000645 object *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000646
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000647 if (import_modules == NULL) {
648 err_setstr(SystemError, "sys.modules has been deleted");
649 return NULL;
650 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000651 if ((m = dictlookup(import_modules, name)) != NULL) {
652 INCREF(m);
653 }
654 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000655 int i;
656 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
657 if (i < 0)
658 return NULL;
659 if ((m = dictlookup(import_modules, name)) == NULL) {
660 if (err_occurred() == NULL)
661 err_setstr(SystemError,
662 "built-in module not initialized properly");
663 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000664 else
665 INCREF(m);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000666 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000667 else
668 m = load_module(name);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000669 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000670
671 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000672}
673
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000674
675/* Re-import a module of any kind and return its module object, WITH
676 INCREMENTED REFERENCE COUNT */
677
678object *
679reload_module(m)
680 object *m;
681{
682 char *name;
683 int i;
684
685 if (m == NULL || !is_moduleobject(m)) {
686 err_setstr(TypeError, "reload() argument must be module");
687 return NULL;
688 }
689 name = getmodulename(m);
690 if (name == NULL)
691 return NULL;
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000692 if (import_modules == NULL) {
693 err_setstr(SystemError, "sys.modules has been deleted");
694 return NULL;
695 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000696 if (m != dictlookup(import_modules, name)) {
697 err_setstr(ImportError, "reload() module not in sys.modules");
698 return NULL;
699 }
700 /* Check for built-in and frozen modules */
701 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
702 if (i < 0)
703 return NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000704 INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000705 }
706 else
707 m = load_module(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000708 return m;
709}
710
711
712/* Module 'imp' provides Python access to the primitives used for
713 importing modules.
714*/
715
716static object *
717imp_get_magic(self, args)
718 object *self;
719 object *args;
720{
721 char buf[4];
722
723 if (!newgetargs(args, ""))
724 return NULL;
725 buf[0] = (MAGIC >> 0) & 0xff;
726 buf[1] = (MAGIC >> 8) & 0xff;
Guido van Rossum90f0e071995-01-30 12:53:06 +0000727 buf[2] = (MAGIC >> 16) & 0xff;
728 buf[3] = (MAGIC >> 24) & 0xff;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000729
730 return newsizedstringobject(buf, 4);
731}
732
733static object *
734imp_get_suffixes(self, args)
735 object *self;
736 object *args;
737{
738 object *list;
739 struct filedescr *fdp;
740
741 if (!newgetargs(args, ""))
742 return NULL;
743 list = newlistobject(0);
744 if (list == NULL)
745 return NULL;
746 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
747 object *item = mkvalue("ssi",
748 fdp->suffix, fdp->mode, fdp->type);
749 if (item == NULL) {
750 DECREF(list);
751 return NULL;
752 }
753 if (addlistitem(list, item) < 0) {
754 DECREF(list);
755 DECREF(item);
756 return NULL;
757 }
758 DECREF(item);
759 }
760 return list;
761}
762
763static object *
764imp_find_module(self, args)
765 object *self;
766 object *args;
767{
768 extern int fclose PROTO((FILE *));
769 char *name;
770 object *path = NULL;
771 object *fob, *ret;
772 struct filedescr *fdp;
773 char pathname[MAXPATHLEN+1];
774 FILE *fp;
775 if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
776 return NULL;
777 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
778 if (fdp == NULL)
779 return NULL;
780 fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
781 if (fob == NULL) {
782 fclose(fp);
783 return NULL;
784 }
785 ret = mkvalue("Os(ssi)",
786 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
787 DECREF(fob);
788 return ret;
789}
790
791static object *
792imp_init_builtin(self, args)
793 object *self;
794 object *args;
795{
796 char *name;
797 int ret;
798 object *m;
799 if (!newgetargs(args, "s", &name))
800 return NULL;
801 ret = init_builtin(name);
802 if (ret < 0)
803 return NULL;
804 if (ret == 0) {
805 INCREF(None);
806 return None;
807 }
808 m = add_module(name);
809 XINCREF(m);
810 return m;
811}
812
813static object *
814imp_init_frozen(self, args)
815 object *self;
816 object *args;
817{
818 char *name;
819 int ret;
820 object *m;
821 if (!newgetargs(args, "s", &name))
822 return NULL;
823 ret = init_frozen(name);
824 if (ret < 0)
825 return NULL;
826 if (ret == 0) {
827 INCREF(None);
828 return None;
829 }
830 m = add_module(name);
831 XINCREF(m);
832 return m;
833}
834
835static object *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000836imp_get_frozen_object(self, args)
837 object *self;
838 object *args;
839{
840 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +0000841
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000842 if (!newgetargs(args, "s", &name))
843 return NULL;
844 return get_frozen_object(name);
845}
846
847static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000848imp_is_builtin(self, args)
849 object *self;
850 object *args;
851{
852 int i;
853 char *name;
854 if (!newgetargs(args, "s", &name))
855 return NULL;
856 for (i = 0; inittab[i].name != NULL; i++) {
857 if (strcmp(name, inittab[i].name) == 0) {
858 if (inittab[i].initfunc == NULL)
859 return newintobject(-1);
860 else
861 return newintobject(1);
862 }
863 }
864 return newintobject(0);
865}
866
867static object *
868imp_is_frozen(self, args)
869 object *self;
870 object *args;
871{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000872 struct _frozen *p;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000873 char *name;
874 if (!newgetargs(args, "s", &name))
875 return NULL;
876 for (p = frozen_modules; ; p++) {
877 if (p->name == NULL)
878 break;
879 if (strcmp(p->name, name) == 0)
880 return newintobject(1);
881 }
882 return newintobject(0);
883}
884
885static FILE *
886get_file(pathname, fob, mode)
887 char *pathname;
888 object *fob;
889 char *mode;
890{
891 FILE *fp;
892 if (fob == NULL) {
893 fp = fopen(pathname, mode);
894 if (fp == NULL)
895 err_errno(IOError);
896 }
897 else {
898 fp = getfilefile(fob);
899 if (fp == NULL)
900 err_setstr(ValueError, "bad/closed file object");
901 }
902 return fp;
903}
904
905static object *
906imp_load_compiled(self, args)
907 object *self;
908 object *args;
909{
910 char *name;
911 char *pathname;
912 object *fob = NULL;
913 object *m;
914 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000915 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000916 return NULL;
917 fp = get_file(pathname, fob, "rb");
918 if (fp == NULL)
919 return NULL;
920 m = load_compiled_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000921 return m;
922}
923
924static object *
925imp_load_dynamic(self, args)
926 object *self;
927 object *args;
928{
929 char *name;
930 char *pathname;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000931 object *fob = NULL;
932 object *m;
933 FILE *fp = NULL;
934 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000935 return NULL;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000936 if (fob)
937 fp = get_file(pathname, fob, "r");
938 m = load_dynamic_module(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +0000939 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000940}
941
942static object *
943imp_load_source(self, args)
944 object *self;
945 object *args;
946{
947 char *name;
948 char *pathname;
949 object *fob = NULL;
950 object *m;
951 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000952 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000953 return NULL;
954 fp = get_file(pathname, fob, "r");
955 if (fp == NULL)
956 return NULL;
957 m = load_source_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000958 return m;
959}
960
Jack Jansen9c96a921995-02-15 22:57:06 +0000961#ifdef macintosh
962static object *
963imp_load_resource(self, args)
964 object *self;
965 object *args;
966{
967 char *name;
968 char *pathname;
969 object *m;
970
971 if (!newgetargs(args, "ss", &name, &pathname))
972 return NULL;
973 m = PyMac_LoadResourceModule(name, pathname);
974 return m;
975}
976#endif /* macintosh */
977
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000978static object *
979imp_new_module(self, args)
980 object *self;
981 object *args;
982{
983 char *name;
984 if (!newgetargs(args, "s", &name))
985 return NULL;
986 return newmoduleobject(name);
987}
988
989static struct methodlist imp_methods[] = {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000990 {"get_frozen_object", imp_get_frozen_object, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000991 {"get_magic", imp_get_magic, 1},
992 {"get_suffixes", imp_get_suffixes, 1},
993 {"find_module", imp_find_module, 1},
994 {"init_builtin", imp_init_builtin, 1},
995 {"init_frozen", imp_init_frozen, 1},
996 {"is_builtin", imp_is_builtin, 1},
997 {"is_frozen", imp_is_frozen, 1},
998 {"load_compiled", imp_load_compiled, 1},
999 {"load_dynamic", imp_load_dynamic, 1},
1000 {"load_source", imp_load_source, 1},
1001 {"new_module", imp_new_module, 1},
Jack Jansen9c96a921995-02-15 22:57:06 +00001002#ifdef macintosh
1003 {"load_resource", imp_load_resource, 1},
1004#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001005 {NULL, NULL} /* sentinel */
1006};
1007
1008void
1009initimp()
1010{
1011 object *m, *d, *v;
1012
1013 m = initmodule("imp", imp_methods);
1014 d = getmoduledict(m);
1015
1016 v = newintobject(SEARCH_ERROR);
1017 dictinsert(d, "SEARCH_ERROR", v);
1018 XDECREF(v);
1019
1020 v = newintobject(PY_SOURCE);
1021 dictinsert(d, "PY_SOURCE", v);
1022 XDECREF(v);
1023
1024 v = newintobject(PY_COMPILED);
1025 dictinsert(d, "PY_COMPILED", v);
1026 XDECREF(v);
1027
1028 v = newintobject(C_EXTENSION);
1029 dictinsert(d, "C_EXTENSION", v);
1030 XDECREF(v);
1031
Jack Jansenae12e191995-06-18 20:06:44 +00001032#ifdef macintosh
1033 v = newintobject(PY_RESOURCE);
1034 dictinsert(d, "PY_RESOURCE", v);
1035 XDECREF(v);
1036#endif
1037
1038
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001039 if (err_occurred())
1040 fatal("imp module initialization failed");
1041}