blob: 53a7ba493049aece307d3b1315ec5f0a325cd0f3 [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 Rossumf56e3db1993-04-01 20:59:32 +0000576extern struct frozen {
577 char *name;
578 char *code;
579 int size;
580} frozen_modules[];
581
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000582static struct frozen *
583find_frozen(name)
584 char *name;
585{
586 struct frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000587
588 for (p = frozen_modules; ; p++) {
589 if (p->name == NULL)
590 return NULL;
591 if (strcmp(p->name, name) == 0)
592 break;
593 }
594 return p;
595}
596
597static object *
598get_frozen_object(name)
599 char *name;
600{
601 struct frozen *p = find_frozen(name);
602
603 if (p == NULL) {
604 err_setstr(ImportError, "No such frozen object");
605 return NULL;
606 }
607 return rds_object(p->code, p->size);
608}
609
610/* Initialize a frozen module.
611 Return 1 for succes, 0 if the module is not found, and -1 with
612 an exception set if the initialization failed.
613 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +0000614
615int
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000616init_frozen(name)
617 char *name;
618{
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000619 struct frozen *p = find_frozen(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000620 object *co;
621 object *m;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000622
623 if (p == NULL)
624 return 0;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000625 if (verbose)
626 fprintf(stderr, "import %s # frozen\n", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000627 co = rds_object(p->code, p->size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000628 if (co == NULL)
629 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000630 if (!is_codeobject(co)) {
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000631 DECREF(co);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000632 err_setstr(TypeError, "frozen object is not a code object");
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000633 return -1;
634 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000635 m = exec_code_module(name, co);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000636 DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000637 if (m == NULL)
638 return -1;
639 DECREF(m);
640 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000641}
Guido van Rossum74e6a111994-08-29 12:54:38 +0000642
643
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000644/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000645 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +0000646
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000647object *
648import_module(name)
649 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000650{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000651 object *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000652
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000653 if (import_modules == NULL) {
654 err_setstr(SystemError, "sys.modules has been deleted");
655 return NULL;
656 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000657 if ((m = dictlookup(import_modules, name)) != NULL) {
658 INCREF(m);
659 }
660 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000661 int i;
662 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
663 if (i < 0)
664 return NULL;
665 if ((m = dictlookup(import_modules, name)) == NULL) {
666 if (err_occurred() == NULL)
667 err_setstr(SystemError,
668 "built-in module not initialized properly");
669 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000670 else
671 INCREF(m);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000672 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000673 else
674 m = load_module(name);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000675 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000676
677 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000678}
679
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000680
681/* Re-import a module of any kind and return its module object, WITH
682 INCREMENTED REFERENCE COUNT */
683
684object *
685reload_module(m)
686 object *m;
687{
688 char *name;
689 int i;
690
691 if (m == NULL || !is_moduleobject(m)) {
692 err_setstr(TypeError, "reload() argument must be module");
693 return NULL;
694 }
695 name = getmodulename(m);
696 if (name == NULL)
697 return NULL;
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000698 if (import_modules == NULL) {
699 err_setstr(SystemError, "sys.modules has been deleted");
700 return NULL;
701 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000702 if (m != dictlookup(import_modules, name)) {
703 err_setstr(ImportError, "reload() module not in sys.modules");
704 return NULL;
705 }
706 /* Check for built-in and frozen modules */
707 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
708 if (i < 0)
709 return NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000710 INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000711 }
712 else
713 m = load_module(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000714 return m;
715}
716
717
718/* Module 'imp' provides Python access to the primitives used for
719 importing modules.
720*/
721
722static object *
723imp_get_magic(self, args)
724 object *self;
725 object *args;
726{
727 char buf[4];
728
729 if (!newgetargs(args, ""))
730 return NULL;
731 buf[0] = (MAGIC >> 0) & 0xff;
732 buf[1] = (MAGIC >> 8) & 0xff;
Guido van Rossum90f0e071995-01-30 12:53:06 +0000733 buf[2] = (MAGIC >> 16) & 0xff;
734 buf[3] = (MAGIC >> 24) & 0xff;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000735
736 return newsizedstringobject(buf, 4);
737}
738
739static object *
740imp_get_suffixes(self, args)
741 object *self;
742 object *args;
743{
744 object *list;
745 struct filedescr *fdp;
746
747 if (!newgetargs(args, ""))
748 return NULL;
749 list = newlistobject(0);
750 if (list == NULL)
751 return NULL;
752 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
753 object *item = mkvalue("ssi",
754 fdp->suffix, fdp->mode, fdp->type);
755 if (item == NULL) {
756 DECREF(list);
757 return NULL;
758 }
759 if (addlistitem(list, item) < 0) {
760 DECREF(list);
761 DECREF(item);
762 return NULL;
763 }
764 DECREF(item);
765 }
766 return list;
767}
768
769static object *
770imp_find_module(self, args)
771 object *self;
772 object *args;
773{
774 extern int fclose PROTO((FILE *));
775 char *name;
776 object *path = NULL;
777 object *fob, *ret;
778 struct filedescr *fdp;
779 char pathname[MAXPATHLEN+1];
780 FILE *fp;
781 if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
782 return NULL;
783 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
784 if (fdp == NULL)
785 return NULL;
786 fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
787 if (fob == NULL) {
788 fclose(fp);
789 return NULL;
790 }
791 ret = mkvalue("Os(ssi)",
792 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
793 DECREF(fob);
794 return ret;
795}
796
797static object *
798imp_init_builtin(self, args)
799 object *self;
800 object *args;
801{
802 char *name;
803 int ret;
804 object *m;
805 if (!newgetargs(args, "s", &name))
806 return NULL;
807 ret = init_builtin(name);
808 if (ret < 0)
809 return NULL;
810 if (ret == 0) {
811 INCREF(None);
812 return None;
813 }
814 m = add_module(name);
815 XINCREF(m);
816 return m;
817}
818
819static object *
820imp_init_frozen(self, args)
821 object *self;
822 object *args;
823{
824 char *name;
825 int ret;
826 object *m;
827 if (!newgetargs(args, "s", &name))
828 return NULL;
829 ret = init_frozen(name);
830 if (ret < 0)
831 return NULL;
832 if (ret == 0) {
833 INCREF(None);
834 return None;
835 }
836 m = add_module(name);
837 XINCREF(m);
838 return m;
839}
840
841static object *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000842imp_get_frozen_object(self, args)
843 object *self;
844 object *args;
845{
846 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +0000847
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000848 if (!newgetargs(args, "s", &name))
849 return NULL;
850 return get_frozen_object(name);
851}
852
853static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000854imp_is_builtin(self, args)
855 object *self;
856 object *args;
857{
858 int i;
859 char *name;
860 if (!newgetargs(args, "s", &name))
861 return NULL;
862 for (i = 0; inittab[i].name != NULL; i++) {
863 if (strcmp(name, inittab[i].name) == 0) {
864 if (inittab[i].initfunc == NULL)
865 return newintobject(-1);
866 else
867 return newintobject(1);
868 }
869 }
870 return newintobject(0);
871}
872
873static object *
874imp_is_frozen(self, args)
875 object *self;
876 object *args;
877{
878 struct frozen *p;
879 char *name;
880 if (!newgetargs(args, "s", &name))
881 return NULL;
882 for (p = frozen_modules; ; p++) {
883 if (p->name == NULL)
884 break;
885 if (strcmp(p->name, name) == 0)
886 return newintobject(1);
887 }
888 return newintobject(0);
889}
890
891static FILE *
892get_file(pathname, fob, mode)
893 char *pathname;
894 object *fob;
895 char *mode;
896{
897 FILE *fp;
898 if (fob == NULL) {
899 fp = fopen(pathname, mode);
900 if (fp == NULL)
901 err_errno(IOError);
902 }
903 else {
904 fp = getfilefile(fob);
905 if (fp == NULL)
906 err_setstr(ValueError, "bad/closed file object");
907 }
908 return fp;
909}
910
911static object *
912imp_load_compiled(self, args)
913 object *self;
914 object *args;
915{
916 char *name;
917 char *pathname;
918 object *fob = NULL;
919 object *m;
920 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000921 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000922 return NULL;
923 fp = get_file(pathname, fob, "rb");
924 if (fp == NULL)
925 return NULL;
926 m = load_compiled_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000927 return m;
928}
929
930static object *
931imp_load_dynamic(self, args)
932 object *self;
933 object *args;
934{
935 char *name;
936 char *pathname;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000937 object *fob = NULL;
938 object *m;
939 FILE *fp = NULL;
940 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000941 return NULL;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000942 if (fob)
943 fp = get_file(pathname, fob, "r");
944 m = load_dynamic_module(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +0000945 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000946}
947
948static object *
949imp_load_source(self, args)
950 object *self;
951 object *args;
952{
953 char *name;
954 char *pathname;
955 object *fob = NULL;
956 object *m;
957 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000958 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000959 return NULL;
960 fp = get_file(pathname, fob, "r");
961 if (fp == NULL)
962 return NULL;
963 m = load_source_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000964 return m;
965}
966
Jack Jansen9c96a921995-02-15 22:57:06 +0000967#ifdef macintosh
968static object *
969imp_load_resource(self, args)
970 object *self;
971 object *args;
972{
973 char *name;
974 char *pathname;
975 object *m;
976
977 if (!newgetargs(args, "ss", &name, &pathname))
978 return NULL;
979 m = PyMac_LoadResourceModule(name, pathname);
980 return m;
981}
982#endif /* macintosh */
983
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000984static object *
985imp_new_module(self, args)
986 object *self;
987 object *args;
988{
989 char *name;
990 if (!newgetargs(args, "s", &name))
991 return NULL;
992 return newmoduleobject(name);
993}
994
995static struct methodlist imp_methods[] = {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000996 {"get_frozen_object", imp_get_frozen_object, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000997 {"get_magic", imp_get_magic, 1},
998 {"get_suffixes", imp_get_suffixes, 1},
999 {"find_module", imp_find_module, 1},
1000 {"init_builtin", imp_init_builtin, 1},
1001 {"init_frozen", imp_init_frozen, 1},
1002 {"is_builtin", imp_is_builtin, 1},
1003 {"is_frozen", imp_is_frozen, 1},
1004 {"load_compiled", imp_load_compiled, 1},
1005 {"load_dynamic", imp_load_dynamic, 1},
1006 {"load_source", imp_load_source, 1},
1007 {"new_module", imp_new_module, 1},
Jack Jansen9c96a921995-02-15 22:57:06 +00001008#ifdef macintosh
1009 {"load_resource", imp_load_resource, 1},
1010#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001011 {NULL, NULL} /* sentinel */
1012};
1013
1014void
1015initimp()
1016{
1017 object *m, *d, *v;
1018
1019 m = initmodule("imp", imp_methods);
1020 d = getmoduledict(m);
1021
1022 v = newintobject(SEARCH_ERROR);
1023 dictinsert(d, "SEARCH_ERROR", v);
1024 XDECREF(v);
1025
1026 v = newintobject(PY_SOURCE);
1027 dictinsert(d, "PY_SOURCE", v);
1028 XDECREF(v);
1029
1030 v = newintobject(PY_COMPILED);
1031 dictinsert(d, "PY_COMPILED", v);
1032 XDECREF(v);
1033
1034 v = newintobject(C_EXTENSION);
1035 dictinsert(d, "C_EXTENSION", v);
1036 XDECREF(v);
1037
Jack Jansenae12e191995-06-18 20:06:44 +00001038#ifdef macintosh
1039 v = newintobject(PY_RESOURCE);
1040 dictinsert(d, "PY_RESOURCE", v);
1041 XDECREF(v);
1042#endif
1043
1044
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001045 if (err_occurred())
1046 fatal("imp module initialization failed");
1047}