blob: afaaab7eef7f9ca7c63d98845bcfa5ec403fdd3c [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 Rossum85a5fbb1990-10-14 12:07:46 +000029#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030#include "token.h"
31#include "graminit.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032#include "import.h"
33#include "errcode.h"
34#include "sysmodule.h"
Guido van Rossum6135a871995-01-09 17:53:26 +000035#include "bltinmodule.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000036#include "pythonrun.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000037#include "marshal.h"
38#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000039#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000040#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000041#include "importdl.h"
Jack Jansen9c96a921995-02-15 22:57:06 +000042#ifdef macintosh
Jack Jansen614cf811995-07-28 11:28:14 +000043/* 'argument' is a grammar symbol, but also used in some mac header files */
44#undef argument
Jack Jansen9c96a921995-02-15 22:57:06 +000045#include "macglue.h"
46#endif
Guido van Rossumc405b7b1991-06-04 19:39:42 +000047
Guido van Rossum74e6a111994-08-29 12:54:38 +000048extern int verbose; /* Defined in pythonrun.c */
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +000049
Guido van Rossum74e6a111994-08-29 12:54:38 +000050extern long getmtime(); /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000051
Guido van Rossum6c849691994-09-26 15:47:17 +000052/* Magic word to reject .pyc files generated by other Python versions */
Guido van Rossum7faeab31995-07-07 22:50:36 +000053/* Change for each incompatible change */
54/* The value of CR and LF is incorporated so if you ever read or write
55 a .pyc file in text mode the magic number will be wrong; also, the
56 Apple MPW compiler swaps their values, botching string constants */
57/* XXX Perhaps the magic number should be frozen and a version field
58 added to the .pyc file header? */
Guido van Rossum681d79a1995-07-18 14:51:37 +000059#define MAGIC (11913 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000060
Guido van Rossum1ae940a1995-01-02 19:04:15 +000061object *import_modules; /* This becomes sys.modules */
Guido van Rossum3f5da241990-12-20 15:06:42 +000062
Guido van Rossum66f1fa81991-04-03 19:03:52 +000063
Guido van Rossum1ae940a1995-01-02 19:04:15 +000064/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065
66void
67initimport()
68{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000069 if (import_modules != NULL)
70 fatal("duplicate initimport() call");
71 if ((import_modules = newdictobject()) == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +000072 fatal("no mem for dictionary of modules");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000073}
74
Guido van Rossum3f5da241990-12-20 15:06:42 +000075
Guido van Rossum1ae940a1995-01-02 19:04:15 +000076/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +000077
Guido van Rossum3f5da241990-12-20 15:06:42 +000078void
79doneimport()
80{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000081 if (import_modules != NULL) {
Guido van Rossum0de81bf1995-01-26 00:41:28 +000082 object *tmp = import_modules;
83 import_modules = NULL;
84 /* This deletes all modules from sys.modules.
85 When a module is deallocated, it in turn clears its dictionary,
86 thus hopefully breaking any circular references between modules
87 and between a module's dictionary and its functions.
88 Note that "import" will fail while we are cleaning up.
89 */
90 mappingclear(tmp);
91 DECREF(tmp);
Guido van Rossum3f5da241990-12-20 15:06:42 +000092 }
Guido van Rossum8d15b5d1990-10-26 14:58:58 +000093}
Guido van Rossum7f133ed1991-02-19 12:23:57 +000094
95
Guido van Rossum1ae940a1995-01-02 19:04:15 +000096/* Helper for pythonrun.c -- return magic number */
97
98long
99get_pyc_magic()
100{
101 return MAGIC;
102}
103
104
105/* Helper for sysmodule.c -- return modules dictionary */
106
107object *
108get_modules()
109{
110 return import_modules;
111}
112
113
114/* Get the module object corresponding to a module name.
115 First check the modules dictionary if there's one there,
116 if not, create a new one and insert in in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000117 Because the former action is most common, THIS DOES NOT RETURN A
118 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000119
120object *
121add_module(name)
122 char *name;
123{
124 object *m;
125
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000126 if (import_modules == NULL) {
127 err_setstr(SystemError, "sys.modules has been deleted");
128 return NULL;
129 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000130 if ((m = dictlookup(import_modules, name)) != NULL &&
131 is_moduleobject(m))
132 return m;
133 m = newmoduleobject(name);
134 if (m == NULL)
135 return NULL;
136 if (dictinsert(import_modules, name, m) != 0) {
137 DECREF(m);
138 return NULL;
139 }
140 DECREF(m); /* Yes, it still exists, in modules! */
141
142 return m;
143}
144
145
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000146/* Execute a code object in a module and return the module object
147 WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000148
Jack Jansen9c96a921995-02-15 22:57:06 +0000149object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000150exec_code_module(name, co)
151 char *name;
Jack Jansen9c96a921995-02-15 22:57:06 +0000152 object *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000153{
154 object *m, *d, *v;
155
156 m = add_module(name);
157 if (m == NULL)
158 return NULL;
159 d = getmoduledict(m);
Guido van Rossum6135a871995-01-09 17:53:26 +0000160 if (dictlookup(d, "__builtins__") == NULL) {
Guido van Rossum94390ec1995-01-12 11:37:57 +0000161 if (dictinsert(d, "__builtins__", getbuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000162 return NULL;
163 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000164 /* Remember the filename as the __file__ attribute */
165 if (dictinsert(d, "__file__", ((codeobject *)co)->co_filename) != 0)
166 err_clear(); /* Not important enough to report */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000167 v = eval_code((codeobject *)co, d, d); /* XXX owner? */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000168 if (v == NULL)
169 return NULL;
170 DECREF(v);
171 INCREF(m);
172
173 return m;
174}
175
176
177/* Given a pathname for a Python source file, fill a buffer with the
178 pathname for the corresponding compiled file. Return the pathname
179 for the compiled file, or NULL if there's no space in the buffer.
180 Doesn't set an exception. */
181
182static char *
183make_compiled_pathname(pathname, buf, buflen)
184 char *pathname;
185 char *buf;
186 int buflen;
187{
188 int len;
189
190 len = strlen(pathname);
191 if (len+2 > buflen)
192 return NULL;
193 strcpy(buf, pathname);
194 strcpy(buf+len, "c");
195
196 return buf;
197}
198
199
200/* Given a pathname for a Python source file, its time of last
201 modification, and a pathname for a compiled file, check whether the
202 compiled file represents the same version of the source. If so,
203 return a FILE pointer for the compiled file, positioned just after
204 the header; if not, return NULL.
205 Doesn't set an exception. */
206
207static FILE *
208check_compiled_module(pathname, mtime, cpathname)
209 char *pathname;
210 long mtime;
211 char *cpathname;
212{
213 FILE *fp;
214 long magic;
215 long pyc_mtime;
216
217 fp = fopen(cpathname, "rb");
218 if (fp == NULL)
219 return NULL;
220 magic = rd_long(fp);
221 if (magic != MAGIC) {
222 if (verbose)
223 fprintf(stderr, "# %s has bad magic\n", cpathname);
224 fclose(fp);
225 return NULL;
226 }
227 pyc_mtime = rd_long(fp);
228 if (pyc_mtime != mtime) {
229 if (verbose)
230 fprintf(stderr, "# %s has bad mtime\n", cpathname);
231 fclose(fp);
232 return NULL;
233 }
234 if (verbose)
235 fprintf(stderr, "# %s matches %s\n", cpathname, pathname);
236 return fp;
237}
238
239
240/* Read a code object from a file and check it for validity */
241
242static codeobject *
243read_compiled_module(fp)
244 FILE *fp;
245{
246 object *co;
247
248 co = rd_object(fp);
249 /* Ugly: rd_object() may return NULL with or without error */
250 if (co == NULL || !is_codeobject(co)) {
251 if (!err_occurred())
252 err_setstr(ImportError,
253 "Non-code object in .pyc file");
254 XDECREF(co);
255 return NULL;
256 }
257 return (codeobject *)co;
258}
259
260
261/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000262 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000263
264static object *
265load_compiled_module(name, cpathname, fp)
266 char *name;
267 char *cpathname;
268 FILE *fp;
269{
270 long magic;
271 codeobject *co;
272 object *m;
273
274 magic = rd_long(fp);
275 if (magic != MAGIC) {
276 err_setstr(ImportError, "Bad magic number in .pyc file");
277 return NULL;
278 }
279 (void) rd_long(fp);
280 co = read_compiled_module(fp);
281 if (co == NULL)
282 return NULL;
283 if (verbose)
284 fprintf(stderr, "import %s # precompiled from %s\n",
285 name, cpathname);
Jack Jansen9c96a921995-02-15 22:57:06 +0000286 m = exec_code_module(name, (object *)co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000287 DECREF(co);
288
289 return m;
290}
291
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000292/* Parse a source file and return the corresponding code object */
293
294static codeobject *
295parse_source_module(pathname, fp)
296 char *pathname;
297 FILE *fp;
298{
299 codeobject *co;
300 node *n;
301
302 n = parse_file(fp, pathname, file_input);
303 if (n == NULL)
304 return NULL;
305 co = compile(n, pathname);
306 freetree(n);
307
308 return co;
309}
310
311
312/* Write a compiled module to a file, placing the time of last
313 modification of its source into the header.
314 Errors are ignored, if a write error occurs an attempt is made to
315 remove the file. */
316
317static void
318write_compiled_module(co, cpathname, mtime)
319 codeobject *co;
320 char *cpathname;
321 long mtime;
322{
323 FILE *fp;
324
325 fp = fopen(cpathname, "wb");
326 if (fp == NULL) {
327 if (verbose)
328 fprintf(stderr,
329 "# can't create %s\n", cpathname);
330 return;
331 }
332 wr_long(MAGIC, fp);
333 /* First write a 0 for mtime */
334 wr_long(0L, fp);
335 wr_object((object *)co, fp);
336 if (ferror(fp)) {
337 if (verbose)
338 fprintf(stderr, "# can't write %s\n", cpathname);
339 /* Don't keep partial file */
340 fclose(fp);
341 (void) unlink(cpathname);
342 return;
343 }
344 /* Now write the true mtime */
345 fseek(fp, 4L, 0);
346 wr_long(mtime, fp);
347 fflush(fp);
348 fclose(fp);
349 if (verbose)
350 fprintf(stderr, "# wrote %s\n", cpathname);
351#ifdef macintosh
Guido van Rossumbe1a6e21996-02-21 15:29:20 +0000352 setfiletype(cpathname, 'Pyth', 'PYC ');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000353#endif
354}
355
356
357/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000358 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
359 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000360
361static object *
362load_source_module(name, pathname, fp)
363 char *name;
364 char *pathname;
365 FILE *fp;
366{
367 long mtime;
368 FILE *fpc;
369 char buf[MAXPATHLEN+1];
370 char *cpathname;
371 codeobject *co;
372 object *m;
373
374 mtime = getmtime(pathname);
375 cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1);
376 if (cpathname != NULL &&
377 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
378 co = read_compiled_module(fpc);
379 fclose(fpc);
380 if (co == NULL)
381 return NULL;
382 if (verbose)
383 fprintf(stderr, "import %s # precompiled from %s\n",
384 name, cpathname);
385 }
386 else {
387 co = parse_source_module(pathname, fp);
388 if (co == NULL)
389 return NULL;
390 if (verbose)
391 fprintf(stderr, "import %s # from %s\n",
392 name, pathname);
393 write_compiled_module(co, cpathname, mtime);
394 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000395 m = exec_code_module(name, (object *)co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000396 DECREF(co);
397
398 return m;
399}
400
401
402/* Search the path (default sys.path) for a module. Return the
403 corresponding filedescr struct, and (via return arguments) the
404 pathname and an open file. Return NULL if the module is not found. */
405
406static struct filedescr *
407find_module(name, path, buf, buflen, p_fp)
408 char *name;
409 object *path;
410 /* Output parameters: */
411 char *buf;
412 int buflen;
413 FILE **p_fp;
414{
415 int i, npath, len, namelen;
416 struct filedescr *fdp;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000417 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000418
Guido van Rossuma5a3db71996-04-09 02:39:59 +0000419#ifdef NT
420 if ((fp=PyWin_FindRegisteredModule(name, &fdp, buf, buflen))!=NULL) {
421 *p_fp = fp;
422 return fdp;
423 }
424#endif
425
426
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000427 if (path == NULL)
428 path = sysget("path");
429 if (path == NULL || !is_listobject(path)) {
430 err_setstr(ImportError,
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000431 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000432 return NULL;
433 }
434 npath = getlistsize(path);
435 namelen = strlen(name);
436 for (i = 0; i < npath; i++) {
437 object *v = getlistitem(path, i);
438 if (!is_stringobject(v))
439 continue;
440 len = getstringsize(v);
441 if (len + 2 + namelen + import_maxsuffixsize >= buflen)
442 continue; /* Too long */
443 strcpy(buf, getstringvalue(v));
444 if (strlen(buf) != len)
445 continue; /* v contains '\0' */
Jack Jansen9c96a921995-02-15 22:57:06 +0000446#ifdef macintosh
447 if ( PyMac_FindResourceModule(name, buf) ) {
448 static struct filedescr resfiledescr = { "", "", PY_RESOURCE};
449
450 return &resfiledescr;
451 }
452#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000453 if (len > 0 && buf[len-1] != SEP)
454 buf[len++] = SEP;
455 strcpy(buf+len, name);
456 len += namelen;
457 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
458 strcpy(buf+len, fdp->suffix);
459 if (verbose > 1)
460 fprintf(stderr, "# trying %s\n", buf);
461 fp = fopen(buf, fdp->mode);
462 if (fp != NULL)
463 break;
464 }
465 if (fp != NULL)
466 break;
467 }
468 if (fp == NULL) {
469 char buf[256];
470 sprintf(buf, "No module named %.200s", name);
471 err_setstr(ImportError, buf);
472 return NULL;
473 }
474
475 *p_fp = fp;
476 return fdp;
477}
478
479
480/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000481 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000482
483static object *
484load_module(name)
485 char *name;
486{
487 char buf[MAXPATHLEN+1];
488 struct filedescr *fdp;
489 FILE *fp = NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000490 object *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000491
492 fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp);
493 if (fdp == NULL)
494 return NULL;
495
496 switch (fdp->type) {
497
498 case PY_SOURCE:
499 m = load_source_module(name, buf, fp);
500 break;
501
502 case PY_COMPILED:
503 m = load_compiled_module(name, buf, fp);
504 break;
505
506 case C_EXTENSION:
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000507 m = load_dynamic_module(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000508 break;
509
Jack Jansen9c96a921995-02-15 22:57:06 +0000510#ifdef macintosh
511 case PY_RESOURCE:
512 m = PyMac_LoadResourceModule(name, buf);
513 break;
514#endif
515
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000516 default:
517 err_setstr(SystemError,
518 "find_module returned unexpected result");
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000519 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000520
521 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000522 if ( fp )
523 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000524
525 return m;
526}
527
528
529/* Initialize a built-in module.
530 Return 1 for succes, 0 if the module is not found, and -1 with
531 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000532
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000533static int
534init_builtin(name)
535 char *name;
536{
537 int i;
538 for (i = 0; inittab[i].name != NULL; i++) {
539 if (strcmp(name, inittab[i].name) == 0) {
Guido van Rossum74e6a111994-08-29 12:54:38 +0000540 if (inittab[i].initfunc == NULL) {
541 err_setstr(ImportError,
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000542 "Cannot re-init internal module");
Guido van Rossum74e6a111994-08-29 12:54:38 +0000543 return -1;
544 }
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +0000545 if (verbose)
546 fprintf(stderr, "import %s # builtin\n",
547 name);
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000548 (*inittab[i].initfunc)();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000549 if (err_occurred())
550 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000551 return 1;
552 }
553 }
554 return 0;
555}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000556
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000557
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000558/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000559
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000560extern struct frozen {
561 char *name;
562 char *code;
563 int size;
564} frozen_modules[];
565
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000566static struct frozen *
567find_frozen(name)
568 char *name;
569{
570 struct frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000571
572 for (p = frozen_modules; ; p++) {
573 if (p->name == NULL)
574 return NULL;
575 if (strcmp(p->name, name) == 0)
576 break;
577 }
578 return p;
579}
580
581static object *
582get_frozen_object(name)
583 char *name;
584{
585 struct frozen *p = find_frozen(name);
586
587 if (p == NULL) {
588 err_setstr(ImportError, "No such frozen object");
589 return NULL;
590 }
591 return rds_object(p->code, p->size);
592}
593
594/* Initialize a frozen module.
595 Return 1 for succes, 0 if the module is not found, and -1 with
596 an exception set if the initialization failed.
597 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +0000598
599int
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000600init_frozen(name)
601 char *name;
602{
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000603 struct frozen *p = find_frozen(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000604 object *co;
605 object *m;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000606
607 if (p == NULL)
608 return 0;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000609 if (verbose)
610 fprintf(stderr, "import %s # frozen\n", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000611 co = rds_object(p->code, p->size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000612 if (co == NULL)
613 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000614 if (!is_codeobject(co)) {
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000615 DECREF(co);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000616 err_setstr(TypeError, "frozen object is not a code object");
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000617 return -1;
618 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000619 m = exec_code_module(name, co);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000620 DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000621 if (m == NULL)
622 return -1;
623 DECREF(m);
624 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000625}
Guido van Rossum74e6a111994-08-29 12:54:38 +0000626
627
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000628/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000629 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +0000630
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000631object *
632import_module(name)
633 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000634{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000635 object *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000636
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000637 if (import_modules == NULL) {
638 err_setstr(SystemError, "sys.modules has been deleted");
639 return NULL;
640 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000641 if ((m = dictlookup(import_modules, name)) != NULL) {
642 INCREF(m);
643 }
644 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000645 int i;
646 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
647 if (i < 0)
648 return NULL;
649 if ((m = dictlookup(import_modules, name)) == NULL) {
650 if (err_occurred() == NULL)
651 err_setstr(SystemError,
652 "built-in module not initialized properly");
653 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000654 else
655 INCREF(m);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000656 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000657 else
658 m = load_module(name);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000659 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000660
661 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000662}
663
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000664
665/* Re-import a module of any kind and return its module object, WITH
666 INCREMENTED REFERENCE COUNT */
667
668object *
669reload_module(m)
670 object *m;
671{
672 char *name;
673 int i;
674
675 if (m == NULL || !is_moduleobject(m)) {
676 err_setstr(TypeError, "reload() argument must be module");
677 return NULL;
678 }
679 name = getmodulename(m);
680 if (name == NULL)
681 return NULL;
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000682 if (import_modules == NULL) {
683 err_setstr(SystemError, "sys.modules has been deleted");
684 return NULL;
685 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000686 if (m != dictlookup(import_modules, name)) {
687 err_setstr(ImportError, "reload() module not in sys.modules");
688 return NULL;
689 }
690 /* Check for built-in and frozen modules */
691 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
692 if (i < 0)
693 return NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000694 INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000695 }
696 else
697 m = load_module(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000698 return m;
699}
700
701
702/* Module 'imp' provides Python access to the primitives used for
703 importing modules.
704*/
705
706static object *
707imp_get_magic(self, args)
708 object *self;
709 object *args;
710{
711 char buf[4];
712
713 if (!newgetargs(args, ""))
714 return NULL;
715 buf[0] = (MAGIC >> 0) & 0xff;
716 buf[1] = (MAGIC >> 8) & 0xff;
Guido van Rossum90f0e071995-01-30 12:53:06 +0000717 buf[2] = (MAGIC >> 16) & 0xff;
718 buf[3] = (MAGIC >> 24) & 0xff;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000719
720 return newsizedstringobject(buf, 4);
721}
722
723static object *
724imp_get_suffixes(self, args)
725 object *self;
726 object *args;
727{
728 object *list;
729 struct filedescr *fdp;
730
731 if (!newgetargs(args, ""))
732 return NULL;
733 list = newlistobject(0);
734 if (list == NULL)
735 return NULL;
736 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
737 object *item = mkvalue("ssi",
738 fdp->suffix, fdp->mode, fdp->type);
739 if (item == NULL) {
740 DECREF(list);
741 return NULL;
742 }
743 if (addlistitem(list, item) < 0) {
744 DECREF(list);
745 DECREF(item);
746 return NULL;
747 }
748 DECREF(item);
749 }
750 return list;
751}
752
753static object *
754imp_find_module(self, args)
755 object *self;
756 object *args;
757{
758 extern int fclose PROTO((FILE *));
759 char *name;
760 object *path = NULL;
761 object *fob, *ret;
762 struct filedescr *fdp;
763 char pathname[MAXPATHLEN+1];
764 FILE *fp;
765 if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
766 return NULL;
767 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
768 if (fdp == NULL)
769 return NULL;
770 fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
771 if (fob == NULL) {
772 fclose(fp);
773 return NULL;
774 }
775 ret = mkvalue("Os(ssi)",
776 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
777 DECREF(fob);
778 return ret;
779}
780
781static object *
782imp_init_builtin(self, args)
783 object *self;
784 object *args;
785{
786 char *name;
787 int ret;
788 object *m;
789 if (!newgetargs(args, "s", &name))
790 return NULL;
791 ret = init_builtin(name);
792 if (ret < 0)
793 return NULL;
794 if (ret == 0) {
795 INCREF(None);
796 return None;
797 }
798 m = add_module(name);
799 XINCREF(m);
800 return m;
801}
802
803static object *
804imp_init_frozen(self, args)
805 object *self;
806 object *args;
807{
808 char *name;
809 int ret;
810 object *m;
811 if (!newgetargs(args, "s", &name))
812 return NULL;
813 ret = init_frozen(name);
814 if (ret < 0)
815 return NULL;
816 if (ret == 0) {
817 INCREF(None);
818 return None;
819 }
820 m = add_module(name);
821 XINCREF(m);
822 return m;
823}
824
825static object *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000826imp_get_frozen_object(self, args)
827 object *self;
828 object *args;
829{
830 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +0000831
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000832 if (!newgetargs(args, "s", &name))
833 return NULL;
834 return get_frozen_object(name);
835}
836
837static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000838imp_is_builtin(self, args)
839 object *self;
840 object *args;
841{
842 int i;
843 char *name;
844 if (!newgetargs(args, "s", &name))
845 return NULL;
846 for (i = 0; inittab[i].name != NULL; i++) {
847 if (strcmp(name, inittab[i].name) == 0) {
848 if (inittab[i].initfunc == NULL)
849 return newintobject(-1);
850 else
851 return newintobject(1);
852 }
853 }
854 return newintobject(0);
855}
856
857static object *
858imp_is_frozen(self, args)
859 object *self;
860 object *args;
861{
862 struct frozen *p;
863 char *name;
864 if (!newgetargs(args, "s", &name))
865 return NULL;
866 for (p = frozen_modules; ; p++) {
867 if (p->name == NULL)
868 break;
869 if (strcmp(p->name, name) == 0)
870 return newintobject(1);
871 }
872 return newintobject(0);
873}
874
875static FILE *
876get_file(pathname, fob, mode)
877 char *pathname;
878 object *fob;
879 char *mode;
880{
881 FILE *fp;
882 if (fob == NULL) {
883 fp = fopen(pathname, mode);
884 if (fp == NULL)
885 err_errno(IOError);
886 }
887 else {
888 fp = getfilefile(fob);
889 if (fp == NULL)
890 err_setstr(ValueError, "bad/closed file object");
891 }
892 return fp;
893}
894
895static object *
896imp_load_compiled(self, args)
897 object *self;
898 object *args;
899{
900 char *name;
901 char *pathname;
902 object *fob = NULL;
903 object *m;
904 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000905 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000906 return NULL;
907 fp = get_file(pathname, fob, "rb");
908 if (fp == NULL)
909 return NULL;
910 m = load_compiled_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000911 return m;
912}
913
914static object *
915imp_load_dynamic(self, args)
916 object *self;
917 object *args;
918{
919 char *name;
920 char *pathname;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000921 object *fob = NULL;
922 object *m;
923 FILE *fp = NULL;
924 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000925 return NULL;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000926 if (fob)
927 fp = get_file(pathname, fob, "r");
928 m = load_dynamic_module(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +0000929 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000930}
931
932static object *
933imp_load_source(self, args)
934 object *self;
935 object *args;
936{
937 char *name;
938 char *pathname;
939 object *fob = NULL;
940 object *m;
941 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000942 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000943 return NULL;
944 fp = get_file(pathname, fob, "r");
945 if (fp == NULL)
946 return NULL;
947 m = load_source_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000948 return m;
949}
950
Jack Jansen9c96a921995-02-15 22:57:06 +0000951#ifdef macintosh
952static object *
953imp_load_resource(self, args)
954 object *self;
955 object *args;
956{
957 char *name;
958 char *pathname;
959 object *m;
960
961 if (!newgetargs(args, "ss", &name, &pathname))
962 return NULL;
963 m = PyMac_LoadResourceModule(name, pathname);
964 return m;
965}
966#endif /* macintosh */
967
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000968static object *
969imp_new_module(self, args)
970 object *self;
971 object *args;
972{
973 char *name;
974 if (!newgetargs(args, "s", &name))
975 return NULL;
976 return newmoduleobject(name);
977}
978
979static struct methodlist imp_methods[] = {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000980 {"get_frozen_object", imp_get_frozen_object, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000981 {"get_magic", imp_get_magic, 1},
982 {"get_suffixes", imp_get_suffixes, 1},
983 {"find_module", imp_find_module, 1},
984 {"init_builtin", imp_init_builtin, 1},
985 {"init_frozen", imp_init_frozen, 1},
986 {"is_builtin", imp_is_builtin, 1},
987 {"is_frozen", imp_is_frozen, 1},
988 {"load_compiled", imp_load_compiled, 1},
989 {"load_dynamic", imp_load_dynamic, 1},
990 {"load_source", imp_load_source, 1},
991 {"new_module", imp_new_module, 1},
Jack Jansen9c96a921995-02-15 22:57:06 +0000992#ifdef macintosh
993 {"load_resource", imp_load_resource, 1},
994#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000995 {NULL, NULL} /* sentinel */
996};
997
998void
999initimp()
1000{
1001 object *m, *d, *v;
1002
1003 m = initmodule("imp", imp_methods);
1004 d = getmoduledict(m);
1005
1006 v = newintobject(SEARCH_ERROR);
1007 dictinsert(d, "SEARCH_ERROR", v);
1008 XDECREF(v);
1009
1010 v = newintobject(PY_SOURCE);
1011 dictinsert(d, "PY_SOURCE", v);
1012 XDECREF(v);
1013
1014 v = newintobject(PY_COMPILED);
1015 dictinsert(d, "PY_COMPILED", v);
1016 XDECREF(v);
1017
1018 v = newintobject(C_EXTENSION);
1019 dictinsert(d, "C_EXTENSION", v);
1020 XDECREF(v);
1021
Jack Jansenae12e191995-06-18 20:06:44 +00001022#ifdef macintosh
1023 v = newintobject(PY_RESOURCE);
1024 dictinsert(d, "PY_RESOURCE", v);
1025 XDECREF(v);
1026#endif
1027
1028
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001029 if (err_occurred())
1030 fatal("imp module initialization failed");
1031}