blob: 06350b78a9b21df4737ac32ef8efd37f8ed3bc6f [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 Rossuma5a3db71996-04-09 02:39:59 +0000439 if ((fp=PyWin_FindRegisteredModule(name, &fdp, buf, buflen))!=NULL) {
440 *p_fp = fp;
441 return fdp;
442 }
443#endif
444
445
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000446 if (path == NULL)
447 path = sysget("path");
448 if (path == NULL || !is_listobject(path)) {
449 err_setstr(ImportError,
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000450 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000451 return NULL;
452 }
453 npath = getlistsize(path);
454 namelen = strlen(name);
455 for (i = 0; i < npath; i++) {
456 object *v = getlistitem(path, i);
457 if (!is_stringobject(v))
458 continue;
459 len = getstringsize(v);
460 if (len + 2 + namelen + import_maxsuffixsize >= buflen)
461 continue; /* Too long */
462 strcpy(buf, getstringvalue(v));
463 if (strlen(buf) != len)
464 continue; /* v contains '\0' */
Jack Jansen9c96a921995-02-15 22:57:06 +0000465#ifdef macintosh
466 if ( PyMac_FindResourceModule(name, buf) ) {
467 static struct filedescr resfiledescr = { "", "", PY_RESOURCE};
468
469 return &resfiledescr;
470 }
471#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000472 if (len > 0 && buf[len-1] != SEP)
473 buf[len++] = SEP;
Guido van Rossum40f470f1996-05-23 22:51:04 +0000474#ifdef IMPORT_8x3_NAMES
475 /* see if we are searching in directory dos_8x3 */
476 if (len > 7 && !strncmp(buf + len - 8, "dos_8x3", 7)){
477 int j;
478 char ch; /* limit name to eight lower-case characters */
479 for (j = 0; (ch = name[j]) && j < 8; j++)
480 if (isupper(ch))
481 buf[len++] = tolower(ch);
482 else
483 buf[len++] = ch;
484 }
Guido van Rossum0e41c8c1996-06-20 14:18:34 +0000485 else /* Not in dos_8x3, use the full name */
Guido van Rossum6f489d91996-06-28 20:15:15 +0000486#endif
Guido van Rossum0e41c8c1996-06-20 14:18:34 +0000487 {
Guido van Rossum40f470f1996-05-23 22:51:04 +0000488 strcpy(buf+len, name);
489 len += namelen;
490 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000491 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
492 strcpy(buf+len, fdp->suffix);
493 if (verbose > 1)
494 fprintf(stderr, "# trying %s\n", buf);
495 fp = fopen(buf, fdp->mode);
496 if (fp != NULL)
497 break;
498 }
499 if (fp != NULL)
500 break;
501 }
502 if (fp == NULL) {
503 char buf[256];
504 sprintf(buf, "No module named %.200s", name);
505 err_setstr(ImportError, buf);
506 return NULL;
507 }
508
509 *p_fp = fp;
510 return fdp;
511}
512
513
514/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000515 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000516
517static object *
518load_module(name)
519 char *name;
520{
521 char buf[MAXPATHLEN+1];
522 struct filedescr *fdp;
523 FILE *fp = NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000524 object *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000525
526 fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp);
527 if (fdp == NULL)
528 return NULL;
529
530 switch (fdp->type) {
531
532 case PY_SOURCE:
533 m = load_source_module(name, buf, fp);
534 break;
535
536 case PY_COMPILED:
537 m = load_compiled_module(name, buf, fp);
538 break;
539
540 case C_EXTENSION:
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000541 m = load_dynamic_module(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000542 break;
543
Jack Jansen9c96a921995-02-15 22:57:06 +0000544#ifdef macintosh
545 case PY_RESOURCE:
546 m = PyMac_LoadResourceModule(name, buf);
547 break;
548#endif
549
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000550 default:
551 err_setstr(SystemError,
552 "find_module returned unexpected result");
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000553 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000554
555 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000556 if ( fp )
557 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000558
559 return m;
560}
561
562
563/* Initialize a built-in module.
564 Return 1 for succes, 0 if the module is not found, and -1 with
565 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000566
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000567static int
568init_builtin(name)
569 char *name;
570{
571 int i;
572 for (i = 0; inittab[i].name != NULL; i++) {
573 if (strcmp(name, inittab[i].name) == 0) {
Guido van Rossum74e6a111994-08-29 12:54:38 +0000574 if (inittab[i].initfunc == NULL) {
575 err_setstr(ImportError,
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000576 "Cannot re-init internal module");
Guido van Rossum74e6a111994-08-29 12:54:38 +0000577 return -1;
578 }
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +0000579 if (verbose)
580 fprintf(stderr, "import %s # builtin\n",
581 name);
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000582 (*inittab[i].initfunc)();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000583 if (err_occurred())
584 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000585 return 1;
586 }
587 }
588 return 0;
589}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000590
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000591
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000592/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000593
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000594static struct _frozen *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000595find_frozen(name)
596 char *name;
597{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000598 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000599
600 for (p = frozen_modules; ; p++) {
601 if (p->name == NULL)
602 return NULL;
603 if (strcmp(p->name, name) == 0)
604 break;
605 }
606 return p;
607}
608
609static object *
610get_frozen_object(name)
611 char *name;
612{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000613 struct _frozen *p = find_frozen(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000614
615 if (p == NULL) {
616 err_setstr(ImportError, "No such frozen object");
617 return NULL;
618 }
Guido van Rossum1741d601996-08-08 18:52:59 +0000619 return rds_object((char *)p->code, p->size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000620}
621
622/* Initialize a frozen module.
623 Return 1 for succes, 0 if the module is not found, and -1 with
624 an exception set if the initialization failed.
625 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +0000626
627int
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000628init_frozen(name)
629 char *name;
630{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000631 struct _frozen *p = find_frozen(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000632 object *co;
633 object *m;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000634
635 if (p == NULL)
636 return 0;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000637 if (verbose)
638 fprintf(stderr, "import %s # frozen\n", name);
Guido van Rossum1741d601996-08-08 18:52:59 +0000639 co = rds_object((char *)p->code, p->size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000640 if (co == NULL)
641 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000642 if (!is_codeobject(co)) {
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000643 DECREF(co);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000644 err_setstr(TypeError, "frozen object is not a code object");
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000645 return -1;
646 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000647 m = exec_code_module(name, co);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000648 DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000649 if (m == NULL)
650 return -1;
651 DECREF(m);
652 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000653}
Guido van Rossum74e6a111994-08-29 12:54:38 +0000654
655
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000656/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000657 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +0000658
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000659object *
660import_module(name)
661 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000662{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000663 object *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000664
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000665 if (import_modules == NULL) {
666 err_setstr(SystemError, "sys.modules has been deleted");
667 return NULL;
668 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000669 if ((m = dictlookup(import_modules, name)) != NULL) {
670 INCREF(m);
671 }
672 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000673 int i;
674 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
675 if (i < 0)
676 return NULL;
677 if ((m = dictlookup(import_modules, name)) == NULL) {
678 if (err_occurred() == NULL)
679 err_setstr(SystemError,
680 "built-in module not initialized properly");
681 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000682 else
683 INCREF(m);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000684 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000685 else
686 m = load_module(name);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000687 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000688
689 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000690}
691
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000692
693/* Re-import a module of any kind and return its module object, WITH
694 INCREMENTED REFERENCE COUNT */
695
696object *
697reload_module(m)
698 object *m;
699{
700 char *name;
701 int i;
702
703 if (m == NULL || !is_moduleobject(m)) {
704 err_setstr(TypeError, "reload() argument must be module");
705 return NULL;
706 }
707 name = getmodulename(m);
708 if (name == NULL)
709 return NULL;
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000710 if (import_modules == NULL) {
711 err_setstr(SystemError, "sys.modules has been deleted");
712 return NULL;
713 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000714 if (m != dictlookup(import_modules, name)) {
715 err_setstr(ImportError, "reload() module not in sys.modules");
716 return NULL;
717 }
718 /* Check for built-in and frozen modules */
719 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
720 if (i < 0)
721 return NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000722 INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000723 }
724 else
725 m = load_module(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000726 return m;
727}
728
729
730/* Module 'imp' provides Python access to the primitives used for
731 importing modules.
732*/
733
734static object *
735imp_get_magic(self, args)
736 object *self;
737 object *args;
738{
739 char buf[4];
740
741 if (!newgetargs(args, ""))
742 return NULL;
743 buf[0] = (MAGIC >> 0) & 0xff;
744 buf[1] = (MAGIC >> 8) & 0xff;
Guido van Rossum90f0e071995-01-30 12:53:06 +0000745 buf[2] = (MAGIC >> 16) & 0xff;
746 buf[3] = (MAGIC >> 24) & 0xff;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000747
748 return newsizedstringobject(buf, 4);
749}
750
751static object *
752imp_get_suffixes(self, args)
753 object *self;
754 object *args;
755{
756 object *list;
757 struct filedescr *fdp;
758
759 if (!newgetargs(args, ""))
760 return NULL;
761 list = newlistobject(0);
762 if (list == NULL)
763 return NULL;
764 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
765 object *item = mkvalue("ssi",
766 fdp->suffix, fdp->mode, fdp->type);
767 if (item == NULL) {
768 DECREF(list);
769 return NULL;
770 }
771 if (addlistitem(list, item) < 0) {
772 DECREF(list);
773 DECREF(item);
774 return NULL;
775 }
776 DECREF(item);
777 }
778 return list;
779}
780
781static object *
782imp_find_module(self, args)
783 object *self;
784 object *args;
785{
786 extern int fclose PROTO((FILE *));
787 char *name;
788 object *path = NULL;
789 object *fob, *ret;
790 struct filedescr *fdp;
791 char pathname[MAXPATHLEN+1];
792 FILE *fp;
793 if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
794 return NULL;
795 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
796 if (fdp == NULL)
797 return NULL;
798 fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
799 if (fob == NULL) {
800 fclose(fp);
801 return NULL;
802 }
803 ret = mkvalue("Os(ssi)",
804 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
805 DECREF(fob);
806 return ret;
807}
808
809static object *
810imp_init_builtin(self, args)
811 object *self;
812 object *args;
813{
814 char *name;
815 int ret;
816 object *m;
817 if (!newgetargs(args, "s", &name))
818 return NULL;
819 ret = init_builtin(name);
820 if (ret < 0)
821 return NULL;
822 if (ret == 0) {
823 INCREF(None);
824 return None;
825 }
826 m = add_module(name);
827 XINCREF(m);
828 return m;
829}
830
831static object *
832imp_init_frozen(self, args)
833 object *self;
834 object *args;
835{
836 char *name;
837 int ret;
838 object *m;
839 if (!newgetargs(args, "s", &name))
840 return NULL;
841 ret = init_frozen(name);
842 if (ret < 0)
843 return NULL;
844 if (ret == 0) {
845 INCREF(None);
846 return None;
847 }
848 m = add_module(name);
849 XINCREF(m);
850 return m;
851}
852
853static object *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000854imp_get_frozen_object(self, args)
855 object *self;
856 object *args;
857{
858 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +0000859
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000860 if (!newgetargs(args, "s", &name))
861 return NULL;
862 return get_frozen_object(name);
863}
864
865static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000866imp_is_builtin(self, args)
867 object *self;
868 object *args;
869{
870 int i;
871 char *name;
872 if (!newgetargs(args, "s", &name))
873 return NULL;
874 for (i = 0; inittab[i].name != NULL; i++) {
875 if (strcmp(name, inittab[i].name) == 0) {
876 if (inittab[i].initfunc == NULL)
877 return newintobject(-1);
878 else
879 return newintobject(1);
880 }
881 }
882 return newintobject(0);
883}
884
885static object *
886imp_is_frozen(self, args)
887 object *self;
888 object *args;
889{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000890 struct _frozen *p;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000891 char *name;
892 if (!newgetargs(args, "s", &name))
893 return NULL;
894 for (p = frozen_modules; ; p++) {
895 if (p->name == NULL)
896 break;
897 if (strcmp(p->name, name) == 0)
898 return newintobject(1);
899 }
900 return newintobject(0);
901}
902
903static FILE *
904get_file(pathname, fob, mode)
905 char *pathname;
906 object *fob;
907 char *mode;
908{
909 FILE *fp;
910 if (fob == NULL) {
911 fp = fopen(pathname, mode);
912 if (fp == NULL)
913 err_errno(IOError);
914 }
915 else {
916 fp = getfilefile(fob);
917 if (fp == NULL)
918 err_setstr(ValueError, "bad/closed file object");
919 }
920 return fp;
921}
922
923static object *
924imp_load_compiled(self, args)
925 object *self;
926 object *args;
927{
928 char *name;
929 char *pathname;
930 object *fob = NULL;
931 object *m;
932 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000933 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000934 return NULL;
935 fp = get_file(pathname, fob, "rb");
936 if (fp == NULL)
937 return NULL;
938 m = load_compiled_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000939 return m;
940}
941
942static object *
943imp_load_dynamic(self, args)
944 object *self;
945 object *args;
946{
947 char *name;
948 char *pathname;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000949 object *fob = NULL;
950 object *m;
951 FILE *fp = NULL;
952 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000953 return NULL;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000954 if (fob)
955 fp = get_file(pathname, fob, "r");
956 m = load_dynamic_module(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +0000957 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000958}
959
960static object *
961imp_load_source(self, args)
962 object *self;
963 object *args;
964{
965 char *name;
966 char *pathname;
967 object *fob = NULL;
968 object *m;
969 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000970 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000971 return NULL;
972 fp = get_file(pathname, fob, "r");
973 if (fp == NULL)
974 return NULL;
975 m = load_source_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000976 return m;
977}
978
Jack Jansen9c96a921995-02-15 22:57:06 +0000979#ifdef macintosh
980static object *
981imp_load_resource(self, args)
982 object *self;
983 object *args;
984{
985 char *name;
986 char *pathname;
987 object *m;
988
989 if (!newgetargs(args, "ss", &name, &pathname))
990 return NULL;
991 m = PyMac_LoadResourceModule(name, pathname);
992 return m;
993}
994#endif /* macintosh */
995
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000996static object *
997imp_new_module(self, args)
998 object *self;
999 object *args;
1000{
1001 char *name;
1002 if (!newgetargs(args, "s", &name))
1003 return NULL;
1004 return newmoduleobject(name);
1005}
1006
1007static struct methodlist imp_methods[] = {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001008 {"get_frozen_object", imp_get_frozen_object, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001009 {"get_magic", imp_get_magic, 1},
1010 {"get_suffixes", imp_get_suffixes, 1},
1011 {"find_module", imp_find_module, 1},
1012 {"init_builtin", imp_init_builtin, 1},
1013 {"init_frozen", imp_init_frozen, 1},
1014 {"is_builtin", imp_is_builtin, 1},
1015 {"is_frozen", imp_is_frozen, 1},
1016 {"load_compiled", imp_load_compiled, 1},
1017 {"load_dynamic", imp_load_dynamic, 1},
1018 {"load_source", imp_load_source, 1},
1019 {"new_module", imp_new_module, 1},
Jack Jansen9c96a921995-02-15 22:57:06 +00001020#ifdef macintosh
1021 {"load_resource", imp_load_resource, 1},
1022#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001023 {NULL, NULL} /* sentinel */
1024};
1025
1026void
1027initimp()
1028{
1029 object *m, *d, *v;
1030
1031 m = initmodule("imp", imp_methods);
1032 d = getmoduledict(m);
1033
1034 v = newintobject(SEARCH_ERROR);
1035 dictinsert(d, "SEARCH_ERROR", v);
1036 XDECREF(v);
1037
1038 v = newintobject(PY_SOURCE);
1039 dictinsert(d, "PY_SOURCE", v);
1040 XDECREF(v);
1041
1042 v = newintobject(PY_COMPILED);
1043 dictinsert(d, "PY_COMPILED", v);
1044 XDECREF(v);
1045
1046 v = newintobject(C_EXTENSION);
1047 dictinsert(d, "C_EXTENSION", v);
1048 XDECREF(v);
1049
Jack Jansenae12e191995-06-18 20:06:44 +00001050#ifdef macintosh
1051 v = newintobject(PY_RESOURCE);
1052 dictinsert(d, "PY_RESOURCE", v);
1053 XDECREF(v);
1054#endif
1055
1056
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001057 if (err_occurred())
1058 fatal("imp module initialization failed");
1059}