blob: 58a09fdfc9240b1c40be279222cd35e54cb271a7 [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 Rossum0de81bf1995-01-26 00:41:28 +000071 object *tmp = import_modules;
72 import_modules = NULL;
73 /* This deletes all modules from sys.modules.
74 When a module is deallocated, it in turn clears its dictionary,
75 thus hopefully breaking any circular references between modules
76 and between a module's dictionary and its functions.
77 Note that "import" will fail while we are cleaning up.
78 */
79 mappingclear(tmp);
80 DECREF(tmp);
Guido van Rossum3f5da241990-12-20 15:06:42 +000081 }
Guido van Rossum8d15b5d1990-10-26 14:58:58 +000082}
Guido van Rossum7f133ed1991-02-19 12:23:57 +000083
84
Guido van Rossum1ae940a1995-01-02 19:04:15 +000085/* Helper for pythonrun.c -- return magic number */
86
87long
88get_pyc_magic()
89{
90 return MAGIC;
91}
92
93
94/* Helper for sysmodule.c -- return modules dictionary */
95
96object *
97get_modules()
98{
99 return import_modules;
100}
101
102
103/* Get the module object corresponding to a module name.
104 First check the modules dictionary if there's one there,
105 if not, create a new one and insert in in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000106 Because the former action is most common, THIS DOES NOT RETURN A
107 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000108
109object *
110add_module(name)
111 char *name;
112{
113 object *m;
114
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000115 if (import_modules == NULL) {
116 err_setstr(SystemError, "sys.modules has been deleted");
117 return NULL;
118 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000119 if ((m = dictlookup(import_modules, name)) != NULL &&
120 is_moduleobject(m))
121 return m;
122 m = newmoduleobject(name);
123 if (m == NULL)
124 return NULL;
125 if (dictinsert(import_modules, name, m) != 0) {
126 DECREF(m);
127 return NULL;
128 }
129 DECREF(m); /* Yes, it still exists, in modules! */
130
131 return m;
132}
133
134
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000135/* Execute a code object in a module and return the module object
136 WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000137
138static object *
139exec_code_module(name, co)
140 char *name;
141 codeobject *co;
142{
143 object *m, *d, *v;
144
145 m = add_module(name);
146 if (m == NULL)
147 return NULL;
148 d = getmoduledict(m);
Guido van Rossum6135a871995-01-09 17:53:26 +0000149 if (dictlookup(d, "__builtins__") == NULL) {
Guido van Rossum94390ec1995-01-12 11:37:57 +0000150 if (dictinsert(d, "__builtins__", getbuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000151 return NULL;
152 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000153 v = eval_code((codeobject *)co, d, d, d, (object *)NULL);
154 if (v == NULL)
155 return NULL;
156 DECREF(v);
157 INCREF(m);
158
159 return m;
160}
161
162
163/* Given a pathname for a Python source file, fill a buffer with the
164 pathname for the corresponding compiled file. Return the pathname
165 for the compiled file, or NULL if there's no space in the buffer.
166 Doesn't set an exception. */
167
168static char *
169make_compiled_pathname(pathname, buf, buflen)
170 char *pathname;
171 char *buf;
172 int buflen;
173{
174 int len;
175
176 len = strlen(pathname);
177 if (len+2 > buflen)
178 return NULL;
179 strcpy(buf, pathname);
180 strcpy(buf+len, "c");
181
182 return buf;
183}
184
185
186/* Given a pathname for a Python source file, its time of last
187 modification, and a pathname for a compiled file, check whether the
188 compiled file represents the same version of the source. If so,
189 return a FILE pointer for the compiled file, positioned just after
190 the header; if not, return NULL.
191 Doesn't set an exception. */
192
193static FILE *
194check_compiled_module(pathname, mtime, cpathname)
195 char *pathname;
196 long mtime;
197 char *cpathname;
198{
199 FILE *fp;
200 long magic;
201 long pyc_mtime;
202
203 fp = fopen(cpathname, "rb");
204 if (fp == NULL)
205 return NULL;
206 magic = rd_long(fp);
207 if (magic != MAGIC) {
208 if (verbose)
209 fprintf(stderr, "# %s has bad magic\n", cpathname);
210 fclose(fp);
211 return NULL;
212 }
213 pyc_mtime = rd_long(fp);
214 if (pyc_mtime != mtime) {
215 if (verbose)
216 fprintf(stderr, "# %s has bad mtime\n", cpathname);
217 fclose(fp);
218 return NULL;
219 }
220 if (verbose)
221 fprintf(stderr, "# %s matches %s\n", cpathname, pathname);
222 return fp;
223}
224
225
226/* Read a code object from a file and check it for validity */
227
228static codeobject *
229read_compiled_module(fp)
230 FILE *fp;
231{
232 object *co;
233
234 co = rd_object(fp);
235 /* Ugly: rd_object() may return NULL with or without error */
236 if (co == NULL || !is_codeobject(co)) {
237 if (!err_occurred())
238 err_setstr(ImportError,
239 "Non-code object in .pyc file");
240 XDECREF(co);
241 return NULL;
242 }
243 return (codeobject *)co;
244}
245
246
247/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000248 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000249
250static object *
251load_compiled_module(name, cpathname, fp)
252 char *name;
253 char *cpathname;
254 FILE *fp;
255{
256 long magic;
257 codeobject *co;
258 object *m;
259
260 magic = rd_long(fp);
261 if (magic != MAGIC) {
262 err_setstr(ImportError, "Bad magic number in .pyc file");
263 return NULL;
264 }
265 (void) rd_long(fp);
266 co = read_compiled_module(fp);
267 if (co == NULL)
268 return NULL;
269 if (verbose)
270 fprintf(stderr, "import %s # precompiled from %s\n",
271 name, cpathname);
272 m = exec_code_module(name, co);
273 DECREF(co);
274
275 return m;
276}
277
278
279/* Parse a source file and return the corresponding code object */
280
281static codeobject *
282parse_source_module(pathname, fp)
283 char *pathname;
284 FILE *fp;
285{
286 codeobject *co;
287 node *n;
288
289 n = parse_file(fp, pathname, file_input);
290 if (n == NULL)
291 return NULL;
292 co = compile(n, pathname);
293 freetree(n);
294
295 return co;
296}
297
298
299/* Write a compiled module to a file, placing the time of last
300 modification of its source into the header.
301 Errors are ignored, if a write error occurs an attempt is made to
302 remove the file. */
303
304static void
305write_compiled_module(co, cpathname, mtime)
306 codeobject *co;
307 char *cpathname;
308 long mtime;
309{
310 FILE *fp;
311
312 fp = fopen(cpathname, "wb");
313 if (fp == NULL) {
314 if (verbose)
315 fprintf(stderr,
316 "# can't create %s\n", cpathname);
317 return;
318 }
319 wr_long(MAGIC, fp);
320 /* First write a 0 for mtime */
321 wr_long(0L, fp);
322 wr_object((object *)co, fp);
323 if (ferror(fp)) {
324 if (verbose)
325 fprintf(stderr, "# can't write %s\n", cpathname);
326 /* Don't keep partial file */
327 fclose(fp);
328 (void) unlink(cpathname);
329 return;
330 }
331 /* Now write the true mtime */
332 fseek(fp, 4L, 0);
333 wr_long(mtime, fp);
334 fflush(fp);
335 fclose(fp);
336 if (verbose)
337 fprintf(stderr, "# wrote %s\n", cpathname);
338#ifdef macintosh
339 setfiletype(cpathname, 'PYTH', 'PYC ');
340#endif
341}
342
343
344/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000345 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
346 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000347
348static object *
349load_source_module(name, pathname, fp)
350 char *name;
351 char *pathname;
352 FILE *fp;
353{
354 long mtime;
355 FILE *fpc;
356 char buf[MAXPATHLEN+1];
357 char *cpathname;
358 codeobject *co;
359 object *m;
360
361 mtime = getmtime(pathname);
362 cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1);
363 if (cpathname != NULL &&
364 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
365 co = read_compiled_module(fpc);
366 fclose(fpc);
367 if (co == NULL)
368 return NULL;
369 if (verbose)
370 fprintf(stderr, "import %s # precompiled from %s\n",
371 name, cpathname);
372 }
373 else {
374 co = parse_source_module(pathname, fp);
375 if (co == NULL)
376 return NULL;
377 if (verbose)
378 fprintf(stderr, "import %s # from %s\n",
379 name, pathname);
380 write_compiled_module(co, cpathname, mtime);
381 }
382 m = exec_code_module(name, co);
383 DECREF(co);
384
385 return m;
386}
387
388
389/* Search the path (default sys.path) for a module. Return the
390 corresponding filedescr struct, and (via return arguments) the
391 pathname and an open file. Return NULL if the module is not found. */
392
393static struct filedescr *
394find_module(name, path, buf, buflen, p_fp)
395 char *name;
396 object *path;
397 /* Output parameters: */
398 char *buf;
399 int buflen;
400 FILE **p_fp;
401{
402 int i, npath, len, namelen;
403 struct filedescr *fdp;
404 FILE *fp;
405
406 if (path == NULL)
407 path = sysget("path");
408 if (path == NULL || !is_listobject(path)) {
409 err_setstr(ImportError,
410 "module search path must be list of directory names");
411 return NULL;
412 }
413 npath = getlistsize(path);
414 namelen = strlen(name);
415 for (i = 0; i < npath; i++) {
416 object *v = getlistitem(path, i);
417 if (!is_stringobject(v))
418 continue;
419 len = getstringsize(v);
420 if (len + 2 + namelen + import_maxsuffixsize >= buflen)
421 continue; /* Too long */
422 strcpy(buf, getstringvalue(v));
423 if (strlen(buf) != len)
424 continue; /* v contains '\0' */
425 if (len > 0 && buf[len-1] != SEP)
426 buf[len++] = SEP;
427 strcpy(buf+len, name);
428 len += namelen;
429 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
430 strcpy(buf+len, fdp->suffix);
431 if (verbose > 1)
432 fprintf(stderr, "# trying %s\n", buf);
433 fp = fopen(buf, fdp->mode);
434 if (fp != NULL)
435 break;
436 }
437 if (fp != NULL)
438 break;
439 }
440 if (fp == NULL) {
441 char buf[256];
442 sprintf(buf, "No module named %.200s", name);
443 err_setstr(ImportError, buf);
444 return NULL;
445 }
446
447 *p_fp = fp;
448 return fdp;
449}
450
451
452/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000453 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000454
455static object *
456load_module(name)
457 char *name;
458{
459 char buf[MAXPATHLEN+1];
460 struct filedescr *fdp;
461 FILE *fp = NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000462 object *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000463
464 fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp);
465 if (fdp == NULL)
466 return NULL;
467
468 switch (fdp->type) {
469
470 case PY_SOURCE:
471 m = load_source_module(name, buf, fp);
472 break;
473
474 case PY_COMPILED:
475 m = load_compiled_module(name, buf, fp);
476 break;
477
478 case C_EXTENSION:
479 m = load_dynamic_module(name, buf);
480 break;
481
482 default:
483 err_setstr(SystemError,
484 "find_module returned unexpected result");
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000485 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000486
487 }
488 fclose(fp);
489
490 return m;
491}
492
493
494/* Initialize a built-in module.
495 Return 1 for succes, 0 if the module is not found, and -1 with
496 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000497
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000498static int
499init_builtin(name)
500 char *name;
501{
502 int i;
503 for (i = 0; inittab[i].name != NULL; i++) {
504 if (strcmp(name, inittab[i].name) == 0) {
Guido van Rossum74e6a111994-08-29 12:54:38 +0000505 if (inittab[i].initfunc == NULL) {
506 err_setstr(ImportError,
507 "cannot re-init internal module");
508 return -1;
509 }
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +0000510 if (verbose)
511 fprintf(stderr, "import %s # builtin\n",
512 name);
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000513 (*inittab[i].initfunc)();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000514 if (err_occurred())
515 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000516 return 1;
517 }
518 }
519 return 0;
520}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000521
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000522
523/* Initialize a frozen module.
524 Return 1 for succes, 0 if the module is not found, and -1 with
525 an exception set if the initialization failed. */
526
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000527extern struct frozen {
528 char *name;
529 char *code;
530 int size;
531} frozen_modules[];
532
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000533static int
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000534init_frozen(name)
535 char *name;
536{
537 struct frozen *p;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000538 object *co;
539 object *m;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000540 for (p = frozen_modules; ; p++) {
541 if (p->name == NULL)
542 return 0;
543 if (strcmp(p->name, name) == 0)
544 break;
545 }
546 if (verbose)
547 fprintf(stderr, "import %s # frozen\n", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000548 co = rds_object(p->code, p->size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000549 if (co == NULL)
550 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000551 if (!is_codeobject(co)) {
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000552 DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000553 err_setstr(SystemError, "frozen object is not a code object");
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000554 return -1;
555 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000556 m = exec_code_module(name, (codeobject *)co);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000557 DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000558 if (m == NULL)
559 return -1;
560 DECREF(m);
561 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000562}
Guido van Rossum74e6a111994-08-29 12:54:38 +0000563
564
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000565/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000566 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +0000567
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000568object *
569import_module(name)
570 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000571{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000572 object *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000573
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000574 if (import_modules == NULL) {
575 err_setstr(SystemError, "sys.modules has been deleted");
576 return NULL;
577 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000578 if ((m = dictlookup(import_modules, name)) != NULL) {
579 INCREF(m);
580 }
581 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000582 int i;
583 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
584 if (i < 0)
585 return NULL;
586 if ((m = dictlookup(import_modules, name)) == NULL) {
587 if (err_occurred() == NULL)
588 err_setstr(SystemError,
589 "built-in module not initialized properly");
590 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000591 else
592 INCREF(m);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000593 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000594 else
595 m = load_module(name);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000596 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000597
598 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000599}
600
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000601
602/* Re-import a module of any kind and return its module object, WITH
603 INCREMENTED REFERENCE COUNT */
604
605object *
606reload_module(m)
607 object *m;
608{
609 char *name;
610 int i;
611
612 if (m == NULL || !is_moduleobject(m)) {
613 err_setstr(TypeError, "reload() argument must be module");
614 return NULL;
615 }
616 name = getmodulename(m);
617 if (name == NULL)
618 return NULL;
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000619 if (import_modules == NULL) {
620 err_setstr(SystemError, "sys.modules has been deleted");
621 return NULL;
622 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000623 if (m != dictlookup(import_modules, name)) {
624 err_setstr(ImportError, "reload() module not in sys.modules");
625 return NULL;
626 }
627 /* Check for built-in and frozen modules */
628 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
629 if (i < 0)
630 return NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000631 INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000632 }
633 else
634 m = load_module(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000635 return m;
636}
637
638
639/* Module 'imp' provides Python access to the primitives used for
640 importing modules.
641*/
642
643static object *
644imp_get_magic(self, args)
645 object *self;
646 object *args;
647{
648 char buf[4];
649
650 if (!newgetargs(args, ""))
651 return NULL;
652 buf[0] = (MAGIC >> 0) & 0xff;
653 buf[1] = (MAGIC >> 8) & 0xff;
654 buf[3] = (MAGIC >> 16) & 0xff;
655 buf[4] = (MAGIC >> 24) & 0xff;
656
657 return newsizedstringobject(buf, 4);
658}
659
660static object *
661imp_get_suffixes(self, args)
662 object *self;
663 object *args;
664{
665 object *list;
666 struct filedescr *fdp;
667
668 if (!newgetargs(args, ""))
669 return NULL;
670 list = newlistobject(0);
671 if (list == NULL)
672 return NULL;
673 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
674 object *item = mkvalue("ssi",
675 fdp->suffix, fdp->mode, fdp->type);
676 if (item == NULL) {
677 DECREF(list);
678 return NULL;
679 }
680 if (addlistitem(list, item) < 0) {
681 DECREF(list);
682 DECREF(item);
683 return NULL;
684 }
685 DECREF(item);
686 }
687 return list;
688}
689
690static object *
691imp_find_module(self, args)
692 object *self;
693 object *args;
694{
695 extern int fclose PROTO((FILE *));
696 char *name;
697 object *path = NULL;
698 object *fob, *ret;
699 struct filedescr *fdp;
700 char pathname[MAXPATHLEN+1];
701 FILE *fp;
702 if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
703 return NULL;
704 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
705 if (fdp == NULL)
706 return NULL;
707 fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
708 if (fob == NULL) {
709 fclose(fp);
710 return NULL;
711 }
712 ret = mkvalue("Os(ssi)",
713 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
714 DECREF(fob);
715 return ret;
716}
717
718static object *
719imp_init_builtin(self, args)
720 object *self;
721 object *args;
722{
723 char *name;
724 int ret;
725 object *m;
726 if (!newgetargs(args, "s", &name))
727 return NULL;
728 ret = init_builtin(name);
729 if (ret < 0)
730 return NULL;
731 if (ret == 0) {
732 INCREF(None);
733 return None;
734 }
735 m = add_module(name);
736 XINCREF(m);
737 return m;
738}
739
740static object *
741imp_init_frozen(self, args)
742 object *self;
743 object *args;
744{
745 char *name;
746 int ret;
747 object *m;
748 if (!newgetargs(args, "s", &name))
749 return NULL;
750 ret = init_frozen(name);
751 if (ret < 0)
752 return NULL;
753 if (ret == 0) {
754 INCREF(None);
755 return None;
756 }
757 m = add_module(name);
758 XINCREF(m);
759 return m;
760}
761
762static object *
763imp_is_builtin(self, args)
764 object *self;
765 object *args;
766{
767 int i;
768 char *name;
769 if (!newgetargs(args, "s", &name))
770 return NULL;
771 for (i = 0; inittab[i].name != NULL; i++) {
772 if (strcmp(name, inittab[i].name) == 0) {
773 if (inittab[i].initfunc == NULL)
774 return newintobject(-1);
775 else
776 return newintobject(1);
777 }
778 }
779 return newintobject(0);
780}
781
782static object *
783imp_is_frozen(self, args)
784 object *self;
785 object *args;
786{
787 struct frozen *p;
788 char *name;
789 if (!newgetargs(args, "s", &name))
790 return NULL;
791 for (p = frozen_modules; ; p++) {
792 if (p->name == NULL)
793 break;
794 if (strcmp(p->name, name) == 0)
795 return newintobject(1);
796 }
797 return newintobject(0);
798}
799
800static FILE *
801get_file(pathname, fob, mode)
802 char *pathname;
803 object *fob;
804 char *mode;
805{
806 FILE *fp;
807 if (fob == NULL) {
808 fp = fopen(pathname, mode);
809 if (fp == NULL)
810 err_errno(IOError);
811 }
812 else {
813 fp = getfilefile(fob);
814 if (fp == NULL)
815 err_setstr(ValueError, "bad/closed file object");
816 }
817 return fp;
818}
819
820static object *
821imp_load_compiled(self, args)
822 object *self;
823 object *args;
824{
825 char *name;
826 char *pathname;
827 object *fob = NULL;
828 object *m;
829 FILE *fp;
830 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
831 return NULL;
832 fp = get_file(pathname, fob, "rb");
833 if (fp == NULL)
834 return NULL;
835 m = load_compiled_module(name, pathname, fp);
836 if (fob == NULL)
837 fclose(fp);
838 return m;
839}
840
841static object *
842imp_load_dynamic(self, args)
843 object *self;
844 object *args;
845{
846 char *name;
847 char *pathname;
848 object *dummy;
849 if (!newgetargs(args, "ss|O", &name, &pathname, &dummy))
850 return NULL;
851 return load_dynamic_module(name, pathname);
852}
853
854static object *
855imp_load_source(self, args)
856 object *self;
857 object *args;
858{
859 char *name;
860 char *pathname;
861 object *fob = NULL;
862 object *m;
863 FILE *fp;
864 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
865 return NULL;
866 fp = get_file(pathname, fob, "r");
867 if (fp == NULL)
868 return NULL;
869 m = load_source_module(name, pathname, fp);
870 if (fob == NULL)
871 fclose(fp);
872 return m;
873}
874
875static object *
876imp_new_module(self, args)
877 object *self;
878 object *args;
879{
880 char *name;
881 if (!newgetargs(args, "s", &name))
882 return NULL;
883 return newmoduleobject(name);
884}
885
886static struct methodlist imp_methods[] = {
887 {"get_magic", imp_get_magic, 1},
888 {"get_suffixes", imp_get_suffixes, 1},
889 {"find_module", imp_find_module, 1},
890 {"init_builtin", imp_init_builtin, 1},
891 {"init_frozen", imp_init_frozen, 1},
892 {"is_builtin", imp_is_builtin, 1},
893 {"is_frozen", imp_is_frozen, 1},
894 {"load_compiled", imp_load_compiled, 1},
895 {"load_dynamic", imp_load_dynamic, 1},
896 {"load_source", imp_load_source, 1},
897 {"new_module", imp_new_module, 1},
898 {NULL, NULL} /* sentinel */
899};
900
901void
902initimp()
903{
904 object *m, *d, *v;
905
906 m = initmodule("imp", imp_methods);
907 d = getmoduledict(m);
908
909 v = newintobject(SEARCH_ERROR);
910 dictinsert(d, "SEARCH_ERROR", v);
911 XDECREF(v);
912
913 v = newintobject(PY_SOURCE);
914 dictinsert(d, "PY_SOURCE", v);
915 XDECREF(v);
916
917 v = newintobject(PY_COMPILED);
918 dictinsert(d, "PY_COMPILED", v);
919 XDECREF(v);
920
921 v = newintobject(C_EXTENSION);
922 dictinsert(d, "C_EXTENSION", v);
923 XDECREF(v);
924
925 if (err_occurred())
926 fatal("imp module initialization failed");
927}