blob: 1d9ea35c5bfc5607ee4888c46c5a15e78bea3961 [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"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000042
Guido van Rossum74e6a111994-08-29 12:54:38 +000043extern int verbose; /* Defined in pythonrun.c */
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +000044
Guido van Rossum74e6a111994-08-29 12:54:38 +000045extern long getmtime(); /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000046
Guido van Rossum6c849691994-09-26 15:47:17 +000047/* Magic word to reject .pyc files generated by other Python versions */
Guido van Rossum74e6a111994-08-29 12:54:38 +000048#define MAGIC 0x999903L /* Increment by one for each incompatible change */
Guido van Rossum3ddee711991-12-16 13:06:34 +000049
Guido van Rossum1ae940a1995-01-02 19:04:15 +000050object *import_modules; /* This becomes sys.modules */
Guido van Rossum3f5da241990-12-20 15:06:42 +000051
Guido van Rossum66f1fa81991-04-03 19:03:52 +000052
Guido van Rossum1ae940a1995-01-02 19:04:15 +000053/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054
55void
56initimport()
57{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000058 if (import_modules != NULL)
59 fatal("duplicate initimport() call");
60 if ((import_modules = newdictobject()) == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +000061 fatal("no mem for dictionary of modules");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062}
63
Guido van Rossum3f5da241990-12-20 15:06:42 +000064
Guido van Rossum1ae940a1995-01-02 19:04:15 +000065/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +000066
Guido van Rossum3f5da241990-12-20 15:06:42 +000067void
68doneimport()
69{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000070 if (import_modules != NULL) {
Guido van Rossum25831651993-05-19 14:50:45 +000071 int pos;
72 object *modname, *module;
Guido van Rossum3f5da241990-12-20 15:06:42 +000073 /* Explicitly erase all modules; this is the safest way
74 to get rid of at least *some* circular dependencies */
Guido van Rossum25831651993-05-19 14:50:45 +000075 pos = 0;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000076 while (mappinggetnext(import_modules,
77 &pos, &modname, &module)) {
Guido van Rossum25831651993-05-19 14:50:45 +000078 if (is_moduleobject(module)) {
79 object *dict;
80 dict = getmoduledict(module);
81 if (dict != NULL && is_dictobject(dict))
82 mappingclear(dict);
Guido van Rossum3f5da241990-12-20 15:06:42 +000083 }
84 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +000085 mappingclear(import_modules);
86 DECREF(import_modules);
Guido van Rossum3f5da241990-12-20 15:06:42 +000087 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +000088 import_modules = NULL;
Guido van Rossum8d15b5d1990-10-26 14:58:58 +000089}
Guido van Rossum7f133ed1991-02-19 12:23:57 +000090
91
Guido van Rossum1ae940a1995-01-02 19:04:15 +000092/* Helper for pythonrun.c -- return magic number */
93
94long
95get_pyc_magic()
96{
97 return MAGIC;
98}
99
100
101/* Helper for sysmodule.c -- return modules dictionary */
102
103object *
104get_modules()
105{
106 return import_modules;
107}
108
109
110/* Get the module object corresponding to a module name.
111 First check the modules dictionary if there's one there,
112 if not, create a new one and insert in in the modules dictionary.
113 Because the former action is most common, this does not return a
114 'new' reference! */
115
116object *
117add_module(name)
118 char *name;
119{
120 object *m;
121
122 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
138/* Execute a code object in a module and return its module object */
139
140static object *
141exec_code_module(name, co)
142 char *name;
143 codeobject *co;
144{
145 object *m, *d, *v;
146
147 m = add_module(name);
148 if (m == NULL)
149 return NULL;
150 d = getmoduledict(m);
Guido van Rossum6135a871995-01-09 17:53:26 +0000151 if (dictlookup(d, "__builtins__") == NULL) {
152 if (dictinsert(d, "__builtins__", getbuiltindict()) != 0)
153 return NULL;
154 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000155 v = eval_code((codeobject *)co, d, d, d, (object *)NULL);
156 if (v == NULL)
157 return NULL;
158 DECREF(v);
159 INCREF(m);
160
161 return m;
162}
163
164
165/* Given a pathname for a Python source file, fill a buffer with the
166 pathname for the corresponding compiled file. Return the pathname
167 for the compiled file, or NULL if there's no space in the buffer.
168 Doesn't set an exception. */
169
170static char *
171make_compiled_pathname(pathname, buf, buflen)
172 char *pathname;
173 char *buf;
174 int buflen;
175{
176 int len;
177
178 len = strlen(pathname);
179 if (len+2 > buflen)
180 return NULL;
181 strcpy(buf, pathname);
182 strcpy(buf+len, "c");
183
184 return buf;
185}
186
187
188/* Given a pathname for a Python source file, its time of last
189 modification, and a pathname for a compiled file, check whether the
190 compiled file represents the same version of the source. If so,
191 return a FILE pointer for the compiled file, positioned just after
192 the header; if not, return NULL.
193 Doesn't set an exception. */
194
195static FILE *
196check_compiled_module(pathname, mtime, cpathname)
197 char *pathname;
198 long mtime;
199 char *cpathname;
200{
201 FILE *fp;
202 long magic;
203 long pyc_mtime;
204
205 fp = fopen(cpathname, "rb");
206 if (fp == NULL)
207 return NULL;
208 magic = rd_long(fp);
209 if (magic != MAGIC) {
210 if (verbose)
211 fprintf(stderr, "# %s has bad magic\n", cpathname);
212 fclose(fp);
213 return NULL;
214 }
215 pyc_mtime = rd_long(fp);
216 if (pyc_mtime != mtime) {
217 if (verbose)
218 fprintf(stderr, "# %s has bad mtime\n", cpathname);
219 fclose(fp);
220 return NULL;
221 }
222 if (verbose)
223 fprintf(stderr, "# %s matches %s\n", cpathname, pathname);
224 return fp;
225}
226
227
228/* Read a code object from a file and check it for validity */
229
230static codeobject *
231read_compiled_module(fp)
232 FILE *fp;
233{
234 object *co;
235
236 co = rd_object(fp);
237 /* Ugly: rd_object() may return NULL with or without error */
238 if (co == NULL || !is_codeobject(co)) {
239 if (!err_occurred())
240 err_setstr(ImportError,
241 "Non-code object in .pyc file");
242 XDECREF(co);
243 return NULL;
244 }
245 return (codeobject *)co;
246}
247
248
249/* Load a module from a compiled file, execute it, and return its
250 module object */
251
252static object *
253load_compiled_module(name, cpathname, fp)
254 char *name;
255 char *cpathname;
256 FILE *fp;
257{
258 long magic;
259 codeobject *co;
260 object *m;
261
262 magic = rd_long(fp);
263 if (magic != MAGIC) {
264 err_setstr(ImportError, "Bad magic number in .pyc file");
265 return NULL;
266 }
267 (void) rd_long(fp);
268 co = read_compiled_module(fp);
269 if (co == NULL)
270 return NULL;
271 if (verbose)
272 fprintf(stderr, "import %s # precompiled from %s\n",
273 name, cpathname);
274 m = exec_code_module(name, co);
275 DECREF(co);
276
277 return m;
278}
279
280
281/* 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
347 object. If there's a matching byte-compiled file, use that
348 instead. */
349
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 }
384 m = exec_code_module(name, co);
385 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' */
427 if (len > 0 && buf[len-1] != SEP)
428 buf[len++] = SEP;
429 strcpy(buf+len, name);
430 len += namelen;
431 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
432 strcpy(buf+len, fdp->suffix);
433 if (verbose > 1)
434 fprintf(stderr, "# trying %s\n", buf);
435 fp = fopen(buf, fdp->mode);
436 if (fp != NULL)
437 break;
438 }
439 if (fp != NULL)
440 break;
441 }
442 if (fp == NULL) {
443 char buf[256];
444 sprintf(buf, "No module named %.200s", name);
445 err_setstr(ImportError, buf);
446 return NULL;
447 }
448
449 *p_fp = fp;
450 return fdp;
451}
452
453
454/* Load an external module using the default search path and return
455 its module object */
456
457static object *
458load_module(name)
459 char *name;
460{
461 char buf[MAXPATHLEN+1];
462 struct filedescr *fdp;
463 FILE *fp = NULL;
464 object *m = NULL;
465
466 fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp);
467 if (fdp == NULL)
468 return NULL;
469
470 switch (fdp->type) {
471
472 case PY_SOURCE:
473 m = load_source_module(name, buf, fp);
474 break;
475
476 case PY_COMPILED:
477 m = load_compiled_module(name, buf, fp);
478 break;
479
480 case C_EXTENSION:
481 m = load_dynamic_module(name, buf);
482 break;
483
484 default:
485 err_setstr(SystemError,
486 "find_module returned unexpected result");
487
488 }
489 fclose(fp);
490
491 return m;
492}
493
494
495/* Initialize a built-in module.
496 Return 1 for succes, 0 if the module is not found, and -1 with
497 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000498
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000499static int
500init_builtin(name)
501 char *name;
502{
503 int i;
504 for (i = 0; inittab[i].name != NULL; i++) {
505 if (strcmp(name, inittab[i].name) == 0) {
Guido van Rossum74e6a111994-08-29 12:54:38 +0000506 if (inittab[i].initfunc == NULL) {
507 err_setstr(ImportError,
508 "cannot re-init internal module");
509 return -1;
510 }
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +0000511 if (verbose)
512 fprintf(stderr, "import %s # builtin\n",
513 name);
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000514 (*inittab[i].initfunc)();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000515 if (err_occurred())
516 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000517 return 1;
518 }
519 }
520 return 0;
521}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000522
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000523
524/* Initialize a frozen module.
525 Return 1 for succes, 0 if the module is not found, and -1 with
526 an exception set if the initialization failed. */
527
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000528extern struct frozen {
529 char *name;
530 char *code;
531 int size;
532} frozen_modules[];
533
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000534static int
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000535init_frozen(name)
536 char *name;
537{
538 struct frozen *p;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000539 object *co;
540 object *m;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000541 for (p = frozen_modules; ; p++) {
542 if (p->name == NULL)
543 return 0;
544 if (strcmp(p->name, name) == 0)
545 break;
546 }
547 if (verbose)
548 fprintf(stderr, "import %s # frozen\n", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000549 co = rds_object(p->code, p->size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000550 if (co == NULL)
551 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000552 if (!is_codeobject(co)) {
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000553 DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000554 err_setstr(SystemError, "frozen object is not a code object");
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000555 return -1;
556 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000557 m = exec_code_module(name, (codeobject *)co);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000558 DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000559 return m == NULL ? -1 : 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000560}
Guido van Rossum74e6a111994-08-29 12:54:38 +0000561
562
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000563/* Import a module, either built-in, frozen, or external, and return
564 its module object */
Guido van Rossum74e6a111994-08-29 12:54:38 +0000565
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000566object *
567import_module(name)
568 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000569{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000570 object *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000571
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000572 if ((m = dictlookup(import_modules, name)) == NULL) {
573 int i;
574 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
575 if (i < 0)
576 return NULL;
577 if ((m = dictlookup(import_modules, name)) == NULL) {
578 if (err_occurred() == NULL)
579 err_setstr(SystemError,
580 "built-in module not initialized properly");
581 }
Guido van Rossum74e6a111994-08-29 12:54:38 +0000582 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000583 else
584 m = load_module(name);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000585 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000586
587 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000588}
589
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000590
591/* Re-import a module of any kind and return its module object, WITH
592 INCREMENTED REFERENCE COUNT */
593
594object *
595reload_module(m)
596 object *m;
597{
598 char *name;
599 int i;
600
601 if (m == NULL || !is_moduleobject(m)) {
602 err_setstr(TypeError, "reload() argument must be module");
603 return NULL;
604 }
605 name = getmodulename(m);
606 if (name == NULL)
607 return NULL;
608 if (m != dictlookup(import_modules, name)) {
609 err_setstr(ImportError, "reload() module not in sys.modules");
610 return NULL;
611 }
612 /* Check for built-in and frozen modules */
613 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
614 if (i < 0)
615 return NULL;
616 }
617 else
618 m = load_module(name);
619 XINCREF(m);
620 return m;
621}
622
623
624/* Module 'imp' provides Python access to the primitives used for
625 importing modules.
626*/
627
628static object *
629imp_get_magic(self, args)
630 object *self;
631 object *args;
632{
633 char buf[4];
634
635 if (!newgetargs(args, ""))
636 return NULL;
637 buf[0] = (MAGIC >> 0) & 0xff;
638 buf[1] = (MAGIC >> 8) & 0xff;
639 buf[3] = (MAGIC >> 16) & 0xff;
640 buf[4] = (MAGIC >> 24) & 0xff;
641
642 return newsizedstringobject(buf, 4);
643}
644
645static object *
646imp_get_suffixes(self, args)
647 object *self;
648 object *args;
649{
650 object *list;
651 struct filedescr *fdp;
652
653 if (!newgetargs(args, ""))
654 return NULL;
655 list = newlistobject(0);
656 if (list == NULL)
657 return NULL;
658 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
659 object *item = mkvalue("ssi",
660 fdp->suffix, fdp->mode, fdp->type);
661 if (item == NULL) {
662 DECREF(list);
663 return NULL;
664 }
665 if (addlistitem(list, item) < 0) {
666 DECREF(list);
667 DECREF(item);
668 return NULL;
669 }
670 DECREF(item);
671 }
672 return list;
673}
674
675static object *
676imp_find_module(self, args)
677 object *self;
678 object *args;
679{
680 extern int fclose PROTO((FILE *));
681 char *name;
682 object *path = NULL;
683 object *fob, *ret;
684 struct filedescr *fdp;
685 char pathname[MAXPATHLEN+1];
686 FILE *fp;
687 if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
688 return NULL;
689 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
690 if (fdp == NULL)
691 return NULL;
692 fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
693 if (fob == NULL) {
694 fclose(fp);
695 return NULL;
696 }
697 ret = mkvalue("Os(ssi)",
698 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
699 DECREF(fob);
700 return ret;
701}
702
703static object *
704imp_init_builtin(self, args)
705 object *self;
706 object *args;
707{
708 char *name;
709 int ret;
710 object *m;
711 if (!newgetargs(args, "s", &name))
712 return NULL;
713 ret = init_builtin(name);
714 if (ret < 0)
715 return NULL;
716 if (ret == 0) {
717 INCREF(None);
718 return None;
719 }
720 m = add_module(name);
721 XINCREF(m);
722 return m;
723}
724
725static object *
726imp_init_frozen(self, args)
727 object *self;
728 object *args;
729{
730 char *name;
731 int ret;
732 object *m;
733 if (!newgetargs(args, "s", &name))
734 return NULL;
735 ret = init_frozen(name);
736 if (ret < 0)
737 return NULL;
738 if (ret == 0) {
739 INCREF(None);
740 return None;
741 }
742 m = add_module(name);
743 XINCREF(m);
744 return m;
745}
746
747static object *
748imp_is_builtin(self, args)
749 object *self;
750 object *args;
751{
752 int i;
753 char *name;
754 if (!newgetargs(args, "s", &name))
755 return NULL;
756 for (i = 0; inittab[i].name != NULL; i++) {
757 if (strcmp(name, inittab[i].name) == 0) {
758 if (inittab[i].initfunc == NULL)
759 return newintobject(-1);
760 else
761 return newintobject(1);
762 }
763 }
764 return newintobject(0);
765}
766
767static object *
768imp_is_frozen(self, args)
769 object *self;
770 object *args;
771{
772 struct frozen *p;
773 char *name;
774 if (!newgetargs(args, "s", &name))
775 return NULL;
776 for (p = frozen_modules; ; p++) {
777 if (p->name == NULL)
778 break;
779 if (strcmp(p->name, name) == 0)
780 return newintobject(1);
781 }
782 return newintobject(0);
783}
784
785static FILE *
786get_file(pathname, fob, mode)
787 char *pathname;
788 object *fob;
789 char *mode;
790{
791 FILE *fp;
792 if (fob == NULL) {
793 fp = fopen(pathname, mode);
794 if (fp == NULL)
795 err_errno(IOError);
796 }
797 else {
798 fp = getfilefile(fob);
799 if (fp == NULL)
800 err_setstr(ValueError, "bad/closed file object");
801 }
802 return fp;
803}
804
805static object *
806imp_load_compiled(self, args)
807 object *self;
808 object *args;
809{
810 char *name;
811 char *pathname;
812 object *fob = NULL;
813 object *m;
814 FILE *fp;
815 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
816 return NULL;
817 fp = get_file(pathname, fob, "rb");
818 if (fp == NULL)
819 return NULL;
820 m = load_compiled_module(name, pathname, fp);
821 if (fob == NULL)
822 fclose(fp);
823 return m;
824}
825
826static object *
827imp_load_dynamic(self, args)
828 object *self;
829 object *args;
830{
831 char *name;
832 char *pathname;
833 object *dummy;
834 if (!newgetargs(args, "ss|O", &name, &pathname, &dummy))
835 return NULL;
836 return load_dynamic_module(name, pathname);
837}
838
839static object *
840imp_load_source(self, args)
841 object *self;
842 object *args;
843{
844 char *name;
845 char *pathname;
846 object *fob = NULL;
847 object *m;
848 FILE *fp;
849 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
850 return NULL;
851 fp = get_file(pathname, fob, "r");
852 if (fp == NULL)
853 return NULL;
854 m = load_source_module(name, pathname, fp);
855 if (fob == NULL)
856 fclose(fp);
857 return m;
858}
859
860static object *
861imp_new_module(self, args)
862 object *self;
863 object *args;
864{
865 char *name;
866 if (!newgetargs(args, "s", &name))
867 return NULL;
868 return newmoduleobject(name);
869}
870
871static struct methodlist imp_methods[] = {
872 {"get_magic", imp_get_magic, 1},
873 {"get_suffixes", imp_get_suffixes, 1},
874 {"find_module", imp_find_module, 1},
875 {"init_builtin", imp_init_builtin, 1},
876 {"init_frozen", imp_init_frozen, 1},
877 {"is_builtin", imp_is_builtin, 1},
878 {"is_frozen", imp_is_frozen, 1},
879 {"load_compiled", imp_load_compiled, 1},
880 {"load_dynamic", imp_load_dynamic, 1},
881 {"load_source", imp_load_source, 1},
882 {"new_module", imp_new_module, 1},
883 {NULL, NULL} /* sentinel */
884};
885
886void
887initimp()
888{
889 object *m, *d, *v;
890
891 m = initmodule("imp", imp_methods);
892 d = getmoduledict(m);
893
894 v = newintobject(SEARCH_ERROR);
895 dictinsert(d, "SEARCH_ERROR", v);
896 XDECREF(v);
897
898 v = newintobject(PY_SOURCE);
899 dictinsert(d, "PY_SOURCE", v);
900 XDECREF(v);
901
902 v = newintobject(PY_COMPILED);
903 dictinsert(d, "PY_COMPILED", v);
904 XDECREF(v);
905
906 v = newintobject(C_EXTENSION);
907 dictinsert(d, "C_EXTENSION", v);
908 XDECREF(v);
909
910 if (err_occurred())
911 fatal("imp module initialization failed");
912}