blob: 8dadc0ecbd49d6a1c8f719a1408b891c9ed8b5f1 [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
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Module definition and import implementation */
33
Guido van Rossum3f5da241990-12-20 15:06:42 +000034#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035
Guido van Rossum40f470f1996-05-23 22:51:04 +000036/* XXX Some of the following are duplicate with allobjects.h... */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038#include "token.h"
39#include "graminit.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000040#include "import.h"
41#include "errcode.h"
42#include "sysmodule.h"
Guido van Rossum6135a871995-01-09 17:53:26 +000043#include "bltinmodule.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000044#include "pythonrun.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000045#include "marshal.h"
46#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000047#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000048#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000049#include "importdl.h"
Jack Jansen9c96a921995-02-15 22:57:06 +000050#ifdef macintosh
Jack Jansen614cf811995-07-28 11:28:14 +000051/* 'argument' is a grammar symbol, but also used in some mac header files */
52#undef argument
Jack Jansen9c96a921995-02-15 22:57:06 +000053#include "macglue.h"
54#endif
Guido van Rossumc405b7b1991-06-04 19:39:42 +000055
Guido van Rossum80bb9651996-12-05 23:27:02 +000056#ifdef HAVE_UNISTD_H
57#include <unistd.h>
58#endif
59
Guido van Rossum74e6a111994-08-29 12:54:38 +000060extern long getmtime(); /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000061
Guido van Rossum6c849691994-09-26 15:47:17 +000062/* Magic word to reject .pyc files generated by other Python versions */
Guido van Rossum7faeab31995-07-07 22:50:36 +000063/* Change for each incompatible change */
64/* The value of CR and LF is incorporated so if you ever read or write
65 a .pyc file in text mode the magic number will be wrong; also, the
66 Apple MPW compiler swaps their values, botching string constants */
67/* XXX Perhaps the magic number should be frozen and a version field
68 added to the .pyc file header? */
Guido van Rossumdd5db431997-01-17 21:06:11 +000069/* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */
Guido van Rossum99d18251997-01-24 03:44:53 +000070#define MAGIC (20121 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000071
Guido van Rossum1ae940a1995-01-02 19:04:15 +000072object *import_modules; /* This becomes sys.modules */
Guido van Rossum3f5da241990-12-20 15:06:42 +000073
Guido van Rossum66f1fa81991-04-03 19:03:52 +000074
Guido van Rossum1ae940a1995-01-02 19:04:15 +000075/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076
77void
78initimport()
79{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000080 if (import_modules != NULL)
81 fatal("duplicate initimport() call");
82 if ((import_modules = newdictobject()) == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +000083 fatal("no mem for dictionary of modules");
Guido van Rossum0824f631997-03-11 18:37:35 +000084 if (Py_OptimizeFlag) {
85 /* Replace ".pyc" with ".pyo" in import_filetab */
86 struct filedescr *p;
87 for (p = import_filetab; p->suffix != NULL; p++) {
88 if (strcmp(p->suffix, ".pyc") == 0)
89 p->suffix = ".pyo";
90 }
91 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092}
93
Guido van Rossum3f5da241990-12-20 15:06:42 +000094
Guido van Rossum1ae940a1995-01-02 19:04:15 +000095/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +000096
Guido van Rossum3f5da241990-12-20 15:06:42 +000097void
98doneimport()
99{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000100 if (import_modules != NULL) {
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000101 object *tmp = import_modules;
102 import_modules = NULL;
103 /* This deletes all modules from sys.modules.
104 When a module is deallocated, it in turn clears its dictionary,
105 thus hopefully breaking any circular references between modules
106 and between a module's dictionary and its functions.
107 Note that "import" will fail while we are cleaning up.
108 */
109 mappingclear(tmp);
110 DECREF(tmp);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000111 }
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000112}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000113
114
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000115/* Helper for pythonrun.c -- return magic number */
116
117long
118get_pyc_magic()
119{
120 return MAGIC;
121}
122
123
124/* Helper for sysmodule.c -- return modules dictionary */
125
126object *
127get_modules()
128{
129 return import_modules;
130}
131
132
133/* Get the module object corresponding to a module name.
134 First check the modules dictionary if there's one there,
135 if not, create a new one and insert in in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000136 Because the former action is most common, THIS DOES NOT RETURN A
137 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000138
139object *
140add_module(name)
141 char *name;
142{
143 object *m;
144
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000145 if (import_modules == NULL) {
146 err_setstr(SystemError, "sys.modules has been deleted");
147 return NULL;
148 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000149 if ((m = dictlookup(import_modules, name)) != NULL &&
150 is_moduleobject(m))
151 return m;
152 m = newmoduleobject(name);
153 if (m == NULL)
154 return NULL;
155 if (dictinsert(import_modules, name, m) != 0) {
156 DECREF(m);
157 return NULL;
158 }
159 DECREF(m); /* Yes, it still exists, in modules! */
160
161 return m;
162}
163
164
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000165/* Execute a code object in a module and return the module object
166 WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000167
Jack Jansen9c96a921995-02-15 22:57:06 +0000168object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000169exec_code_module(name, co)
170 char *name;
Jack Jansen9c96a921995-02-15 22:57:06 +0000171 object *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000172{
173 object *m, *d, *v;
174
175 m = add_module(name);
176 if (m == NULL)
177 return NULL;
178 d = getmoduledict(m);
Guido van Rossum6135a871995-01-09 17:53:26 +0000179 if (dictlookup(d, "__builtins__") == NULL) {
Guido van Rossum94390ec1995-01-12 11:37:57 +0000180 if (dictinsert(d, "__builtins__", getbuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000181 return NULL;
182 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000183 /* Remember the filename as the __file__ attribute */
184 if (dictinsert(d, "__file__", ((codeobject *)co)->co_filename) != 0)
185 err_clear(); /* Not important enough to report */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000186 v = eval_code((codeobject *)co, d, d); /* XXX owner? */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000187 if (v == NULL)
188 return NULL;
189 DECREF(v);
190 INCREF(m);
191
192 return m;
193}
194
195
196/* Given a pathname for a Python source file, fill a buffer with the
197 pathname for the corresponding compiled file. Return the pathname
198 for the compiled file, or NULL if there's no space in the buffer.
199 Doesn't set an exception. */
200
201static char *
202make_compiled_pathname(pathname, buf, buflen)
203 char *pathname;
204 char *buf;
205 int buflen;
206{
207 int len;
208
209 len = strlen(pathname);
210 if (len+2 > buflen)
211 return NULL;
212 strcpy(buf, pathname);
Guido van Rossum0824f631997-03-11 18:37:35 +0000213 strcpy(buf+len, Py_OptimizeFlag ? "o" : "c");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000214
215 return buf;
216}
217
218
219/* Given a pathname for a Python source file, its time of last
220 modification, and a pathname for a compiled file, check whether the
221 compiled file represents the same version of the source. If so,
222 return a FILE pointer for the compiled file, positioned just after
223 the header; if not, return NULL.
224 Doesn't set an exception. */
225
226static FILE *
227check_compiled_module(pathname, mtime, cpathname)
228 char *pathname;
229 long mtime;
230 char *cpathname;
231{
232 FILE *fp;
233 long magic;
234 long pyc_mtime;
235
236 fp = fopen(cpathname, "rb");
237 if (fp == NULL)
238 return NULL;
239 magic = rd_long(fp);
240 if (magic != MAGIC) {
241 if (verbose)
242 fprintf(stderr, "# %s has bad magic\n", cpathname);
243 fclose(fp);
244 return NULL;
245 }
246 pyc_mtime = rd_long(fp);
247 if (pyc_mtime != mtime) {
248 if (verbose)
249 fprintf(stderr, "# %s has bad mtime\n", cpathname);
250 fclose(fp);
251 return NULL;
252 }
253 if (verbose)
254 fprintf(stderr, "# %s matches %s\n", cpathname, pathname);
255 return fp;
256}
257
258
259/* Read a code object from a file and check it for validity */
260
261static codeobject *
262read_compiled_module(fp)
263 FILE *fp;
264{
265 object *co;
266
267 co = rd_object(fp);
268 /* Ugly: rd_object() may return NULL with or without error */
269 if (co == NULL || !is_codeobject(co)) {
270 if (!err_occurred())
271 err_setstr(ImportError,
272 "Non-code object in .pyc file");
273 XDECREF(co);
274 return NULL;
275 }
276 return (codeobject *)co;
277}
278
279
280/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000281 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000282
283static object *
284load_compiled_module(name, cpathname, fp)
285 char *name;
286 char *cpathname;
287 FILE *fp;
288{
289 long magic;
290 codeobject *co;
291 object *m;
292
293 magic = rd_long(fp);
294 if (magic != MAGIC) {
295 err_setstr(ImportError, "Bad magic number in .pyc file");
296 return NULL;
297 }
298 (void) rd_long(fp);
299 co = read_compiled_module(fp);
300 if (co == NULL)
301 return NULL;
302 if (verbose)
303 fprintf(stderr, "import %s # precompiled from %s\n",
304 name, cpathname);
Jack Jansen9c96a921995-02-15 22:57:06 +0000305 m = exec_code_module(name, (object *)co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000306 DECREF(co);
307
308 return m;
309}
310
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000311/* Parse a source file and return the corresponding code object */
312
313static codeobject *
314parse_source_module(pathname, fp)
315 char *pathname;
316 FILE *fp;
317{
318 codeobject *co;
319 node *n;
320
321 n = parse_file(fp, pathname, file_input);
322 if (n == NULL)
323 return NULL;
324 co = compile(n, pathname);
325 freetree(n);
326
327 return co;
328}
329
330
331/* Write a compiled module to a file, placing the time of last
332 modification of its source into the header.
333 Errors are ignored, if a write error occurs an attempt is made to
334 remove the file. */
335
336static void
337write_compiled_module(co, cpathname, mtime)
338 codeobject *co;
339 char *cpathname;
340 long mtime;
341{
342 FILE *fp;
343
344 fp = fopen(cpathname, "wb");
345 if (fp == NULL) {
346 if (verbose)
347 fprintf(stderr,
348 "# can't create %s\n", cpathname);
349 return;
350 }
351 wr_long(MAGIC, fp);
352 /* First write a 0 for mtime */
353 wr_long(0L, fp);
354 wr_object((object *)co, fp);
355 if (ferror(fp)) {
356 if (verbose)
357 fprintf(stderr, "# can't write %s\n", cpathname);
358 /* Don't keep partial file */
359 fclose(fp);
360 (void) unlink(cpathname);
361 return;
362 }
363 /* Now write the true mtime */
364 fseek(fp, 4L, 0);
365 wr_long(mtime, fp);
366 fflush(fp);
367 fclose(fp);
368 if (verbose)
369 fprintf(stderr, "# wrote %s\n", cpathname);
370#ifdef macintosh
Guido van Rossumbe1a6e21996-02-21 15:29:20 +0000371 setfiletype(cpathname, 'Pyth', 'PYC ');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000372#endif
373}
374
375
376/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000377 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
378 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000379
380static object *
381load_source_module(name, pathname, fp)
382 char *name;
383 char *pathname;
384 FILE *fp;
385{
386 long mtime;
387 FILE *fpc;
388 char buf[MAXPATHLEN+1];
389 char *cpathname;
390 codeobject *co;
391 object *m;
392
393 mtime = getmtime(pathname);
394 cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1);
395 if (cpathname != NULL &&
396 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
397 co = read_compiled_module(fpc);
398 fclose(fpc);
399 if (co == NULL)
400 return NULL;
401 if (verbose)
402 fprintf(stderr, "import %s # precompiled from %s\n",
403 name, cpathname);
404 }
405 else {
406 co = parse_source_module(pathname, fp);
407 if (co == NULL)
408 return NULL;
409 if (verbose)
410 fprintf(stderr, "import %s # from %s\n",
411 name, pathname);
412 write_compiled_module(co, cpathname, mtime);
413 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000414 m = exec_code_module(name, (object *)co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000415 DECREF(co);
416
417 return m;
418}
419
420
421/* Search the path (default sys.path) for a module. Return the
422 corresponding filedescr struct, and (via return arguments) the
423 pathname and an open file. Return NULL if the module is not found. */
424
425static struct filedescr *
426find_module(name, path, buf, buflen, p_fp)
427 char *name;
428 object *path;
429 /* Output parameters: */
430 char *buf;
431 int buflen;
432 FILE **p_fp;
433{
434 int i, npath, len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +0000435 struct filedescr *fdp = NULL;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000436 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000437
Guido van Rossumac279101996-08-22 23:10:58 +0000438#ifdef MS_COREDLL
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000439 extern FILE *PyWin_FindRegisteredModule();
Guido van Rossuma5a3db71996-04-09 02:39:59 +0000440 if ((fp=PyWin_FindRegisteredModule(name, &fdp, buf, buflen))!=NULL) {
441 *p_fp = fp;
442 return fdp;
443 }
444#endif
445
446
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000447 if (path == NULL)
448 path = sysget("path");
449 if (path == NULL || !is_listobject(path)) {
450 err_setstr(ImportError,
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000451 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000452 return NULL;
453 }
454 npath = getlistsize(path);
455 namelen = strlen(name);
456 for (i = 0; i < npath; i++) {
457 object *v = getlistitem(path, i);
458 if (!is_stringobject(v))
459 continue;
460 len = getstringsize(v);
461 if (len + 2 + namelen + import_maxsuffixsize >= buflen)
462 continue; /* Too long */
463 strcpy(buf, getstringvalue(v));
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000464 if ((int)strlen(buf) != len)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000465 continue; /* v contains '\0' */
Jack Jansen9c96a921995-02-15 22:57:06 +0000466#ifdef macintosh
467 if ( PyMac_FindResourceModule(name, buf) ) {
468 static struct filedescr resfiledescr = { "", "", PY_RESOURCE};
469
470 return &resfiledescr;
471 }
472#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000473 if (len > 0 && buf[len-1] != SEP)
474 buf[len++] = SEP;
Guido van Rossum40f470f1996-05-23 22:51:04 +0000475#ifdef IMPORT_8x3_NAMES
476 /* see if we are searching in directory dos_8x3 */
477 if (len > 7 && !strncmp(buf + len - 8, "dos_8x3", 7)){
478 int j;
479 char ch; /* limit name to eight lower-case characters */
480 for (j = 0; (ch = name[j]) && j < 8; j++)
481 if (isupper(ch))
482 buf[len++] = tolower(ch);
483 else
484 buf[len++] = ch;
485 }
Guido van Rossum0e41c8c1996-06-20 14:18:34 +0000486 else /* Not in dos_8x3, use the full name */
Guido van Rossum6f489d91996-06-28 20:15:15 +0000487#endif
Guido van Rossum0e41c8c1996-06-20 14:18:34 +0000488 {
Guido van Rossum40f470f1996-05-23 22:51:04 +0000489 strcpy(buf+len, name);
490 len += namelen;
491 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000492 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
493 strcpy(buf+len, fdp->suffix);
494 if (verbose > 1)
495 fprintf(stderr, "# trying %s\n", buf);
496 fp = fopen(buf, fdp->mode);
497 if (fp != NULL)
498 break;
499 }
500 if (fp != NULL)
501 break;
502 }
503 if (fp == NULL) {
504 char buf[256];
505 sprintf(buf, "No module named %.200s", name);
506 err_setstr(ImportError, buf);
507 return NULL;
508 }
509
510 *p_fp = fp;
511 return fdp;
512}
513
514
515/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000516 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000517
518static object *
519load_module(name)
520 char *name;
521{
522 char buf[MAXPATHLEN+1];
523 struct filedescr *fdp;
524 FILE *fp = NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000525 object *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000526
527 fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp);
528 if (fdp == NULL)
529 return NULL;
530
531 switch (fdp->type) {
532
533 case PY_SOURCE:
534 m = load_source_module(name, buf, fp);
535 break;
536
537 case PY_COMPILED:
538 m = load_compiled_module(name, buf, fp);
539 break;
540
541 case C_EXTENSION:
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000542 m = load_dynamic_module(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000543 break;
544
Jack Jansen9c96a921995-02-15 22:57:06 +0000545#ifdef macintosh
546 case PY_RESOURCE:
547 m = PyMac_LoadResourceModule(name, buf);
548 break;
549#endif
550
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000551 default:
552 err_setstr(SystemError,
553 "find_module returned unexpected result");
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000554 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000555
556 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000557 if ( fp )
558 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000559
560 return m;
561}
562
563
564/* Initialize a built-in module.
565 Return 1 for succes, 0 if the module is not found, and -1 with
566 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000567
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000568static int
569init_builtin(name)
570 char *name;
571{
572 int i;
573 for (i = 0; inittab[i].name != NULL; i++) {
574 if (strcmp(name, inittab[i].name) == 0) {
Guido van Rossum74e6a111994-08-29 12:54:38 +0000575 if (inittab[i].initfunc == NULL) {
576 err_setstr(ImportError,
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000577 "Cannot re-init internal module");
Guido van Rossum74e6a111994-08-29 12:54:38 +0000578 return -1;
579 }
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +0000580 if (verbose)
581 fprintf(stderr, "import %s # builtin\n",
582 name);
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000583 (*inittab[i].initfunc)();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000584 if (err_occurred())
585 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000586 return 1;
587 }
588 }
589 return 0;
590}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000591
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000592
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000593/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000594
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000595static struct _frozen *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000596find_frozen(name)
597 char *name;
598{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000599 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000600
601 for (p = frozen_modules; ; p++) {
602 if (p->name == NULL)
603 return NULL;
604 if (strcmp(p->name, name) == 0)
605 break;
606 }
607 return p;
608}
609
610static object *
611get_frozen_object(name)
612 char *name;
613{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000614 struct _frozen *p = find_frozen(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000615
616 if (p == NULL) {
617 err_setstr(ImportError, "No such frozen object");
618 return NULL;
619 }
Guido van Rossum1741d601996-08-08 18:52:59 +0000620 return rds_object((char *)p->code, p->size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000621}
622
623/* Initialize a frozen module.
624 Return 1 for succes, 0 if the module is not found, and -1 with
625 an exception set if the initialization failed.
626 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +0000627
628int
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000629init_frozen(name)
630 char *name;
631{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000632 struct _frozen *p = find_frozen(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000633 object *co;
634 object *m;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000635
636 if (p == NULL)
637 return 0;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000638 if (verbose)
639 fprintf(stderr, "import %s # frozen\n", name);
Guido van Rossum1741d601996-08-08 18:52:59 +0000640 co = rds_object((char *)p->code, p->size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000641 if (co == NULL)
642 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000643 if (!is_codeobject(co)) {
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000644 DECREF(co);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000645 err_setstr(TypeError, "frozen object is not a code object");
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000646 return -1;
647 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000648 m = exec_code_module(name, co);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000649 DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000650 if (m == NULL)
651 return -1;
652 DECREF(m);
653 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000654}
Guido van Rossum74e6a111994-08-29 12:54:38 +0000655
656
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000657/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000658 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +0000659
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000660object *
661import_module(name)
662 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000663{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000664 object *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000665
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000666 if (import_modules == NULL) {
667 err_setstr(SystemError, "sys.modules has been deleted");
668 return NULL;
669 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000670 if ((m = dictlookup(import_modules, name)) != NULL) {
671 INCREF(m);
672 }
673 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000674 int i;
675 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
676 if (i < 0)
677 return NULL;
678 if ((m = dictlookup(import_modules, name)) == NULL) {
679 if (err_occurred() == NULL)
680 err_setstr(SystemError,
681 "built-in module not initialized properly");
682 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000683 else
684 INCREF(m);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000685 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000686 else
687 m = load_module(name);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000688 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000689
690 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000691}
692
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000693
694/* Re-import a module of any kind and return its module object, WITH
695 INCREMENTED REFERENCE COUNT */
696
697object *
698reload_module(m)
699 object *m;
700{
701 char *name;
702 int i;
703
704 if (m == NULL || !is_moduleobject(m)) {
705 err_setstr(TypeError, "reload() argument must be module");
706 return NULL;
707 }
708 name = getmodulename(m);
709 if (name == NULL)
710 return NULL;
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000711 if (import_modules == NULL) {
712 err_setstr(SystemError, "sys.modules has been deleted");
713 return NULL;
714 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000715 if (m != dictlookup(import_modules, name)) {
716 err_setstr(ImportError, "reload() module not in sys.modules");
717 return NULL;
718 }
719 /* Check for built-in and frozen modules */
720 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
721 if (i < 0)
722 return NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000723 INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000724 }
725 else
726 m = load_module(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000727 return m;
728}
729
730
731/* Module 'imp' provides Python access to the primitives used for
732 importing modules.
733*/
734
735static object *
736imp_get_magic(self, args)
737 object *self;
738 object *args;
739{
740 char buf[4];
741
742 if (!newgetargs(args, ""))
743 return NULL;
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000744 buf[0] = (char) ((MAGIC >> 0) & 0xff);
745 buf[1] = (char) ((MAGIC >> 8) & 0xff);
746 buf[2] = (char) ((MAGIC >> 16) & 0xff);
747 buf[3] = (char) ((MAGIC >> 24) & 0xff);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000748
749 return newsizedstringobject(buf, 4);
750}
751
752static object *
753imp_get_suffixes(self, args)
754 object *self;
755 object *args;
756{
757 object *list;
758 struct filedescr *fdp;
759
760 if (!newgetargs(args, ""))
761 return NULL;
762 list = newlistobject(0);
763 if (list == NULL)
764 return NULL;
765 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
766 object *item = mkvalue("ssi",
767 fdp->suffix, fdp->mode, fdp->type);
768 if (item == NULL) {
769 DECREF(list);
770 return NULL;
771 }
772 if (addlistitem(list, item) < 0) {
773 DECREF(list);
774 DECREF(item);
775 return NULL;
776 }
777 DECREF(item);
778 }
779 return list;
780}
781
782static object *
783imp_find_module(self, args)
784 object *self;
785 object *args;
786{
787 extern int fclose PROTO((FILE *));
788 char *name;
789 object *path = NULL;
790 object *fob, *ret;
791 struct filedescr *fdp;
792 char pathname[MAXPATHLEN+1];
793 FILE *fp;
794 if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
795 return NULL;
796 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
797 if (fdp == NULL)
798 return NULL;
799 fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
800 if (fob == NULL) {
801 fclose(fp);
802 return NULL;
803 }
804 ret = mkvalue("Os(ssi)",
805 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
806 DECREF(fob);
807 return ret;
808}
809
810static object *
811imp_init_builtin(self, args)
812 object *self;
813 object *args;
814{
815 char *name;
816 int ret;
817 object *m;
818 if (!newgetargs(args, "s", &name))
819 return NULL;
820 ret = init_builtin(name);
821 if (ret < 0)
822 return NULL;
823 if (ret == 0) {
824 INCREF(None);
825 return None;
826 }
827 m = add_module(name);
828 XINCREF(m);
829 return m;
830}
831
832static object *
833imp_init_frozen(self, args)
834 object *self;
835 object *args;
836{
837 char *name;
838 int ret;
839 object *m;
840 if (!newgetargs(args, "s", &name))
841 return NULL;
842 ret = init_frozen(name);
843 if (ret < 0)
844 return NULL;
845 if (ret == 0) {
846 INCREF(None);
847 return None;
848 }
849 m = add_module(name);
850 XINCREF(m);
851 return m;
852}
853
854static object *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000855imp_get_frozen_object(self, args)
856 object *self;
857 object *args;
858{
859 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +0000860
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000861 if (!newgetargs(args, "s", &name))
862 return NULL;
863 return get_frozen_object(name);
864}
865
866static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000867imp_is_builtin(self, args)
868 object *self;
869 object *args;
870{
871 int i;
872 char *name;
873 if (!newgetargs(args, "s", &name))
874 return NULL;
875 for (i = 0; inittab[i].name != NULL; i++) {
876 if (strcmp(name, inittab[i].name) == 0) {
877 if (inittab[i].initfunc == NULL)
878 return newintobject(-1);
879 else
880 return newintobject(1);
881 }
882 }
883 return newintobject(0);
884}
885
886static object *
887imp_is_frozen(self, args)
888 object *self;
889 object *args;
890{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000891 struct _frozen *p;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000892 char *name;
893 if (!newgetargs(args, "s", &name))
894 return NULL;
895 for (p = frozen_modules; ; p++) {
896 if (p->name == NULL)
897 break;
898 if (strcmp(p->name, name) == 0)
899 return newintobject(1);
900 }
901 return newintobject(0);
902}
903
904static FILE *
905get_file(pathname, fob, mode)
906 char *pathname;
907 object *fob;
908 char *mode;
909{
910 FILE *fp;
911 if (fob == NULL) {
912 fp = fopen(pathname, mode);
913 if (fp == NULL)
914 err_errno(IOError);
915 }
916 else {
917 fp = getfilefile(fob);
918 if (fp == NULL)
919 err_setstr(ValueError, "bad/closed file object");
920 }
921 return fp;
922}
923
924static object *
925imp_load_compiled(self, args)
926 object *self;
927 object *args;
928{
929 char *name;
930 char *pathname;
931 object *fob = NULL;
932 object *m;
933 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000934 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000935 return NULL;
936 fp = get_file(pathname, fob, "rb");
937 if (fp == NULL)
938 return NULL;
939 m = load_compiled_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000940 return m;
941}
942
943static object *
944imp_load_dynamic(self, args)
945 object *self;
946 object *args;
947{
948 char *name;
949 char *pathname;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000950 object *fob = NULL;
951 object *m;
952 FILE *fp = NULL;
953 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000954 return NULL;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000955 if (fob)
956 fp = get_file(pathname, fob, "r");
957 m = load_dynamic_module(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +0000958 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000959}
960
961static object *
962imp_load_source(self, args)
963 object *self;
964 object *args;
965{
966 char *name;
967 char *pathname;
968 object *fob = NULL;
969 object *m;
970 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000971 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000972 return NULL;
973 fp = get_file(pathname, fob, "r");
974 if (fp == NULL)
975 return NULL;
976 m = load_source_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000977 return m;
978}
979
Jack Jansen9c96a921995-02-15 22:57:06 +0000980#ifdef macintosh
981static object *
982imp_load_resource(self, args)
983 object *self;
984 object *args;
985{
986 char *name;
987 char *pathname;
988 object *m;
989
990 if (!newgetargs(args, "ss", &name, &pathname))
991 return NULL;
992 m = PyMac_LoadResourceModule(name, pathname);
993 return m;
994}
995#endif /* macintosh */
996
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000997static object *
998imp_new_module(self, args)
999 object *self;
1000 object *args;
1001{
1002 char *name;
1003 if (!newgetargs(args, "s", &name))
1004 return NULL;
1005 return newmoduleobject(name);
1006}
1007
1008static struct methodlist imp_methods[] = {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001009 {"get_frozen_object", imp_get_frozen_object, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001010 {"get_magic", imp_get_magic, 1},
1011 {"get_suffixes", imp_get_suffixes, 1},
1012 {"find_module", imp_find_module, 1},
1013 {"init_builtin", imp_init_builtin, 1},
1014 {"init_frozen", imp_init_frozen, 1},
1015 {"is_builtin", imp_is_builtin, 1},
1016 {"is_frozen", imp_is_frozen, 1},
1017 {"load_compiled", imp_load_compiled, 1},
1018 {"load_dynamic", imp_load_dynamic, 1},
1019 {"load_source", imp_load_source, 1},
1020 {"new_module", imp_new_module, 1},
Jack Jansen9c96a921995-02-15 22:57:06 +00001021#ifdef macintosh
1022 {"load_resource", imp_load_resource, 1},
1023#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001024 {NULL, NULL} /* sentinel */
1025};
1026
1027void
1028initimp()
1029{
1030 object *m, *d, *v;
1031
1032 m = initmodule("imp", imp_methods);
1033 d = getmoduledict(m);
1034
1035 v = newintobject(SEARCH_ERROR);
1036 dictinsert(d, "SEARCH_ERROR", v);
1037 XDECREF(v);
1038
1039 v = newintobject(PY_SOURCE);
1040 dictinsert(d, "PY_SOURCE", v);
1041 XDECREF(v);
1042
1043 v = newintobject(PY_COMPILED);
1044 dictinsert(d, "PY_COMPILED", v);
1045 XDECREF(v);
1046
1047 v = newintobject(C_EXTENSION);
1048 dictinsert(d, "C_EXTENSION", v);
1049 XDECREF(v);
1050
Jack Jansenae12e191995-06-18 20:06:44 +00001051#ifdef macintosh
1052 v = newintobject(PY_RESOURCE);
1053 dictinsert(d, "PY_RESOURCE", v);
1054 XDECREF(v);
1055#endif
1056
1057
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001058 if (err_occurred())
1059 fatal("imp module initialization failed");
1060}