blob: 05d2c557bd199106811bd1d3e122b46194b4daa7 [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
43#include "macglue.h"
44#endif
Guido van Rossumc405b7b1991-06-04 19:39:42 +000045
Guido van Rossum74e6a111994-08-29 12:54:38 +000046extern int verbose; /* Defined in pythonrun.c */
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +000047
Guido van Rossum74e6a111994-08-29 12:54:38 +000048extern long getmtime(); /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000049
Guido van Rossum6c849691994-09-26 15:47:17 +000050/* Magic word to reject .pyc files generated by other Python versions */
Guido van Rossum74e6a111994-08-29 12:54:38 +000051#define MAGIC 0x999903L /* Increment by one for each incompatible change */
Guido van Rossum3ddee711991-12-16 13:06:34 +000052
Guido van Rossum1ae940a1995-01-02 19:04:15 +000053object *import_modules; /* This becomes sys.modules */
Guido van Rossum3f5da241990-12-20 15:06:42 +000054
Guido van Rossum66f1fa81991-04-03 19:03:52 +000055
Guido van Rossum1ae940a1995-01-02 19:04:15 +000056/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057
58void
59initimport()
60{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000061 if (import_modules != NULL)
62 fatal("duplicate initimport() call");
63 if ((import_modules = newdictobject()) == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +000064 fatal("no mem for dictionary of modules");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065}
66
Guido van Rossum3f5da241990-12-20 15:06:42 +000067
Guido van Rossum1ae940a1995-01-02 19:04:15 +000068/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +000069
Guido van Rossum3f5da241990-12-20 15:06:42 +000070void
71doneimport()
72{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000073 if (import_modules != NULL) {
Guido van Rossum0de81bf1995-01-26 00:41:28 +000074 object *tmp = import_modules;
75 import_modules = NULL;
76 /* This deletes all modules from sys.modules.
77 When a module is deallocated, it in turn clears its dictionary,
78 thus hopefully breaking any circular references between modules
79 and between a module's dictionary and its functions.
80 Note that "import" will fail while we are cleaning up.
81 */
82 mappingclear(tmp);
83 DECREF(tmp);
Guido van Rossum3f5da241990-12-20 15:06:42 +000084 }
Guido van Rossum8d15b5d1990-10-26 14:58:58 +000085}
Guido van Rossum7f133ed1991-02-19 12:23:57 +000086
87
Guido van Rossum1ae940a1995-01-02 19:04:15 +000088/* Helper for pythonrun.c -- return magic number */
89
90long
91get_pyc_magic()
92{
93 return MAGIC;
94}
95
96
97/* Helper for sysmodule.c -- return modules dictionary */
98
99object *
100get_modules()
101{
102 return import_modules;
103}
104
105
106/* Get the module object corresponding to a module name.
107 First check the modules dictionary if there's one there,
108 if not, create a new one and insert in in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000109 Because the former action is most common, THIS DOES NOT RETURN A
110 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000111
112object *
113add_module(name)
114 char *name;
115{
116 object *m;
117
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000118 if (import_modules == NULL) {
119 err_setstr(SystemError, "sys.modules has been deleted");
120 return NULL;
121 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000122 if ((m = dictlookup(import_modules, name)) != NULL &&
123 is_moduleobject(m))
124 return m;
125 m = newmoduleobject(name);
126 if (m == NULL)
127 return NULL;
128 if (dictinsert(import_modules, name, m) != 0) {
129 DECREF(m);
130 return NULL;
131 }
132 DECREF(m); /* Yes, it still exists, in modules! */
133
134 return m;
135}
136
137
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000138/* Execute a code object in a module and return the module object
139 WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000140
Jack Jansen9c96a921995-02-15 22:57:06 +0000141object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000142exec_code_module(name, co)
143 char *name;
Jack Jansen9c96a921995-02-15 22:57:06 +0000144 object *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000145{
146 object *m, *d, *v;
147
148 m = add_module(name);
149 if (m == NULL)
150 return NULL;
151 d = getmoduledict(m);
Guido van Rossum6135a871995-01-09 17:53:26 +0000152 if (dictlookup(d, "__builtins__") == NULL) {
Guido van Rossum94390ec1995-01-12 11:37:57 +0000153 if (dictinsert(d, "__builtins__", getbuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000154 return NULL;
155 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000156 v = eval_code((codeobject *)co, d, d, d, (object *)NULL);
157 if (v == NULL)
158 return NULL;
159 DECREF(v);
160 INCREF(m);
161
162 return m;
163}
164
165
166/* Given a pathname for a Python source file, fill a buffer with the
167 pathname for the corresponding compiled file. Return the pathname
168 for the compiled file, or NULL if there's no space in the buffer.
169 Doesn't set an exception. */
170
171static char *
172make_compiled_pathname(pathname, buf, buflen)
173 char *pathname;
174 char *buf;
175 int buflen;
176{
177 int len;
178
179 len = strlen(pathname);
180 if (len+2 > buflen)
181 return NULL;
182 strcpy(buf, pathname);
183 strcpy(buf+len, "c");
184
185 return buf;
186}
187
188
189/* Given a pathname for a Python source file, its time of last
190 modification, and a pathname for a compiled file, check whether the
191 compiled file represents the same version of the source. If so,
192 return a FILE pointer for the compiled file, positioned just after
193 the header; if not, return NULL.
194 Doesn't set an exception. */
195
196static FILE *
197check_compiled_module(pathname, mtime, cpathname)
198 char *pathname;
199 long mtime;
200 char *cpathname;
201{
202 FILE *fp;
203 long magic;
204 long pyc_mtime;
205
206 fp = fopen(cpathname, "rb");
207 if (fp == NULL)
208 return NULL;
209 magic = rd_long(fp);
210 if (magic != MAGIC) {
211 if (verbose)
212 fprintf(stderr, "# %s has bad magic\n", cpathname);
213 fclose(fp);
214 return NULL;
215 }
216 pyc_mtime = rd_long(fp);
217 if (pyc_mtime != mtime) {
218 if (verbose)
219 fprintf(stderr, "# %s has bad mtime\n", cpathname);
220 fclose(fp);
221 return NULL;
222 }
223 if (verbose)
224 fprintf(stderr, "# %s matches %s\n", cpathname, pathname);
225 return fp;
226}
227
228
229/* Read a code object from a file and check it for validity */
230
231static codeobject *
232read_compiled_module(fp)
233 FILE *fp;
234{
235 object *co;
236
237 co = rd_object(fp);
238 /* Ugly: rd_object() may return NULL with or without error */
239 if (co == NULL || !is_codeobject(co)) {
240 if (!err_occurred())
241 err_setstr(ImportError,
242 "Non-code object in .pyc file");
243 XDECREF(co);
244 return NULL;
245 }
246 return (codeobject *)co;
247}
248
249
250/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000251 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000252
253static object *
254load_compiled_module(name, cpathname, fp)
255 char *name;
256 char *cpathname;
257 FILE *fp;
258{
259 long magic;
260 codeobject *co;
261 object *m;
262
263 magic = rd_long(fp);
264 if (magic != MAGIC) {
265 err_setstr(ImportError, "Bad magic number in .pyc file");
266 return NULL;
267 }
268 (void) rd_long(fp);
269 co = read_compiled_module(fp);
270 if (co == NULL)
271 return NULL;
272 if (verbose)
273 fprintf(stderr, "import %s # precompiled from %s\n",
274 name, cpathname);
Jack Jansen9c96a921995-02-15 22:57:06 +0000275 m = exec_code_module(name, (object *)co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000276 DECREF(co);
277
278 return m;
279}
280
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000281/* Parse a source file and return the corresponding code object */
282
283static codeobject *
284parse_source_module(pathname, fp)
285 char *pathname;
286 FILE *fp;
287{
288 codeobject *co;
289 node *n;
290
291 n = parse_file(fp, pathname, file_input);
292 if (n == NULL)
293 return NULL;
294 co = compile(n, pathname);
295 freetree(n);
296
297 return co;
298}
299
300
301/* Write a compiled module to a file, placing the time of last
302 modification of its source into the header.
303 Errors are ignored, if a write error occurs an attempt is made to
304 remove the file. */
305
306static void
307write_compiled_module(co, cpathname, mtime)
308 codeobject *co;
309 char *cpathname;
310 long mtime;
311{
312 FILE *fp;
313
314 fp = fopen(cpathname, "wb");
315 if (fp == NULL) {
316 if (verbose)
317 fprintf(stderr,
318 "# can't create %s\n", cpathname);
319 return;
320 }
321 wr_long(MAGIC, fp);
322 /* First write a 0 for mtime */
323 wr_long(0L, fp);
324 wr_object((object *)co, fp);
325 if (ferror(fp)) {
326 if (verbose)
327 fprintf(stderr, "# can't write %s\n", cpathname);
328 /* Don't keep partial file */
329 fclose(fp);
330 (void) unlink(cpathname);
331 return;
332 }
333 /* Now write the true mtime */
334 fseek(fp, 4L, 0);
335 wr_long(mtime, fp);
336 fflush(fp);
337 fclose(fp);
338 if (verbose)
339 fprintf(stderr, "# wrote %s\n", cpathname);
340#ifdef macintosh
341 setfiletype(cpathname, 'PYTH', 'PYC ');
342#endif
343}
344
345
346/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000347 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
348 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000349
350static object *
351load_source_module(name, pathname, fp)
352 char *name;
353 char *pathname;
354 FILE *fp;
355{
356 long mtime;
357 FILE *fpc;
358 char buf[MAXPATHLEN+1];
359 char *cpathname;
360 codeobject *co;
361 object *m;
362
363 mtime = getmtime(pathname);
364 cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1);
365 if (cpathname != NULL &&
366 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
367 co = read_compiled_module(fpc);
368 fclose(fpc);
369 if (co == NULL)
370 return NULL;
371 if (verbose)
372 fprintf(stderr, "import %s # precompiled from %s\n",
373 name, cpathname);
374 }
375 else {
376 co = parse_source_module(pathname, fp);
377 if (co == NULL)
378 return NULL;
379 if (verbose)
380 fprintf(stderr, "import %s # from %s\n",
381 name, pathname);
382 write_compiled_module(co, cpathname, mtime);
383 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000384 m = exec_code_module(name, (object *)co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000385 DECREF(co);
386
387 return m;
388}
389
390
391/* Search the path (default sys.path) for a module. Return the
392 corresponding filedescr struct, and (via return arguments) the
393 pathname and an open file. Return NULL if the module is not found. */
394
395static struct filedescr *
396find_module(name, path, buf, buflen, p_fp)
397 char *name;
398 object *path;
399 /* Output parameters: */
400 char *buf;
401 int buflen;
402 FILE **p_fp;
403{
404 int i, npath, len, namelen;
405 struct filedescr *fdp;
406 FILE *fp;
407
408 if (path == NULL)
409 path = sysget("path");
410 if (path == NULL || !is_listobject(path)) {
411 err_setstr(ImportError,
412 "module search path must be list of directory names");
413 return NULL;
414 }
415 npath = getlistsize(path);
416 namelen = strlen(name);
417 for (i = 0; i < npath; i++) {
418 object *v = getlistitem(path, i);
419 if (!is_stringobject(v))
420 continue;
421 len = getstringsize(v);
422 if (len + 2 + namelen + import_maxsuffixsize >= buflen)
423 continue; /* Too long */
424 strcpy(buf, getstringvalue(v));
425 if (strlen(buf) != len)
426 continue; /* v contains '\0' */
Jack Jansen9c96a921995-02-15 22:57:06 +0000427#ifdef macintosh
428 if ( PyMac_FindResourceModule(name, buf) ) {
429 static struct filedescr resfiledescr = { "", "", PY_RESOURCE};
430
431 return &resfiledescr;
432 }
433#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000434 if (len > 0 && buf[len-1] != SEP)
435 buf[len++] = SEP;
436 strcpy(buf+len, name);
437 len += namelen;
438 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
439 strcpy(buf+len, fdp->suffix);
440 if (verbose > 1)
441 fprintf(stderr, "# trying %s\n", buf);
442 fp = fopen(buf, fdp->mode);
443 if (fp != NULL)
444 break;
445 }
446 if (fp != NULL)
447 break;
448 }
449 if (fp == NULL) {
450 char buf[256];
451 sprintf(buf, "No module named %.200s", name);
452 err_setstr(ImportError, buf);
453 return NULL;
454 }
455
456 *p_fp = fp;
457 return fdp;
458}
459
460
461/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000462 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000463
464static object *
465load_module(name)
466 char *name;
467{
468 char buf[MAXPATHLEN+1];
469 struct filedescr *fdp;
470 FILE *fp = NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000471 object *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000472
473 fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp);
474 if (fdp == NULL)
475 return NULL;
476
477 switch (fdp->type) {
478
479 case PY_SOURCE:
480 m = load_source_module(name, buf, fp);
481 break;
482
483 case PY_COMPILED:
484 m = load_compiled_module(name, buf, fp);
485 break;
486
487 case C_EXTENSION:
488 m = load_dynamic_module(name, buf);
489 break;
490
Jack Jansen9c96a921995-02-15 22:57:06 +0000491#ifdef macintosh
492 case PY_RESOURCE:
493 m = PyMac_LoadResourceModule(name, buf);
494 break;
495#endif
496
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000497 default:
498 err_setstr(SystemError,
499 "find_module returned unexpected result");
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000500 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000501
502 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000503 if ( fp )
504 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000505
506 return m;
507}
508
509
510/* Initialize a built-in module.
511 Return 1 for succes, 0 if the module is not found, and -1 with
512 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000513
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000514static int
515init_builtin(name)
516 char *name;
517{
518 int i;
519 for (i = 0; inittab[i].name != NULL; i++) {
520 if (strcmp(name, inittab[i].name) == 0) {
Guido van Rossum74e6a111994-08-29 12:54:38 +0000521 if (inittab[i].initfunc == NULL) {
522 err_setstr(ImportError,
523 "cannot re-init internal module");
524 return -1;
525 }
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +0000526 if (verbose)
527 fprintf(stderr, "import %s # builtin\n",
528 name);
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000529 (*inittab[i].initfunc)();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000530 if (err_occurred())
531 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000532 return 1;
533 }
534 }
535 return 0;
536}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000537
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000538
539/* Initialize a frozen module.
540 Return 1 for succes, 0 if the module is not found, and -1 with
541 an exception set if the initialization failed. */
542
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000543extern struct frozen {
544 char *name;
545 char *code;
546 int size;
547} frozen_modules[];
548
Guido van Rossum0b344901995-02-07 15:35:27 +0000549/* This function is also used from frozenmain.c */
550
551int
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000552init_frozen(name)
553 char *name;
554{
555 struct frozen *p;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000556 object *co;
557 object *m;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000558 for (p = frozen_modules; ; p++) {
559 if (p->name == NULL)
560 return 0;
561 if (strcmp(p->name, name) == 0)
562 break;
563 }
564 if (verbose)
565 fprintf(stderr, "import %s # frozen\n", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000566 co = rds_object(p->code, p->size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000567 if (co == NULL)
568 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000569 if (!is_codeobject(co)) {
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000570 DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000571 err_setstr(SystemError, "frozen object is not a code object");
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000572 return -1;
573 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000574 m = exec_code_module(name, co);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000575 DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000576 if (m == NULL)
577 return -1;
578 DECREF(m);
579 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000580}
Guido van Rossum74e6a111994-08-29 12:54:38 +0000581
582
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000583/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000584 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +0000585
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000586object *
587import_module(name)
588 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000589{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000590 object *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000591
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000592 if (import_modules == NULL) {
593 err_setstr(SystemError, "sys.modules has been deleted");
594 return NULL;
595 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000596 if ((m = dictlookup(import_modules, name)) != NULL) {
597 INCREF(m);
598 }
599 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000600 int i;
601 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
602 if (i < 0)
603 return NULL;
604 if ((m = dictlookup(import_modules, name)) == NULL) {
605 if (err_occurred() == NULL)
606 err_setstr(SystemError,
607 "built-in module not initialized properly");
608 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000609 else
610 INCREF(m);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000611 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000612 else
613 m = load_module(name);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000614 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000615
616 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000617}
618
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000619
620/* Re-import a module of any kind and return its module object, WITH
621 INCREMENTED REFERENCE COUNT */
622
623object *
624reload_module(m)
625 object *m;
626{
627 char *name;
628 int i;
629
630 if (m == NULL || !is_moduleobject(m)) {
631 err_setstr(TypeError, "reload() argument must be module");
632 return NULL;
633 }
634 name = getmodulename(m);
635 if (name == NULL)
636 return NULL;
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 Rossum1ae940a1995-01-02 19:04:15 +0000641 if (m != dictlookup(import_modules, name)) {
642 err_setstr(ImportError, "reload() module not in sys.modules");
643 return NULL;
644 }
645 /* Check for built-in and frozen modules */
646 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
647 if (i < 0)
648 return NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000649 INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000650 }
651 else
652 m = load_module(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000653 return m;
654}
655
656
657/* Module 'imp' provides Python access to the primitives used for
658 importing modules.
659*/
660
661static object *
662imp_get_magic(self, args)
663 object *self;
664 object *args;
665{
666 char buf[4];
667
668 if (!newgetargs(args, ""))
669 return NULL;
670 buf[0] = (MAGIC >> 0) & 0xff;
671 buf[1] = (MAGIC >> 8) & 0xff;
Guido van Rossum90f0e071995-01-30 12:53:06 +0000672 buf[2] = (MAGIC >> 16) & 0xff;
673 buf[3] = (MAGIC >> 24) & 0xff;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000674
675 return newsizedstringobject(buf, 4);
676}
677
678static object *
679imp_get_suffixes(self, args)
680 object *self;
681 object *args;
682{
683 object *list;
684 struct filedescr *fdp;
685
686 if (!newgetargs(args, ""))
687 return NULL;
688 list = newlistobject(0);
689 if (list == NULL)
690 return NULL;
691 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
692 object *item = mkvalue("ssi",
693 fdp->suffix, fdp->mode, fdp->type);
694 if (item == NULL) {
695 DECREF(list);
696 return NULL;
697 }
698 if (addlistitem(list, item) < 0) {
699 DECREF(list);
700 DECREF(item);
701 return NULL;
702 }
703 DECREF(item);
704 }
705 return list;
706}
707
708static object *
709imp_find_module(self, args)
710 object *self;
711 object *args;
712{
713 extern int fclose PROTO((FILE *));
714 char *name;
715 object *path = NULL;
716 object *fob, *ret;
717 struct filedescr *fdp;
718 char pathname[MAXPATHLEN+1];
719 FILE *fp;
720 if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
721 return NULL;
722 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
723 if (fdp == NULL)
724 return NULL;
725 fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
726 if (fob == NULL) {
727 fclose(fp);
728 return NULL;
729 }
730 ret = mkvalue("Os(ssi)",
731 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
732 DECREF(fob);
733 return ret;
734}
735
736static object *
737imp_init_builtin(self, args)
738 object *self;
739 object *args;
740{
741 char *name;
742 int ret;
743 object *m;
744 if (!newgetargs(args, "s", &name))
745 return NULL;
746 ret = init_builtin(name);
747 if (ret < 0)
748 return NULL;
749 if (ret == 0) {
750 INCREF(None);
751 return None;
752 }
753 m = add_module(name);
754 XINCREF(m);
755 return m;
756}
757
758static object *
759imp_init_frozen(self, args)
760 object *self;
761 object *args;
762{
763 char *name;
764 int ret;
765 object *m;
766 if (!newgetargs(args, "s", &name))
767 return NULL;
768 ret = init_frozen(name);
769 if (ret < 0)
770 return NULL;
771 if (ret == 0) {
772 INCREF(None);
773 return None;
774 }
775 m = add_module(name);
776 XINCREF(m);
777 return m;
778}
779
780static object *
781imp_is_builtin(self, args)
782 object *self;
783 object *args;
784{
785 int i;
786 char *name;
787 if (!newgetargs(args, "s", &name))
788 return NULL;
789 for (i = 0; inittab[i].name != NULL; i++) {
790 if (strcmp(name, inittab[i].name) == 0) {
791 if (inittab[i].initfunc == NULL)
792 return newintobject(-1);
793 else
794 return newintobject(1);
795 }
796 }
797 return newintobject(0);
798}
799
800static object *
801imp_is_frozen(self, args)
802 object *self;
803 object *args;
804{
805 struct frozen *p;
806 char *name;
807 if (!newgetargs(args, "s", &name))
808 return NULL;
809 for (p = frozen_modules; ; p++) {
810 if (p->name == NULL)
811 break;
812 if (strcmp(p->name, name) == 0)
813 return newintobject(1);
814 }
815 return newintobject(0);
816}
817
818static FILE *
819get_file(pathname, fob, mode)
820 char *pathname;
821 object *fob;
822 char *mode;
823{
824 FILE *fp;
825 if (fob == NULL) {
826 fp = fopen(pathname, mode);
827 if (fp == NULL)
828 err_errno(IOError);
829 }
830 else {
831 fp = getfilefile(fob);
832 if (fp == NULL)
833 err_setstr(ValueError, "bad/closed file object");
834 }
835 return fp;
836}
837
838static object *
839imp_load_compiled(self, args)
840 object *self;
841 object *args;
842{
843 char *name;
844 char *pathname;
845 object *fob = NULL;
846 object *m;
847 FILE *fp;
848 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
849 return NULL;
850 fp = get_file(pathname, fob, "rb");
851 if (fp == NULL)
852 return NULL;
853 m = load_compiled_module(name, pathname, fp);
854 if (fob == NULL)
855 fclose(fp);
856 return m;
857}
858
859static object *
860imp_load_dynamic(self, args)
861 object *self;
862 object *args;
863{
864 char *name;
865 char *pathname;
866 object *dummy;
867 if (!newgetargs(args, "ss|O", &name, &pathname, &dummy))
868 return NULL;
869 return load_dynamic_module(name, pathname);
870}
871
872static object *
873imp_load_source(self, args)
874 object *self;
875 object *args;
876{
877 char *name;
878 char *pathname;
879 object *fob = NULL;
880 object *m;
881 FILE *fp;
882 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
883 return NULL;
884 fp = get_file(pathname, fob, "r");
885 if (fp == NULL)
886 return NULL;
887 m = load_source_module(name, pathname, fp);
888 if (fob == NULL)
889 fclose(fp);
890 return m;
891}
892
Jack Jansen9c96a921995-02-15 22:57:06 +0000893#ifdef macintosh
894static object *
895imp_load_resource(self, args)
896 object *self;
897 object *args;
898{
899 char *name;
900 char *pathname;
901 object *m;
902
903 if (!newgetargs(args, "ss", &name, &pathname))
904 return NULL;
905 m = PyMac_LoadResourceModule(name, pathname);
906 return m;
907}
908#endif /* macintosh */
909
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000910static object *
911imp_new_module(self, args)
912 object *self;
913 object *args;
914{
915 char *name;
916 if (!newgetargs(args, "s", &name))
917 return NULL;
918 return newmoduleobject(name);
919}
920
921static struct methodlist imp_methods[] = {
922 {"get_magic", imp_get_magic, 1},
923 {"get_suffixes", imp_get_suffixes, 1},
924 {"find_module", imp_find_module, 1},
925 {"init_builtin", imp_init_builtin, 1},
926 {"init_frozen", imp_init_frozen, 1},
927 {"is_builtin", imp_is_builtin, 1},
928 {"is_frozen", imp_is_frozen, 1},
929 {"load_compiled", imp_load_compiled, 1},
930 {"load_dynamic", imp_load_dynamic, 1},
931 {"load_source", imp_load_source, 1},
932 {"new_module", imp_new_module, 1},
Jack Jansen9c96a921995-02-15 22:57:06 +0000933#ifdef macintosh
934 {"load_resource", imp_load_resource, 1},
935#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000936 {NULL, NULL} /* sentinel */
937};
938
939void
940initimp()
941{
942 object *m, *d, *v;
943
944 m = initmodule("imp", imp_methods);
945 d = getmoduledict(m);
946
947 v = newintobject(SEARCH_ERROR);
948 dictinsert(d, "SEARCH_ERROR", v);
949 XDECREF(v);
950
951 v = newintobject(PY_SOURCE);
952 dictinsert(d, "PY_SOURCE", v);
953 XDECREF(v);
954
955 v = newintobject(PY_COMPILED);
956 dictinsert(d, "PY_COMPILED", v);
957 XDECREF(v);
958
959 v = newintobject(C_EXTENSION);
960 dictinsert(d, "C_EXTENSION", v);
961 XDECREF(v);
962
963 if (err_occurred())
964 fatal("imp module initialization failed");
965}