blob: 18968d0a0fc812a639c3178ea6e533a65fcbb0f1 [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 Rossum681d79a1995-07-18 14:51:37 +0000164 v = eval_code((codeobject *)co, d, d); /* XXX owner? */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000165 if (v == NULL)
166 return NULL;
167 DECREF(v);
168 INCREF(m);
169
170 return m;
171}
172
173
174/* Given a pathname for a Python source file, fill a buffer with the
175 pathname for the corresponding compiled file. Return the pathname
176 for the compiled file, or NULL if there's no space in the buffer.
177 Doesn't set an exception. */
178
179static char *
180make_compiled_pathname(pathname, buf, buflen)
181 char *pathname;
182 char *buf;
183 int buflen;
184{
185 int len;
186
187 len = strlen(pathname);
188 if (len+2 > buflen)
189 return NULL;
190 strcpy(buf, pathname);
191 strcpy(buf+len, "c");
192
193 return buf;
194}
195
196
197/* Given a pathname for a Python source file, its time of last
198 modification, and a pathname for a compiled file, check whether the
199 compiled file represents the same version of the source. If so,
200 return a FILE pointer for the compiled file, positioned just after
201 the header; if not, return NULL.
202 Doesn't set an exception. */
203
204static FILE *
205check_compiled_module(pathname, mtime, cpathname)
206 char *pathname;
207 long mtime;
208 char *cpathname;
209{
210 FILE *fp;
211 long magic;
212 long pyc_mtime;
213
214 fp = fopen(cpathname, "rb");
215 if (fp == NULL)
216 return NULL;
217 magic = rd_long(fp);
218 if (magic != MAGIC) {
219 if (verbose)
220 fprintf(stderr, "# %s has bad magic\n", cpathname);
221 fclose(fp);
222 return NULL;
223 }
224 pyc_mtime = rd_long(fp);
225 if (pyc_mtime != mtime) {
226 if (verbose)
227 fprintf(stderr, "# %s has bad mtime\n", cpathname);
228 fclose(fp);
229 return NULL;
230 }
231 if (verbose)
232 fprintf(stderr, "# %s matches %s\n", cpathname, pathname);
233 return fp;
234}
235
236
237/* Read a code object from a file and check it for validity */
238
239static codeobject *
240read_compiled_module(fp)
241 FILE *fp;
242{
243 object *co;
244
245 co = rd_object(fp);
246 /* Ugly: rd_object() may return NULL with or without error */
247 if (co == NULL || !is_codeobject(co)) {
248 if (!err_occurred())
249 err_setstr(ImportError,
250 "Non-code object in .pyc file");
251 XDECREF(co);
252 return NULL;
253 }
254 return (codeobject *)co;
255}
256
257
258/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000259 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000260
261static object *
262load_compiled_module(name, cpathname, fp)
263 char *name;
264 char *cpathname;
265 FILE *fp;
266{
267 long magic;
268 codeobject *co;
269 object *m;
270
271 magic = rd_long(fp);
272 if (magic != MAGIC) {
273 err_setstr(ImportError, "Bad magic number in .pyc file");
274 return NULL;
275 }
276 (void) rd_long(fp);
277 co = read_compiled_module(fp);
278 if (co == NULL)
279 return NULL;
280 if (verbose)
281 fprintf(stderr, "import %s # precompiled from %s\n",
282 name, cpathname);
Jack Jansen9c96a921995-02-15 22:57:06 +0000283 m = exec_code_module(name, (object *)co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000284 DECREF(co);
285
286 return m;
287}
288
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000289/* Parse a source file and return the corresponding code object */
290
291static codeobject *
292parse_source_module(pathname, fp)
293 char *pathname;
294 FILE *fp;
295{
296 codeobject *co;
297 node *n;
298
299 n = parse_file(fp, pathname, file_input);
300 if (n == NULL)
301 return NULL;
302 co = compile(n, pathname);
303 freetree(n);
304
305 return co;
306}
307
308
309/* Write a compiled module to a file, placing the time of last
310 modification of its source into the header.
311 Errors are ignored, if a write error occurs an attempt is made to
312 remove the file. */
313
314static void
315write_compiled_module(co, cpathname, mtime)
316 codeobject *co;
317 char *cpathname;
318 long mtime;
319{
320 FILE *fp;
321
322 fp = fopen(cpathname, "wb");
323 if (fp == NULL) {
324 if (verbose)
325 fprintf(stderr,
326 "# can't create %s\n", cpathname);
327 return;
328 }
329 wr_long(MAGIC, fp);
330 /* First write a 0 for mtime */
331 wr_long(0L, fp);
332 wr_object((object *)co, fp);
333 if (ferror(fp)) {
334 if (verbose)
335 fprintf(stderr, "# can't write %s\n", cpathname);
336 /* Don't keep partial file */
337 fclose(fp);
338 (void) unlink(cpathname);
339 return;
340 }
341 /* Now write the true mtime */
342 fseek(fp, 4L, 0);
343 wr_long(mtime, fp);
344 fflush(fp);
345 fclose(fp);
346 if (verbose)
347 fprintf(stderr, "# wrote %s\n", cpathname);
348#ifdef macintosh
Guido van Rossumbe1a6e21996-02-21 15:29:20 +0000349 setfiletype(cpathname, 'Pyth', 'PYC ');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000350#endif
351}
352
353
354/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000355 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
356 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000357
358static object *
359load_source_module(name, pathname, fp)
360 char *name;
361 char *pathname;
362 FILE *fp;
363{
364 long mtime;
365 FILE *fpc;
366 char buf[MAXPATHLEN+1];
367 char *cpathname;
368 codeobject *co;
369 object *m;
370
371 mtime = getmtime(pathname);
372 cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1);
373 if (cpathname != NULL &&
374 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
375 co = read_compiled_module(fpc);
376 fclose(fpc);
377 if (co == NULL)
378 return NULL;
379 if (verbose)
380 fprintf(stderr, "import %s # precompiled from %s\n",
381 name, cpathname);
382 }
383 else {
384 co = parse_source_module(pathname, fp);
385 if (co == NULL)
386 return NULL;
387 if (verbose)
388 fprintf(stderr, "import %s # from %s\n",
389 name, pathname);
390 write_compiled_module(co, cpathname, mtime);
391 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000392 m = exec_code_module(name, (object *)co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000393 DECREF(co);
394
395 return m;
396}
397
398
399/* Search the path (default sys.path) for a module. Return the
400 corresponding filedescr struct, and (via return arguments) the
401 pathname and an open file. Return NULL if the module is not found. */
402
403static struct filedescr *
404find_module(name, path, buf, buflen, p_fp)
405 char *name;
406 object *path;
407 /* Output parameters: */
408 char *buf;
409 int buflen;
410 FILE **p_fp;
411{
412 int i, npath, len, namelen;
413 struct filedescr *fdp;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000414 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000415
Guido van Rossuma5a3db71996-04-09 02:39:59 +0000416#ifdef NT
417 if ((fp=PyWin_FindRegisteredModule(name, &fdp, buf, buflen))!=NULL) {
418 *p_fp = fp;
419 return fdp;
420 }
421#endif
422
423
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000424 if (path == NULL)
425 path = sysget("path");
426 if (path == NULL || !is_listobject(path)) {
427 err_setstr(ImportError,
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000428 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000429 return NULL;
430 }
431 npath = getlistsize(path);
432 namelen = strlen(name);
433 for (i = 0; i < npath; i++) {
434 object *v = getlistitem(path, i);
435 if (!is_stringobject(v))
436 continue;
437 len = getstringsize(v);
438 if (len + 2 + namelen + import_maxsuffixsize >= buflen)
439 continue; /* Too long */
440 strcpy(buf, getstringvalue(v));
441 if (strlen(buf) != len)
442 continue; /* v contains '\0' */
Jack Jansen9c96a921995-02-15 22:57:06 +0000443#ifdef macintosh
444 if ( PyMac_FindResourceModule(name, buf) ) {
445 static struct filedescr resfiledescr = { "", "", PY_RESOURCE};
446
447 return &resfiledescr;
448 }
449#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000450 if (len > 0 && buf[len-1] != SEP)
451 buf[len++] = SEP;
452 strcpy(buf+len, name);
453 len += namelen;
454 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
455 strcpy(buf+len, fdp->suffix);
456 if (verbose > 1)
457 fprintf(stderr, "# trying %s\n", buf);
458 fp = fopen(buf, fdp->mode);
459 if (fp != NULL)
460 break;
461 }
462 if (fp != NULL)
463 break;
464 }
465 if (fp == NULL) {
466 char buf[256];
467 sprintf(buf, "No module named %.200s", name);
468 err_setstr(ImportError, buf);
469 return NULL;
470 }
471
472 *p_fp = fp;
473 return fdp;
474}
475
476
477/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000478 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000479
480static object *
481load_module(name)
482 char *name;
483{
484 char buf[MAXPATHLEN+1];
485 struct filedescr *fdp;
486 FILE *fp = NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000487 object *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000488
489 fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp);
490 if (fdp == NULL)
491 return NULL;
492
493 switch (fdp->type) {
494
495 case PY_SOURCE:
496 m = load_source_module(name, buf, fp);
497 break;
498
499 case PY_COMPILED:
500 m = load_compiled_module(name, buf, fp);
501 break;
502
503 case C_EXTENSION:
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000504 m = load_dynamic_module(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000505 break;
506
Jack Jansen9c96a921995-02-15 22:57:06 +0000507#ifdef macintosh
508 case PY_RESOURCE:
509 m = PyMac_LoadResourceModule(name, buf);
510 break;
511#endif
512
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000513 default:
514 err_setstr(SystemError,
515 "find_module returned unexpected result");
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000516 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000517
518 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000519 if ( fp )
520 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000521
522 return m;
523}
524
525
526/* Initialize a built-in module.
527 Return 1 for succes, 0 if the module is not found, and -1 with
528 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000529
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000530static int
531init_builtin(name)
532 char *name;
533{
534 int i;
535 for (i = 0; inittab[i].name != NULL; i++) {
536 if (strcmp(name, inittab[i].name) == 0) {
Guido van Rossum74e6a111994-08-29 12:54:38 +0000537 if (inittab[i].initfunc == NULL) {
538 err_setstr(ImportError,
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000539 "Cannot re-init internal module");
Guido van Rossum74e6a111994-08-29 12:54:38 +0000540 return -1;
541 }
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +0000542 if (verbose)
543 fprintf(stderr, "import %s # builtin\n",
544 name);
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000545 (*inittab[i].initfunc)();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000546 if (err_occurred())
547 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000548 return 1;
549 }
550 }
551 return 0;
552}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000553
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000554
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000555/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000556
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000557extern struct frozen {
558 char *name;
559 char *code;
560 int size;
561} frozen_modules[];
562
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000563static struct frozen *
564find_frozen(name)
565 char *name;
566{
567 struct frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000568
569 for (p = frozen_modules; ; p++) {
570 if (p->name == NULL)
571 return NULL;
572 if (strcmp(p->name, name) == 0)
573 break;
574 }
575 return p;
576}
577
578static object *
579get_frozen_object(name)
580 char *name;
581{
582 struct frozen *p = find_frozen(name);
583
584 if (p == NULL) {
585 err_setstr(ImportError, "No such frozen object");
586 return NULL;
587 }
588 return rds_object(p->code, p->size);
589}
590
591/* Initialize a frozen module.
592 Return 1 for succes, 0 if the module is not found, and -1 with
593 an exception set if the initialization failed.
594 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +0000595
596int
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000597init_frozen(name)
598 char *name;
599{
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000600 struct frozen *p = find_frozen(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000601 object *co;
602 object *m;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000603
604 if (p == NULL)
605 return 0;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000606 if (verbose)
607 fprintf(stderr, "import %s # frozen\n", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000608 co = rds_object(p->code, p->size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000609 if (co == NULL)
610 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000611 if (!is_codeobject(co)) {
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000612 DECREF(co);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000613 err_setstr(TypeError, "frozen object is not a code object");
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000614 return -1;
615 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000616 m = exec_code_module(name, co);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000617 DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000618 if (m == NULL)
619 return -1;
620 DECREF(m);
621 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000622}
Guido van Rossum74e6a111994-08-29 12:54:38 +0000623
624
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000625/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000626 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +0000627
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000628object *
629import_module(name)
630 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000631{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000632 object *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000633
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000634 if (import_modules == NULL) {
635 err_setstr(SystemError, "sys.modules has been deleted");
636 return NULL;
637 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000638 if ((m = dictlookup(import_modules, name)) != NULL) {
639 INCREF(m);
640 }
641 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000642 int i;
643 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
644 if (i < 0)
645 return NULL;
646 if ((m = dictlookup(import_modules, name)) == NULL) {
647 if (err_occurred() == NULL)
648 err_setstr(SystemError,
649 "built-in module not initialized properly");
650 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000651 else
652 INCREF(m);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000653 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000654 else
655 m = load_module(name);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000656 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000657
658 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000659}
660
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000661
662/* Re-import a module of any kind and return its module object, WITH
663 INCREMENTED REFERENCE COUNT */
664
665object *
666reload_module(m)
667 object *m;
668{
669 char *name;
670 int i;
671
672 if (m == NULL || !is_moduleobject(m)) {
673 err_setstr(TypeError, "reload() argument must be module");
674 return NULL;
675 }
676 name = getmodulename(m);
677 if (name == NULL)
678 return NULL;
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000679 if (import_modules == NULL) {
680 err_setstr(SystemError, "sys.modules has been deleted");
681 return NULL;
682 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000683 if (m != dictlookup(import_modules, name)) {
684 err_setstr(ImportError, "reload() module not in sys.modules");
685 return NULL;
686 }
687 /* Check for built-in and frozen modules */
688 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
689 if (i < 0)
690 return NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000691 INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000692 }
693 else
694 m = load_module(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000695 return m;
696}
697
698
699/* Module 'imp' provides Python access to the primitives used for
700 importing modules.
701*/
702
703static object *
704imp_get_magic(self, args)
705 object *self;
706 object *args;
707{
708 char buf[4];
709
710 if (!newgetargs(args, ""))
711 return NULL;
712 buf[0] = (MAGIC >> 0) & 0xff;
713 buf[1] = (MAGIC >> 8) & 0xff;
Guido van Rossum90f0e071995-01-30 12:53:06 +0000714 buf[2] = (MAGIC >> 16) & 0xff;
715 buf[3] = (MAGIC >> 24) & 0xff;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000716
717 return newsizedstringobject(buf, 4);
718}
719
720static object *
721imp_get_suffixes(self, args)
722 object *self;
723 object *args;
724{
725 object *list;
726 struct filedescr *fdp;
727
728 if (!newgetargs(args, ""))
729 return NULL;
730 list = newlistobject(0);
731 if (list == NULL)
732 return NULL;
733 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
734 object *item = mkvalue("ssi",
735 fdp->suffix, fdp->mode, fdp->type);
736 if (item == NULL) {
737 DECREF(list);
738 return NULL;
739 }
740 if (addlistitem(list, item) < 0) {
741 DECREF(list);
742 DECREF(item);
743 return NULL;
744 }
745 DECREF(item);
746 }
747 return list;
748}
749
750static object *
751imp_find_module(self, args)
752 object *self;
753 object *args;
754{
755 extern int fclose PROTO((FILE *));
756 char *name;
757 object *path = NULL;
758 object *fob, *ret;
759 struct filedescr *fdp;
760 char pathname[MAXPATHLEN+1];
761 FILE *fp;
762 if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
763 return NULL;
764 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
765 if (fdp == NULL)
766 return NULL;
767 fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
768 if (fob == NULL) {
769 fclose(fp);
770 return NULL;
771 }
772 ret = mkvalue("Os(ssi)",
773 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
774 DECREF(fob);
775 return ret;
776}
777
778static object *
779imp_init_builtin(self, args)
780 object *self;
781 object *args;
782{
783 char *name;
784 int ret;
785 object *m;
786 if (!newgetargs(args, "s", &name))
787 return NULL;
788 ret = init_builtin(name);
789 if (ret < 0)
790 return NULL;
791 if (ret == 0) {
792 INCREF(None);
793 return None;
794 }
795 m = add_module(name);
796 XINCREF(m);
797 return m;
798}
799
800static object *
801imp_init_frozen(self, args)
802 object *self;
803 object *args;
804{
805 char *name;
806 int ret;
807 object *m;
808 if (!newgetargs(args, "s", &name))
809 return NULL;
810 ret = init_frozen(name);
811 if (ret < 0)
812 return NULL;
813 if (ret == 0) {
814 INCREF(None);
815 return None;
816 }
817 m = add_module(name);
818 XINCREF(m);
819 return m;
820}
821
822static object *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000823imp_get_frozen_object(self, args)
824 object *self;
825 object *args;
826{
827 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +0000828
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000829 if (!newgetargs(args, "s", &name))
830 return NULL;
831 return get_frozen_object(name);
832}
833
834static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000835imp_is_builtin(self, args)
836 object *self;
837 object *args;
838{
839 int i;
840 char *name;
841 if (!newgetargs(args, "s", &name))
842 return NULL;
843 for (i = 0; inittab[i].name != NULL; i++) {
844 if (strcmp(name, inittab[i].name) == 0) {
845 if (inittab[i].initfunc == NULL)
846 return newintobject(-1);
847 else
848 return newintobject(1);
849 }
850 }
851 return newintobject(0);
852}
853
854static object *
855imp_is_frozen(self, args)
856 object *self;
857 object *args;
858{
859 struct frozen *p;
860 char *name;
861 if (!newgetargs(args, "s", &name))
862 return NULL;
863 for (p = frozen_modules; ; p++) {
864 if (p->name == NULL)
865 break;
866 if (strcmp(p->name, name) == 0)
867 return newintobject(1);
868 }
869 return newintobject(0);
870}
871
872static FILE *
873get_file(pathname, fob, mode)
874 char *pathname;
875 object *fob;
876 char *mode;
877{
878 FILE *fp;
879 if (fob == NULL) {
880 fp = fopen(pathname, mode);
881 if (fp == NULL)
882 err_errno(IOError);
883 }
884 else {
885 fp = getfilefile(fob);
886 if (fp == NULL)
887 err_setstr(ValueError, "bad/closed file object");
888 }
889 return fp;
890}
891
892static object *
893imp_load_compiled(self, args)
894 object *self;
895 object *args;
896{
897 char *name;
898 char *pathname;
899 object *fob = NULL;
900 object *m;
901 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000902 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000903 return NULL;
904 fp = get_file(pathname, fob, "rb");
905 if (fp == NULL)
906 return NULL;
907 m = load_compiled_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000908 return m;
909}
910
911static object *
912imp_load_dynamic(self, args)
913 object *self;
914 object *args;
915{
916 char *name;
917 char *pathname;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000918 object *fob = NULL;
919 object *m;
920 FILE *fp = NULL;
921 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000922 return NULL;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000923 if (fob)
924 fp = get_file(pathname, fob, "r");
925 m = load_dynamic_module(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +0000926 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000927}
928
929static object *
930imp_load_source(self, args)
931 object *self;
932 object *args;
933{
934 char *name;
935 char *pathname;
936 object *fob = NULL;
937 object *m;
938 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000939 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000940 return NULL;
941 fp = get_file(pathname, fob, "r");
942 if (fp == NULL)
943 return NULL;
944 m = load_source_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000945 return m;
946}
947
Jack Jansen9c96a921995-02-15 22:57:06 +0000948#ifdef macintosh
949static object *
950imp_load_resource(self, args)
951 object *self;
952 object *args;
953{
954 char *name;
955 char *pathname;
956 object *m;
957
958 if (!newgetargs(args, "ss", &name, &pathname))
959 return NULL;
960 m = PyMac_LoadResourceModule(name, pathname);
961 return m;
962}
963#endif /* macintosh */
964
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000965static object *
966imp_new_module(self, args)
967 object *self;
968 object *args;
969{
970 char *name;
971 if (!newgetargs(args, "s", &name))
972 return NULL;
973 return newmoduleobject(name);
974}
975
976static struct methodlist imp_methods[] = {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000977 {"get_frozen_object", imp_get_frozen_object, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000978 {"get_magic", imp_get_magic, 1},
979 {"get_suffixes", imp_get_suffixes, 1},
980 {"find_module", imp_find_module, 1},
981 {"init_builtin", imp_init_builtin, 1},
982 {"init_frozen", imp_init_frozen, 1},
983 {"is_builtin", imp_is_builtin, 1},
984 {"is_frozen", imp_is_frozen, 1},
985 {"load_compiled", imp_load_compiled, 1},
986 {"load_dynamic", imp_load_dynamic, 1},
987 {"load_source", imp_load_source, 1},
988 {"new_module", imp_new_module, 1},
Jack Jansen9c96a921995-02-15 22:57:06 +0000989#ifdef macintosh
990 {"load_resource", imp_load_resource, 1},
991#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000992 {NULL, NULL} /* sentinel */
993};
994
995void
996initimp()
997{
998 object *m, *d, *v;
999
1000 m = initmodule("imp", imp_methods);
1001 d = getmoduledict(m);
1002
1003 v = newintobject(SEARCH_ERROR);
1004 dictinsert(d, "SEARCH_ERROR", v);
1005 XDECREF(v);
1006
1007 v = newintobject(PY_SOURCE);
1008 dictinsert(d, "PY_SOURCE", v);
1009 XDECREF(v);
1010
1011 v = newintobject(PY_COMPILED);
1012 dictinsert(d, "PY_COMPILED", v);
1013 XDECREF(v);
1014
1015 v = newintobject(C_EXTENSION);
1016 dictinsert(d, "C_EXTENSION", v);
1017 XDECREF(v);
1018
Jack Jansenae12e191995-06-18 20:06:44 +00001019#ifdef macintosh
1020 v = newintobject(PY_RESOURCE);
1021 dictinsert(d, "PY_RESOURCE", v);
1022 XDECREF(v);
1023#endif
1024
1025
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001026 if (err_occurred())
1027 fatal("imp module initialization failed");
1028}