blob: a0af0507eef3f93d5fe765c2268c0fdf3d2959a5 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum74e6a111994-08-29 12:54:38 +00002Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
Guido van Rossum34679b71993-01-26 13:33:44 +00003Amsterdam, The 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 Rossum3f5da241990-12-20 15:06:42 +000035#include "pythonrun.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000036#include "marshal.h"
37#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000038#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000039#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000040#include "importdl.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000041
Guido van Rossum74e6a111994-08-29 12:54:38 +000042extern int verbose; /* Defined in pythonrun.c */
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +000043
Guido van Rossum74e6a111994-08-29 12:54:38 +000044extern long getmtime(); /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000045
Guido van Rossum6c849691994-09-26 15:47:17 +000046/* Magic word to reject .pyc files generated by other Python versions */
Guido van Rossum74e6a111994-08-29 12:54:38 +000047#define MAGIC 0x999903L /* Increment by one for each incompatible change */
Guido van Rossum3ddee711991-12-16 13:06:34 +000048
Guido van Rossum1ae940a1995-01-02 19:04:15 +000049object *import_modules; /* This becomes sys.modules */
Guido van Rossum3f5da241990-12-20 15:06:42 +000050
Guido van Rossum66f1fa81991-04-03 19:03:52 +000051
Guido van Rossum1ae940a1995-01-02 19:04:15 +000052/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053
54void
55initimport()
56{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000057 if (import_modules != NULL)
58 fatal("duplicate initimport() call");
59 if ((import_modules = newdictobject()) == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +000060 fatal("no mem for dictionary of modules");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061}
62
Guido van Rossum3f5da241990-12-20 15:06:42 +000063
Guido van Rossum1ae940a1995-01-02 19:04:15 +000064/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +000065
Guido van Rossum3f5da241990-12-20 15:06:42 +000066void
67doneimport()
68{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000069 if (import_modules != NULL) {
Guido van Rossum25831651993-05-19 14:50:45 +000070 int pos;
71 object *modname, *module;
Guido van Rossum3f5da241990-12-20 15:06:42 +000072 /* Explicitly erase all modules; this is the safest way
73 to get rid of at least *some* circular dependencies */
Guido van Rossum25831651993-05-19 14:50:45 +000074 pos = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000075 while (mappinggetnext(import_modules,
76 &pos, &modname, &module)) {
Guido van Rossum25831651993-05-19 14:50:45 +000077 if (is_moduleobject(module)) {
78 object *dict;
79 dict = getmoduledict(module);
80 if (dict != NULL && is_dictobject(dict))
81 mappingclear(dict);
Guido van Rossum3f5da241990-12-20 15:06:42 +000082 }
83 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +000084 mappingclear(import_modules);
85 DECREF(import_modules);
Guido van Rossum3f5da241990-12-20 15:06:42 +000086 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +000087 import_modules = NULL;
Guido van Rossum8d15b5d1990-10-26 14:58:58 +000088}
Guido van Rossum7f133ed1991-02-19 12:23:57 +000089
90
Guido van Rossum1ae940a1995-01-02 19:04:15 +000091/* Helper for pythonrun.c -- return magic number */
92
93long
94get_pyc_magic()
95{
96 return MAGIC;
97}
98
99
100/* Helper for sysmodule.c -- return modules dictionary */
101
102object *
103get_modules()
104{
105 return import_modules;
106}
107
108
109/* Get the module object corresponding to a module name.
110 First check the modules dictionary if there's one there,
111 if not, create a new one and insert in in the modules dictionary.
112 Because the former action is most common, this does not return a
113 'new' reference! */
114
115object *
116add_module(name)
117 char *name;
118{
119 object *m;
120
121 if ((m = dictlookup(import_modules, name)) != NULL &&
122 is_moduleobject(m))
123 return m;
124 m = newmoduleobject(name);
125 if (m == NULL)
126 return NULL;
127 if (dictinsert(import_modules, name, m) != 0) {
128 DECREF(m);
129 return NULL;
130 }
131 DECREF(m); /* Yes, it still exists, in modules! */
132
133 return m;
134}
135
136
137/* Execute a code object in a module and return its module object */
138
139static object *
140exec_code_module(name, co)
141 char *name;
142 codeobject *co;
143{
144 object *m, *d, *v;
145
146 m = add_module(name);
147 if (m == NULL)
148 return NULL;
149 d = getmoduledict(m);
150 v = eval_code((codeobject *)co, d, d, d, (object *)NULL);
151 if (v == NULL)
152 return NULL;
153 DECREF(v);
154 INCREF(m);
155
156 return m;
157}
158
159
160/* Given a pathname for a Python source file, fill a buffer with the
161 pathname for the corresponding compiled file. Return the pathname
162 for the compiled file, or NULL if there's no space in the buffer.
163 Doesn't set an exception. */
164
165static char *
166make_compiled_pathname(pathname, buf, buflen)
167 char *pathname;
168 char *buf;
169 int buflen;
170{
171 int len;
172
173 len = strlen(pathname);
174 if (len+2 > buflen)
175 return NULL;
176 strcpy(buf, pathname);
177 strcpy(buf+len, "c");
178
179 return buf;
180}
181
182
183/* Given a pathname for a Python source file, its time of last
184 modification, and a pathname for a compiled file, check whether the
185 compiled file represents the same version of the source. If so,
186 return a FILE pointer for the compiled file, positioned just after
187 the header; if not, return NULL.
188 Doesn't set an exception. */
189
190static FILE *
191check_compiled_module(pathname, mtime, cpathname)
192 char *pathname;
193 long mtime;
194 char *cpathname;
195{
196 FILE *fp;
197 long magic;
198 long pyc_mtime;
199
200 fp = fopen(cpathname, "rb");
201 if (fp == NULL)
202 return NULL;
203 magic = rd_long(fp);
204 if (magic != MAGIC) {
205 if (verbose)
206 fprintf(stderr, "# %s has bad magic\n", cpathname);
207 fclose(fp);
208 return NULL;
209 }
210 pyc_mtime = rd_long(fp);
211 if (pyc_mtime != mtime) {
212 if (verbose)
213 fprintf(stderr, "# %s has bad mtime\n", cpathname);
214 fclose(fp);
215 return NULL;
216 }
217 if (verbose)
218 fprintf(stderr, "# %s matches %s\n", cpathname, pathname);
219 return fp;
220}
221
222
223/* Read a code object from a file and check it for validity */
224
225static codeobject *
226read_compiled_module(fp)
227 FILE *fp;
228{
229 object *co;
230
231 co = rd_object(fp);
232 /* Ugly: rd_object() may return NULL with or without error */
233 if (co == NULL || !is_codeobject(co)) {
234 if (!err_occurred())
235 err_setstr(ImportError,
236 "Non-code object in .pyc file");
237 XDECREF(co);
238 return NULL;
239 }
240 return (codeobject *)co;
241}
242
243
244/* Load a module from a compiled file, execute it, and return its
245 module object */
246
247static object *
248load_compiled_module(name, cpathname, fp)
249 char *name;
250 char *cpathname;
251 FILE *fp;
252{
253 long magic;
254 codeobject *co;
255 object *m;
256
257 magic = rd_long(fp);
258 if (magic != MAGIC) {
259 err_setstr(ImportError, "Bad magic number in .pyc file");
260 return NULL;
261 }
262 (void) rd_long(fp);
263 co = read_compiled_module(fp);
264 if (co == NULL)
265 return NULL;
266 if (verbose)
267 fprintf(stderr, "import %s # precompiled from %s\n",
268 name, cpathname);
269 m = exec_code_module(name, co);
270 DECREF(co);
271
272 return m;
273}
274
275
276/* Parse a source file and return the corresponding code object */
277
278static codeobject *
279parse_source_module(pathname, fp)
280 char *pathname;
281 FILE *fp;
282{
283 codeobject *co;
284 node *n;
285
286 n = parse_file(fp, pathname, file_input);
287 if (n == NULL)
288 return NULL;
289 co = compile(n, pathname);
290 freetree(n);
291
292 return co;
293}
294
295
296/* Write a compiled module to a file, placing the time of last
297 modification of its source into the header.
298 Errors are ignored, if a write error occurs an attempt is made to
299 remove the file. */
300
301static void
302write_compiled_module(co, cpathname, mtime)
303 codeobject *co;
304 char *cpathname;
305 long mtime;
306{
307 FILE *fp;
308
309 fp = fopen(cpathname, "wb");
310 if (fp == NULL) {
311 if (verbose)
312 fprintf(stderr,
313 "# can't create %s\n", cpathname);
314 return;
315 }
316 wr_long(MAGIC, fp);
317 /* First write a 0 for mtime */
318 wr_long(0L, fp);
319 wr_object((object *)co, fp);
320 if (ferror(fp)) {
321 if (verbose)
322 fprintf(stderr, "# can't write %s\n", cpathname);
323 /* Don't keep partial file */
324 fclose(fp);
325 (void) unlink(cpathname);
326 return;
327 }
328 /* Now write the true mtime */
329 fseek(fp, 4L, 0);
330 wr_long(mtime, fp);
331 fflush(fp);
332 fclose(fp);
333 if (verbose)
334 fprintf(stderr, "# wrote %s\n", cpathname);
335#ifdef macintosh
336 setfiletype(cpathname, 'PYTH', 'PYC ');
337#endif
338}
339
340
341/* Load a source module from a given file and return its module
342 object. If there's a matching byte-compiled file, use that
343 instead. */
344
345static object *
346load_source_module(name, pathname, fp)
347 char *name;
348 char *pathname;
349 FILE *fp;
350{
351 long mtime;
352 FILE *fpc;
353 char buf[MAXPATHLEN+1];
354 char *cpathname;
355 codeobject *co;
356 object *m;
357
358 mtime = getmtime(pathname);
359 cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1);
360 if (cpathname != NULL &&
361 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
362 co = read_compiled_module(fpc);
363 fclose(fpc);
364 if (co == NULL)
365 return NULL;
366 if (verbose)
367 fprintf(stderr, "import %s # precompiled from %s\n",
368 name, cpathname);
369 }
370 else {
371 co = parse_source_module(pathname, fp);
372 if (co == NULL)
373 return NULL;
374 if (verbose)
375 fprintf(stderr, "import %s # from %s\n",
376 name, pathname);
377 write_compiled_module(co, cpathname, mtime);
378 }
379 m = exec_code_module(name, co);
380 DECREF(co);
381
382 return m;
383}
384
385
386/* Search the path (default sys.path) for a module. Return the
387 corresponding filedescr struct, and (via return arguments) the
388 pathname and an open file. Return NULL if the module is not found. */
389
390static struct filedescr *
391find_module(name, path, buf, buflen, p_fp)
392 char *name;
393 object *path;
394 /* Output parameters: */
395 char *buf;
396 int buflen;
397 FILE **p_fp;
398{
399 int i, npath, len, namelen;
400 struct filedescr *fdp;
401 FILE *fp;
402
403 if (path == NULL)
404 path = sysget("path");
405 if (path == NULL || !is_listobject(path)) {
406 err_setstr(ImportError,
407 "module search path must be list of directory names");
408 return NULL;
409 }
410 npath = getlistsize(path);
411 namelen = strlen(name);
412 for (i = 0; i < npath; i++) {
413 object *v = getlistitem(path, i);
414 if (!is_stringobject(v))
415 continue;
416 len = getstringsize(v);
417 if (len + 2 + namelen + import_maxsuffixsize >= buflen)
418 continue; /* Too long */
419 strcpy(buf, getstringvalue(v));
420 if (strlen(buf) != len)
421 continue; /* v contains '\0' */
422 if (len > 0 && buf[len-1] != SEP)
423 buf[len++] = SEP;
424 strcpy(buf+len, name);
425 len += namelen;
426 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
427 strcpy(buf+len, fdp->suffix);
428 if (verbose > 1)
429 fprintf(stderr, "# trying %s\n", buf);
430 fp = fopen(buf, fdp->mode);
431 if (fp != NULL)
432 break;
433 }
434 if (fp != NULL)
435 break;
436 }
437 if (fp == NULL) {
438 char buf[256];
439 sprintf(buf, "No module named %.200s", name);
440 err_setstr(ImportError, buf);
441 return NULL;
442 }
443
444 *p_fp = fp;
445 return fdp;
446}
447
448
449/* Load an external module using the default search path and return
450 its module object */
451
452static object *
453load_module(name)
454 char *name;
455{
456 char buf[MAXPATHLEN+1];
457 struct filedescr *fdp;
458 FILE *fp = NULL;
459 object *m = NULL;
460
461 fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp);
462 if (fdp == NULL)
463 return NULL;
464
465 switch (fdp->type) {
466
467 case PY_SOURCE:
468 m = load_source_module(name, buf, fp);
469 break;
470
471 case PY_COMPILED:
472 m = load_compiled_module(name, buf, fp);
473 break;
474
475 case C_EXTENSION:
476 m = load_dynamic_module(name, buf);
477 break;
478
479 default:
480 err_setstr(SystemError,
481 "find_module returned unexpected result");
482
483 }
484 fclose(fp);
485
486 return m;
487}
488
489
490/* Initialize a built-in module.
491 Return 1 for succes, 0 if the module is not found, and -1 with
492 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000493
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000494static int
495init_builtin(name)
496 char *name;
497{
498 int i;
499 for (i = 0; inittab[i].name != NULL; i++) {
500 if (strcmp(name, inittab[i].name) == 0) {
Guido van Rossum74e6a111994-08-29 12:54:38 +0000501 if (inittab[i].initfunc == NULL) {
502 err_setstr(ImportError,
503 "cannot re-init internal module");
504 return -1;
505 }
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +0000506 if (verbose)
507 fprintf(stderr, "import %s # builtin\n",
508 name);
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000509 (*inittab[i].initfunc)();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000510 if (err_occurred())
511 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000512 return 1;
513 }
514 }
515 return 0;
516}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000517
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000518
519/* Initialize a frozen module.
520 Return 1 for succes, 0 if the module is not found, and -1 with
521 an exception set if the initialization failed. */
522
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000523extern struct frozen {
524 char *name;
525 char *code;
526 int size;
527} frozen_modules[];
528
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000529static int
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000530init_frozen(name)
531 char *name;
532{
533 struct frozen *p;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000534 object *co;
535 object *m;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000536 for (p = frozen_modules; ; p++) {
537 if (p->name == NULL)
538 return 0;
539 if (strcmp(p->name, name) == 0)
540 break;
541 }
542 if (verbose)
543 fprintf(stderr, "import %s # frozen\n", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000544 co = rds_object(p->code, p->size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000545 if (co == NULL)
546 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000547 if (!is_codeobject(co)) {
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000548 DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000549 err_setstr(SystemError, "frozen object is not a code object");
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000550 return -1;
551 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000552 m = exec_code_module(name, (codeobject *)co);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000553 DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000554 return m == NULL ? -1 : 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000555}
Guido van Rossum74e6a111994-08-29 12:54:38 +0000556
557
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000558/* Import a module, either built-in, frozen, or external, and return
559 its module object */
Guido van Rossum74e6a111994-08-29 12:54:38 +0000560
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000561object *
562import_module(name)
563 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000564{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000565 object *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000566
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000567 if ((m = dictlookup(import_modules, name)) == NULL) {
568 int i;
569 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
570 if (i < 0)
571 return NULL;
572 if ((m = dictlookup(import_modules, name)) == NULL) {
573 if (err_occurred() == NULL)
574 err_setstr(SystemError,
575 "built-in module not initialized properly");
576 }
Guido van Rossum74e6a111994-08-29 12:54:38 +0000577 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000578 else
579 m = load_module(name);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000580 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000581
582 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000583}
584
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000585
586/* Re-import a module of any kind and return its module object, WITH
587 INCREMENTED REFERENCE COUNT */
588
589object *
590reload_module(m)
591 object *m;
592{
593 char *name;
594 int i;
595
596 if (m == NULL || !is_moduleobject(m)) {
597 err_setstr(TypeError, "reload() argument must be module");
598 return NULL;
599 }
600 name = getmodulename(m);
601 if (name == NULL)
602 return NULL;
603 if (m != dictlookup(import_modules, name)) {
604 err_setstr(ImportError, "reload() module not in sys.modules");
605 return NULL;
606 }
607 /* Check for built-in and frozen modules */
608 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
609 if (i < 0)
610 return NULL;
611 }
612 else
613 m = load_module(name);
614 XINCREF(m);
615 return m;
616}
617
618
619/* Module 'imp' provides Python access to the primitives used for
620 importing modules.
621*/
622
623static object *
624imp_get_magic(self, args)
625 object *self;
626 object *args;
627{
628 char buf[4];
629
630 if (!newgetargs(args, ""))
631 return NULL;
632 buf[0] = (MAGIC >> 0) & 0xff;
633 buf[1] = (MAGIC >> 8) & 0xff;
634 buf[3] = (MAGIC >> 16) & 0xff;
635 buf[4] = (MAGIC >> 24) & 0xff;
636
637 return newsizedstringobject(buf, 4);
638}
639
640static object *
641imp_get_suffixes(self, args)
642 object *self;
643 object *args;
644{
645 object *list;
646 struct filedescr *fdp;
647
648 if (!newgetargs(args, ""))
649 return NULL;
650 list = newlistobject(0);
651 if (list == NULL)
652 return NULL;
653 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
654 object *item = mkvalue("ssi",
655 fdp->suffix, fdp->mode, fdp->type);
656 if (item == NULL) {
657 DECREF(list);
658 return NULL;
659 }
660 if (addlistitem(list, item) < 0) {
661 DECREF(list);
662 DECREF(item);
663 return NULL;
664 }
665 DECREF(item);
666 }
667 return list;
668}
669
670static object *
671imp_find_module(self, args)
672 object *self;
673 object *args;
674{
675 extern int fclose PROTO((FILE *));
676 char *name;
677 object *path = NULL;
678 object *fob, *ret;
679 struct filedescr *fdp;
680 char pathname[MAXPATHLEN+1];
681 FILE *fp;
682 if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
683 return NULL;
684 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
685 if (fdp == NULL)
686 return NULL;
687 fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
688 if (fob == NULL) {
689 fclose(fp);
690 return NULL;
691 }
692 ret = mkvalue("Os(ssi)",
693 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
694 DECREF(fob);
695 return ret;
696}
697
698static object *
699imp_init_builtin(self, args)
700 object *self;
701 object *args;
702{
703 char *name;
704 int ret;
705 object *m;
706 if (!newgetargs(args, "s", &name))
707 return NULL;
708 ret = init_builtin(name);
709 if (ret < 0)
710 return NULL;
711 if (ret == 0) {
712 INCREF(None);
713 return None;
714 }
715 m = add_module(name);
716 XINCREF(m);
717 return m;
718}
719
720static object *
721imp_init_frozen(self, args)
722 object *self;
723 object *args;
724{
725 char *name;
726 int ret;
727 object *m;
728 if (!newgetargs(args, "s", &name))
729 return NULL;
730 ret = init_frozen(name);
731 if (ret < 0)
732 return NULL;
733 if (ret == 0) {
734 INCREF(None);
735 return None;
736 }
737 m = add_module(name);
738 XINCREF(m);
739 return m;
740}
741
742static object *
743imp_is_builtin(self, args)
744 object *self;
745 object *args;
746{
747 int i;
748 char *name;
749 if (!newgetargs(args, "s", &name))
750 return NULL;
751 for (i = 0; inittab[i].name != NULL; i++) {
752 if (strcmp(name, inittab[i].name) == 0) {
753 if (inittab[i].initfunc == NULL)
754 return newintobject(-1);
755 else
756 return newintobject(1);
757 }
758 }
759 return newintobject(0);
760}
761
762static object *
763imp_is_frozen(self, args)
764 object *self;
765 object *args;
766{
767 struct frozen *p;
768 char *name;
769 if (!newgetargs(args, "s", &name))
770 return NULL;
771 for (p = frozen_modules; ; p++) {
772 if (p->name == NULL)
773 break;
774 if (strcmp(p->name, name) == 0)
775 return newintobject(1);
776 }
777 return newintobject(0);
778}
779
780static FILE *
781get_file(pathname, fob, mode)
782 char *pathname;
783 object *fob;
784 char *mode;
785{
786 FILE *fp;
787 if (fob == NULL) {
788 fp = fopen(pathname, mode);
789 if (fp == NULL)
790 err_errno(IOError);
791 }
792 else {
793 fp = getfilefile(fob);
794 if (fp == NULL)
795 err_setstr(ValueError, "bad/closed file object");
796 }
797 return fp;
798}
799
800static object *
801imp_load_compiled(self, args)
802 object *self;
803 object *args;
804{
805 char *name;
806 char *pathname;
807 object *fob = NULL;
808 object *m;
809 FILE *fp;
810 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
811 return NULL;
812 fp = get_file(pathname, fob, "rb");
813 if (fp == NULL)
814 return NULL;
815 m = load_compiled_module(name, pathname, fp);
816 if (fob == NULL)
817 fclose(fp);
818 return m;
819}
820
821static object *
822imp_load_dynamic(self, args)
823 object *self;
824 object *args;
825{
826 char *name;
827 char *pathname;
828 object *dummy;
829 if (!newgetargs(args, "ss|O", &name, &pathname, &dummy))
830 return NULL;
831 return load_dynamic_module(name, pathname);
832}
833
834static object *
835imp_load_source(self, args)
836 object *self;
837 object *args;
838{
839 char *name;
840 char *pathname;
841 object *fob = NULL;
842 object *m;
843 FILE *fp;
844 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
845 return NULL;
846 fp = get_file(pathname, fob, "r");
847 if (fp == NULL)
848 return NULL;
849 m = load_source_module(name, pathname, fp);
850 if (fob == NULL)
851 fclose(fp);
852 return m;
853}
854
855static object *
856imp_new_module(self, args)
857 object *self;
858 object *args;
859{
860 char *name;
861 if (!newgetargs(args, "s", &name))
862 return NULL;
863 return newmoduleobject(name);
864}
865
866static struct methodlist imp_methods[] = {
867 {"get_magic", imp_get_magic, 1},
868 {"get_suffixes", imp_get_suffixes, 1},
869 {"find_module", imp_find_module, 1},
870 {"init_builtin", imp_init_builtin, 1},
871 {"init_frozen", imp_init_frozen, 1},
872 {"is_builtin", imp_is_builtin, 1},
873 {"is_frozen", imp_is_frozen, 1},
874 {"load_compiled", imp_load_compiled, 1},
875 {"load_dynamic", imp_load_dynamic, 1},
876 {"load_source", imp_load_source, 1},
877 {"new_module", imp_new_module, 1},
878 {NULL, NULL} /* sentinel */
879};
880
881void
882initimp()
883{
884 object *m, *d, *v;
885
886 m = initmodule("imp", imp_methods);
887 d = getmoduledict(m);
888
889 v = newintobject(SEARCH_ERROR);
890 dictinsert(d, "SEARCH_ERROR", v);
891 XDECREF(v);
892
893 v = newintobject(PY_SOURCE);
894 dictinsert(d, "PY_SOURCE", v);
895 XDECREF(v);
896
897 v = newintobject(PY_COMPILED);
898 dictinsert(d, "PY_COMPILED", v);
899 XDECREF(v);
900
901 v = newintobject(C_EXTENSION);
902 dictinsert(d, "C_EXTENSION", v);
903 XDECREF(v);
904
905 if (err_occurred())
906 fatal("imp module initialization failed");
907}