blob: f340add077a1d4df194efcacf7b201fd3ee6dbc1 [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 Rossum8861b741996-07-30 16:49:37 +000058#define MAGIC (5892 | ((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 Rossumac279101996-08-22 23:10:58 +0000418#ifdef MS_COREDLL
Guido van Rossuma5a3db71996-04-09 02:39:59 +0000419 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 */
Guido van Rossum6f489d91996-06-28 20:15:15 +0000466#endif
Guido van Rossum0e41c8c1996-06-20 14:18:34 +0000467 {
Guido van Rossum40f470f1996-05-23 22:51:04 +0000468 strcpy(buf+len, name);
469 len += namelen;
470 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000471 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
472 strcpy(buf+len, fdp->suffix);
473 if (verbose > 1)
474 fprintf(stderr, "# trying %s\n", buf);
475 fp = fopen(buf, fdp->mode);
476 if (fp != NULL)
477 break;
478 }
479 if (fp != NULL)
480 break;
481 }
482 if (fp == NULL) {
483 char buf[256];
484 sprintf(buf, "No module named %.200s", name);
485 err_setstr(ImportError, buf);
486 return NULL;
487 }
488
489 *p_fp = fp;
490 return fdp;
491}
492
493
494/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000495 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000496
497static object *
498load_module(name)
499 char *name;
500{
501 char buf[MAXPATHLEN+1];
502 struct filedescr *fdp;
503 FILE *fp = NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000504 object *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000505
506 fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp);
507 if (fdp == NULL)
508 return NULL;
509
510 switch (fdp->type) {
511
512 case PY_SOURCE:
513 m = load_source_module(name, buf, fp);
514 break;
515
516 case PY_COMPILED:
517 m = load_compiled_module(name, buf, fp);
518 break;
519
520 case C_EXTENSION:
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000521 m = load_dynamic_module(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000522 break;
523
Jack Jansen9c96a921995-02-15 22:57:06 +0000524#ifdef macintosh
525 case PY_RESOURCE:
526 m = PyMac_LoadResourceModule(name, buf);
527 break;
528#endif
529
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000530 default:
531 err_setstr(SystemError,
532 "find_module returned unexpected result");
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000533 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000534
535 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000536 if ( fp )
537 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000538
539 return m;
540}
541
542
543/* Initialize a built-in module.
544 Return 1 for succes, 0 if the module is not found, and -1 with
545 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000546
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000547static int
548init_builtin(name)
549 char *name;
550{
551 int i;
552 for (i = 0; inittab[i].name != NULL; i++) {
553 if (strcmp(name, inittab[i].name) == 0) {
Guido van Rossum74e6a111994-08-29 12:54:38 +0000554 if (inittab[i].initfunc == NULL) {
555 err_setstr(ImportError,
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000556 "Cannot re-init internal module");
Guido van Rossum74e6a111994-08-29 12:54:38 +0000557 return -1;
558 }
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +0000559 if (verbose)
560 fprintf(stderr, "import %s # builtin\n",
561 name);
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000562 (*inittab[i].initfunc)();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000563 if (err_occurred())
564 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000565 return 1;
566 }
567 }
568 return 0;
569}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000570
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000571
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000572/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000573
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000574static struct _frozen *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000575find_frozen(name)
576 char *name;
577{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000578 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000579
580 for (p = frozen_modules; ; p++) {
581 if (p->name == NULL)
582 return NULL;
583 if (strcmp(p->name, name) == 0)
584 break;
585 }
586 return p;
587}
588
589static object *
590get_frozen_object(name)
591 char *name;
592{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000593 struct _frozen *p = find_frozen(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000594
595 if (p == NULL) {
596 err_setstr(ImportError, "No such frozen object");
597 return NULL;
598 }
Guido van Rossum1741d601996-08-08 18:52:59 +0000599 return rds_object((char *)p->code, p->size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000600}
601
602/* Initialize a frozen module.
603 Return 1 for succes, 0 if the module is not found, and -1 with
604 an exception set if the initialization failed.
605 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +0000606
607int
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000608init_frozen(name)
609 char *name;
610{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000611 struct _frozen *p = find_frozen(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000612 object *co;
613 object *m;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000614
615 if (p == NULL)
616 return 0;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000617 if (verbose)
618 fprintf(stderr, "import %s # frozen\n", name);
Guido van Rossum1741d601996-08-08 18:52:59 +0000619 co = rds_object((char *)p->code, p->size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000620 if (co == NULL)
621 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000622 if (!is_codeobject(co)) {
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000623 DECREF(co);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000624 err_setstr(TypeError, "frozen object is not a code object");
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000625 return -1;
626 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000627 m = exec_code_module(name, co);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000628 DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000629 if (m == NULL)
630 return -1;
631 DECREF(m);
632 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000633}
Guido van Rossum74e6a111994-08-29 12:54:38 +0000634
635
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000636/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000637 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +0000638
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000639object *
640import_module(name)
641 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000642{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000643 object *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000644
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000645 if (import_modules == NULL) {
646 err_setstr(SystemError, "sys.modules has been deleted");
647 return NULL;
648 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000649 if ((m = dictlookup(import_modules, name)) != NULL) {
650 INCREF(m);
651 }
652 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000653 int i;
654 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
655 if (i < 0)
656 return NULL;
657 if ((m = dictlookup(import_modules, name)) == NULL) {
658 if (err_occurred() == NULL)
659 err_setstr(SystemError,
660 "built-in module not initialized properly");
661 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000662 else
663 INCREF(m);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000664 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000665 else
666 m = load_module(name);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000667 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000668
669 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000670}
671
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000672
673/* Re-import a module of any kind and return its module object, WITH
674 INCREMENTED REFERENCE COUNT */
675
676object *
677reload_module(m)
678 object *m;
679{
680 char *name;
681 int i;
682
683 if (m == NULL || !is_moduleobject(m)) {
684 err_setstr(TypeError, "reload() argument must be module");
685 return NULL;
686 }
687 name = getmodulename(m);
688 if (name == NULL)
689 return NULL;
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000690 if (import_modules == NULL) {
691 err_setstr(SystemError, "sys.modules has been deleted");
692 return NULL;
693 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000694 if (m != dictlookup(import_modules, name)) {
695 err_setstr(ImportError, "reload() module not in sys.modules");
696 return NULL;
697 }
698 /* Check for built-in and frozen modules */
699 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
700 if (i < 0)
701 return NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000702 INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000703 }
704 else
705 m = load_module(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000706 return m;
707}
708
709
710/* Module 'imp' provides Python access to the primitives used for
711 importing modules.
712*/
713
714static object *
715imp_get_magic(self, args)
716 object *self;
717 object *args;
718{
719 char buf[4];
720
721 if (!newgetargs(args, ""))
722 return NULL;
723 buf[0] = (MAGIC >> 0) & 0xff;
724 buf[1] = (MAGIC >> 8) & 0xff;
Guido van Rossum90f0e071995-01-30 12:53:06 +0000725 buf[2] = (MAGIC >> 16) & 0xff;
726 buf[3] = (MAGIC >> 24) & 0xff;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000727
728 return newsizedstringobject(buf, 4);
729}
730
731static object *
732imp_get_suffixes(self, args)
733 object *self;
734 object *args;
735{
736 object *list;
737 struct filedescr *fdp;
738
739 if (!newgetargs(args, ""))
740 return NULL;
741 list = newlistobject(0);
742 if (list == NULL)
743 return NULL;
744 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
745 object *item = mkvalue("ssi",
746 fdp->suffix, fdp->mode, fdp->type);
747 if (item == NULL) {
748 DECREF(list);
749 return NULL;
750 }
751 if (addlistitem(list, item) < 0) {
752 DECREF(list);
753 DECREF(item);
754 return NULL;
755 }
756 DECREF(item);
757 }
758 return list;
759}
760
761static object *
762imp_find_module(self, args)
763 object *self;
764 object *args;
765{
766 extern int fclose PROTO((FILE *));
767 char *name;
768 object *path = NULL;
769 object *fob, *ret;
770 struct filedescr *fdp;
771 char pathname[MAXPATHLEN+1];
772 FILE *fp;
773 if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
774 return NULL;
775 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
776 if (fdp == NULL)
777 return NULL;
778 fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
779 if (fob == NULL) {
780 fclose(fp);
781 return NULL;
782 }
783 ret = mkvalue("Os(ssi)",
784 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
785 DECREF(fob);
786 return ret;
787}
788
789static object *
790imp_init_builtin(self, args)
791 object *self;
792 object *args;
793{
794 char *name;
795 int ret;
796 object *m;
797 if (!newgetargs(args, "s", &name))
798 return NULL;
799 ret = init_builtin(name);
800 if (ret < 0)
801 return NULL;
802 if (ret == 0) {
803 INCREF(None);
804 return None;
805 }
806 m = add_module(name);
807 XINCREF(m);
808 return m;
809}
810
811static object *
812imp_init_frozen(self, args)
813 object *self;
814 object *args;
815{
816 char *name;
817 int ret;
818 object *m;
819 if (!newgetargs(args, "s", &name))
820 return NULL;
821 ret = init_frozen(name);
822 if (ret < 0)
823 return NULL;
824 if (ret == 0) {
825 INCREF(None);
826 return None;
827 }
828 m = add_module(name);
829 XINCREF(m);
830 return m;
831}
832
833static object *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000834imp_get_frozen_object(self, args)
835 object *self;
836 object *args;
837{
838 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +0000839
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000840 if (!newgetargs(args, "s", &name))
841 return NULL;
842 return get_frozen_object(name);
843}
844
845static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000846imp_is_builtin(self, args)
847 object *self;
848 object *args;
849{
850 int i;
851 char *name;
852 if (!newgetargs(args, "s", &name))
853 return NULL;
854 for (i = 0; inittab[i].name != NULL; i++) {
855 if (strcmp(name, inittab[i].name) == 0) {
856 if (inittab[i].initfunc == NULL)
857 return newintobject(-1);
858 else
859 return newintobject(1);
860 }
861 }
862 return newintobject(0);
863}
864
865static object *
866imp_is_frozen(self, args)
867 object *self;
868 object *args;
869{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000870 struct _frozen *p;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000871 char *name;
872 if (!newgetargs(args, "s", &name))
873 return NULL;
874 for (p = frozen_modules; ; p++) {
875 if (p->name == NULL)
876 break;
877 if (strcmp(p->name, name) == 0)
878 return newintobject(1);
879 }
880 return newintobject(0);
881}
882
883static FILE *
884get_file(pathname, fob, mode)
885 char *pathname;
886 object *fob;
887 char *mode;
888{
889 FILE *fp;
890 if (fob == NULL) {
891 fp = fopen(pathname, mode);
892 if (fp == NULL)
893 err_errno(IOError);
894 }
895 else {
896 fp = getfilefile(fob);
897 if (fp == NULL)
898 err_setstr(ValueError, "bad/closed file object");
899 }
900 return fp;
901}
902
903static object *
904imp_load_compiled(self, args)
905 object *self;
906 object *args;
907{
908 char *name;
909 char *pathname;
910 object *fob = NULL;
911 object *m;
912 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000913 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000914 return NULL;
915 fp = get_file(pathname, fob, "rb");
916 if (fp == NULL)
917 return NULL;
918 m = load_compiled_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000919 return m;
920}
921
922static object *
923imp_load_dynamic(self, args)
924 object *self;
925 object *args;
926{
927 char *name;
928 char *pathname;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000929 object *fob = NULL;
930 object *m;
931 FILE *fp = NULL;
932 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000933 return NULL;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000934 if (fob)
935 fp = get_file(pathname, fob, "r");
936 m = load_dynamic_module(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +0000937 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000938}
939
940static object *
941imp_load_source(self, args)
942 object *self;
943 object *args;
944{
945 char *name;
946 char *pathname;
947 object *fob = NULL;
948 object *m;
949 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000950 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000951 return NULL;
952 fp = get_file(pathname, fob, "r");
953 if (fp == NULL)
954 return NULL;
955 m = load_source_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000956 return m;
957}
958
Jack Jansen9c96a921995-02-15 22:57:06 +0000959#ifdef macintosh
960static object *
961imp_load_resource(self, args)
962 object *self;
963 object *args;
964{
965 char *name;
966 char *pathname;
967 object *m;
968
969 if (!newgetargs(args, "ss", &name, &pathname))
970 return NULL;
971 m = PyMac_LoadResourceModule(name, pathname);
972 return m;
973}
974#endif /* macintosh */
975
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000976static object *
977imp_new_module(self, args)
978 object *self;
979 object *args;
980{
981 char *name;
982 if (!newgetargs(args, "s", &name))
983 return NULL;
984 return newmoduleobject(name);
985}
986
987static struct methodlist imp_methods[] = {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000988 {"get_frozen_object", imp_get_frozen_object, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000989 {"get_magic", imp_get_magic, 1},
990 {"get_suffixes", imp_get_suffixes, 1},
991 {"find_module", imp_find_module, 1},
992 {"init_builtin", imp_init_builtin, 1},
993 {"init_frozen", imp_init_frozen, 1},
994 {"is_builtin", imp_is_builtin, 1},
995 {"is_frozen", imp_is_frozen, 1},
996 {"load_compiled", imp_load_compiled, 1},
997 {"load_dynamic", imp_load_dynamic, 1},
998 {"load_source", imp_load_source, 1},
999 {"new_module", imp_new_module, 1},
Jack Jansen9c96a921995-02-15 22:57:06 +00001000#ifdef macintosh
1001 {"load_resource", imp_load_resource, 1},
1002#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001003 {NULL, NULL} /* sentinel */
1004};
1005
1006void
1007initimp()
1008{
1009 object *m, *d, *v;
1010
1011 m = initmodule("imp", imp_methods);
1012 d = getmoduledict(m);
1013
1014 v = newintobject(SEARCH_ERROR);
1015 dictinsert(d, "SEARCH_ERROR", v);
1016 XDECREF(v);
1017
1018 v = newintobject(PY_SOURCE);
1019 dictinsert(d, "PY_SOURCE", v);
1020 XDECREF(v);
1021
1022 v = newintobject(PY_COMPILED);
1023 dictinsert(d, "PY_COMPILED", v);
1024 XDECREF(v);
1025
1026 v = newintobject(C_EXTENSION);
1027 dictinsert(d, "C_EXTENSION", v);
1028 XDECREF(v);
1029
Jack Jansenae12e191995-06-18 20:06:44 +00001030#ifdef macintosh
1031 v = newintobject(PY_RESOURCE);
1032 dictinsert(d, "PY_RESOURCE", v);
1033 XDECREF(v);
1034#endif
1035
1036
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001037 if (err_occurred())
1038 fatal("imp module initialization failed");
1039}