blob: 9219c365f25c6b2d6f2d736c04078475b4a5c5ba [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.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000113 Because the former action is most common, THIS DOES NOT RETURN A
114 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000115
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
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
141static object *
142exec_code_module(name, co)
143 char *name;
144 codeobject *co;
145{
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);
275 m = exec_code_module(name, co);
276 DECREF(co);
277
278 return m;
279}
280
281
282/* Parse a source file and return the corresponding code object */
283
284static codeobject *
285parse_source_module(pathname, fp)
286 char *pathname;
287 FILE *fp;
288{
289 codeobject *co;
290 node *n;
291
292 n = parse_file(fp, pathname, file_input);
293 if (n == NULL)
294 return NULL;
295 co = compile(n, pathname);
296 freetree(n);
297
298 return co;
299}
300
301
302/* Write a compiled module to a file, placing the time of last
303 modification of its source into the header.
304 Errors are ignored, if a write error occurs an attempt is made to
305 remove the file. */
306
307static void
308write_compiled_module(co, cpathname, mtime)
309 codeobject *co;
310 char *cpathname;
311 long mtime;
312{
313 FILE *fp;
314
315 fp = fopen(cpathname, "wb");
316 if (fp == NULL) {
317 if (verbose)
318 fprintf(stderr,
319 "# can't create %s\n", cpathname);
320 return;
321 }
322 wr_long(MAGIC, fp);
323 /* First write a 0 for mtime */
324 wr_long(0L, fp);
325 wr_object((object *)co, fp);
326 if (ferror(fp)) {
327 if (verbose)
328 fprintf(stderr, "# can't write %s\n", cpathname);
329 /* Don't keep partial file */
330 fclose(fp);
331 (void) unlink(cpathname);
332 return;
333 }
334 /* Now write the true mtime */
335 fseek(fp, 4L, 0);
336 wr_long(mtime, fp);
337 fflush(fp);
338 fclose(fp);
339 if (verbose)
340 fprintf(stderr, "# wrote %s\n", cpathname);
341#ifdef macintosh
342 setfiletype(cpathname, 'PYTH', 'PYC ');
343#endif
344}
345
346
347/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000348 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
349 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000350
351static object *
352load_source_module(name, pathname, fp)
353 char *name;
354 char *pathname;
355 FILE *fp;
356{
357 long mtime;
358 FILE *fpc;
359 char buf[MAXPATHLEN+1];
360 char *cpathname;
361 codeobject *co;
362 object *m;
363
364 mtime = getmtime(pathname);
365 cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1);
366 if (cpathname != NULL &&
367 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
368 co = read_compiled_module(fpc);
369 fclose(fpc);
370 if (co == NULL)
371 return NULL;
372 if (verbose)
373 fprintf(stderr, "import %s # precompiled from %s\n",
374 name, cpathname);
375 }
376 else {
377 co = parse_source_module(pathname, fp);
378 if (co == NULL)
379 return NULL;
380 if (verbose)
381 fprintf(stderr, "import %s # from %s\n",
382 name, pathname);
383 write_compiled_module(co, cpathname, mtime);
384 }
385 m = exec_code_module(name, co);
386 DECREF(co);
387
388 return m;
389}
390
391
392/* Search the path (default sys.path) for a module. Return the
393 corresponding filedescr struct, and (via return arguments) the
394 pathname and an open file. Return NULL if the module is not found. */
395
396static struct filedescr *
397find_module(name, path, buf, buflen, p_fp)
398 char *name;
399 object *path;
400 /* Output parameters: */
401 char *buf;
402 int buflen;
403 FILE **p_fp;
404{
405 int i, npath, len, namelen;
406 struct filedescr *fdp;
407 FILE *fp;
408
409 if (path == NULL)
410 path = sysget("path");
411 if (path == NULL || !is_listobject(path)) {
412 err_setstr(ImportError,
413 "module search path must be list of directory names");
414 return NULL;
415 }
416 npath = getlistsize(path);
417 namelen = strlen(name);
418 for (i = 0; i < npath; i++) {
419 object *v = getlistitem(path, i);
420 if (!is_stringobject(v))
421 continue;
422 len = getstringsize(v);
423 if (len + 2 + namelen + import_maxsuffixsize >= buflen)
424 continue; /* Too long */
425 strcpy(buf, getstringvalue(v));
426 if (strlen(buf) != len)
427 continue; /* v contains '\0' */
428 if (len > 0 && buf[len-1] != SEP)
429 buf[len++] = SEP;
430 strcpy(buf+len, name);
431 len += namelen;
432 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
433 strcpy(buf+len, fdp->suffix);
434 if (verbose > 1)
435 fprintf(stderr, "# trying %s\n", buf);
436 fp = fopen(buf, fdp->mode);
437 if (fp != NULL)
438 break;
439 }
440 if (fp != NULL)
441 break;
442 }
443 if (fp == NULL) {
444 char buf[256];
445 sprintf(buf, "No module named %.200s", name);
446 err_setstr(ImportError, buf);
447 return NULL;
448 }
449
450 *p_fp = fp;
451 return fdp;
452}
453
454
455/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000456 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000457
458static object *
459load_module(name)
460 char *name;
461{
462 char buf[MAXPATHLEN+1];
463 struct filedescr *fdp;
464 FILE *fp = NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000465 object *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000466
467 fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp);
468 if (fdp == NULL)
469 return NULL;
470
471 switch (fdp->type) {
472
473 case PY_SOURCE:
474 m = load_source_module(name, buf, fp);
475 break;
476
477 case PY_COMPILED:
478 m = load_compiled_module(name, buf, fp);
479 break;
480
481 case C_EXTENSION:
482 m = load_dynamic_module(name, buf);
483 break;
484
485 default:
486 err_setstr(SystemError,
487 "find_module returned unexpected result");
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000488 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000489
490 }
491 fclose(fp);
492
493 return m;
494}
495
496
497/* Initialize a built-in module.
498 Return 1 for succes, 0 if the module is not found, and -1 with
499 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000500
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000501static int
502init_builtin(name)
503 char *name;
504{
505 int i;
506 for (i = 0; inittab[i].name != NULL; i++) {
507 if (strcmp(name, inittab[i].name) == 0) {
Guido van Rossum74e6a111994-08-29 12:54:38 +0000508 if (inittab[i].initfunc == NULL) {
509 err_setstr(ImportError,
510 "cannot re-init internal module");
511 return -1;
512 }
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +0000513 if (verbose)
514 fprintf(stderr, "import %s # builtin\n",
515 name);
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000516 (*inittab[i].initfunc)();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000517 if (err_occurred())
518 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000519 return 1;
520 }
521 }
522 return 0;
523}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000524
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000525
526/* Initialize a frozen module.
527 Return 1 for succes, 0 if the module is not found, and -1 with
528 an exception set if the initialization failed. */
529
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000530extern struct frozen {
531 char *name;
532 char *code;
533 int size;
534} frozen_modules[];
535
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000536static int
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000537init_frozen(name)
538 char *name;
539{
540 struct frozen *p;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000541 object *co;
542 object *m;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000543 for (p = frozen_modules; ; p++) {
544 if (p->name == NULL)
545 return 0;
546 if (strcmp(p->name, name) == 0)
547 break;
548 }
549 if (verbose)
550 fprintf(stderr, "import %s # frozen\n", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000551 co = rds_object(p->code, p->size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000552 if (co == NULL)
553 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000554 if (!is_codeobject(co)) {
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000555 DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000556 err_setstr(SystemError, "frozen object is not a code object");
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000557 return -1;
558 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000559 m = exec_code_module(name, (codeobject *)co);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000560 DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000561 if (m == NULL)
562 return -1;
563 DECREF(m);
564 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000565}
Guido van Rossum74e6a111994-08-29 12:54:38 +0000566
567
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000568/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000569 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +0000570
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000571object *
572import_module(name)
573 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000574{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000575 object *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000576
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000577 if ((m = dictlookup(import_modules, name)) != NULL) {
578 INCREF(m);
579 }
580 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000581 int i;
582 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
583 if (i < 0)
584 return NULL;
585 if ((m = dictlookup(import_modules, name)) == NULL) {
586 if (err_occurred() == NULL)
587 err_setstr(SystemError,
588 "built-in module not initialized properly");
589 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000590 else
591 INCREF(m);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000592 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000593 else
594 m = load_module(name);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000595 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000596
597 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000598}
599
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000600
601/* Re-import a module of any kind and return its module object, WITH
602 INCREMENTED REFERENCE COUNT */
603
604object *
605reload_module(m)
606 object *m;
607{
608 char *name;
609 int i;
610
611 if (m == NULL || !is_moduleobject(m)) {
612 err_setstr(TypeError, "reload() argument must be module");
613 return NULL;
614 }
615 name = getmodulename(m);
616 if (name == NULL)
617 return NULL;
618 if (m != dictlookup(import_modules, name)) {
619 err_setstr(ImportError, "reload() module not in sys.modules");
620 return NULL;
621 }
622 /* Check for built-in and frozen modules */
623 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
624 if (i < 0)
625 return NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000626 INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000627 }
628 else
629 m = load_module(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000630 return m;
631}
632
633
634/* Module 'imp' provides Python access to the primitives used for
635 importing modules.
636*/
637
638static object *
639imp_get_magic(self, args)
640 object *self;
641 object *args;
642{
643 char buf[4];
644
645 if (!newgetargs(args, ""))
646 return NULL;
647 buf[0] = (MAGIC >> 0) & 0xff;
648 buf[1] = (MAGIC >> 8) & 0xff;
649 buf[3] = (MAGIC >> 16) & 0xff;
650 buf[4] = (MAGIC >> 24) & 0xff;
651
652 return newsizedstringobject(buf, 4);
653}
654
655static object *
656imp_get_suffixes(self, args)
657 object *self;
658 object *args;
659{
660 object *list;
661 struct filedescr *fdp;
662
663 if (!newgetargs(args, ""))
664 return NULL;
665 list = newlistobject(0);
666 if (list == NULL)
667 return NULL;
668 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
669 object *item = mkvalue("ssi",
670 fdp->suffix, fdp->mode, fdp->type);
671 if (item == NULL) {
672 DECREF(list);
673 return NULL;
674 }
675 if (addlistitem(list, item) < 0) {
676 DECREF(list);
677 DECREF(item);
678 return NULL;
679 }
680 DECREF(item);
681 }
682 return list;
683}
684
685static object *
686imp_find_module(self, args)
687 object *self;
688 object *args;
689{
690 extern int fclose PROTO((FILE *));
691 char *name;
692 object *path = NULL;
693 object *fob, *ret;
694 struct filedescr *fdp;
695 char pathname[MAXPATHLEN+1];
696 FILE *fp;
697 if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
698 return NULL;
699 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
700 if (fdp == NULL)
701 return NULL;
702 fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
703 if (fob == NULL) {
704 fclose(fp);
705 return NULL;
706 }
707 ret = mkvalue("Os(ssi)",
708 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
709 DECREF(fob);
710 return ret;
711}
712
713static object *
714imp_init_builtin(self, args)
715 object *self;
716 object *args;
717{
718 char *name;
719 int ret;
720 object *m;
721 if (!newgetargs(args, "s", &name))
722 return NULL;
723 ret = init_builtin(name);
724 if (ret < 0)
725 return NULL;
726 if (ret == 0) {
727 INCREF(None);
728 return None;
729 }
730 m = add_module(name);
731 XINCREF(m);
732 return m;
733}
734
735static object *
736imp_init_frozen(self, args)
737 object *self;
738 object *args;
739{
740 char *name;
741 int ret;
742 object *m;
743 if (!newgetargs(args, "s", &name))
744 return NULL;
745 ret = init_frozen(name);
746 if (ret < 0)
747 return NULL;
748 if (ret == 0) {
749 INCREF(None);
750 return None;
751 }
752 m = add_module(name);
753 XINCREF(m);
754 return m;
755}
756
757static object *
758imp_is_builtin(self, args)
759 object *self;
760 object *args;
761{
762 int i;
763 char *name;
764 if (!newgetargs(args, "s", &name))
765 return NULL;
766 for (i = 0; inittab[i].name != NULL; i++) {
767 if (strcmp(name, inittab[i].name) == 0) {
768 if (inittab[i].initfunc == NULL)
769 return newintobject(-1);
770 else
771 return newintobject(1);
772 }
773 }
774 return newintobject(0);
775}
776
777static object *
778imp_is_frozen(self, args)
779 object *self;
780 object *args;
781{
782 struct frozen *p;
783 char *name;
784 if (!newgetargs(args, "s", &name))
785 return NULL;
786 for (p = frozen_modules; ; p++) {
787 if (p->name == NULL)
788 break;
789 if (strcmp(p->name, name) == 0)
790 return newintobject(1);
791 }
792 return newintobject(0);
793}
794
795static FILE *
796get_file(pathname, fob, mode)
797 char *pathname;
798 object *fob;
799 char *mode;
800{
801 FILE *fp;
802 if (fob == NULL) {
803 fp = fopen(pathname, mode);
804 if (fp == NULL)
805 err_errno(IOError);
806 }
807 else {
808 fp = getfilefile(fob);
809 if (fp == NULL)
810 err_setstr(ValueError, "bad/closed file object");
811 }
812 return fp;
813}
814
815static object *
816imp_load_compiled(self, args)
817 object *self;
818 object *args;
819{
820 char *name;
821 char *pathname;
822 object *fob = NULL;
823 object *m;
824 FILE *fp;
825 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
826 return NULL;
827 fp = get_file(pathname, fob, "rb");
828 if (fp == NULL)
829 return NULL;
830 m = load_compiled_module(name, pathname, fp);
831 if (fob == NULL)
832 fclose(fp);
833 return m;
834}
835
836static object *
837imp_load_dynamic(self, args)
838 object *self;
839 object *args;
840{
841 char *name;
842 char *pathname;
843 object *dummy;
844 if (!newgetargs(args, "ss|O", &name, &pathname, &dummy))
845 return NULL;
846 return load_dynamic_module(name, pathname);
847}
848
849static object *
850imp_load_source(self, args)
851 object *self;
852 object *args;
853{
854 char *name;
855 char *pathname;
856 object *fob = NULL;
857 object *m;
858 FILE *fp;
859 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
860 return NULL;
861 fp = get_file(pathname, fob, "r");
862 if (fp == NULL)
863 return NULL;
864 m = load_source_module(name, pathname, fp);
865 if (fob == NULL)
866 fclose(fp);
867 return m;
868}
869
870static object *
871imp_new_module(self, args)
872 object *self;
873 object *args;
874{
875 char *name;
876 if (!newgetargs(args, "s", &name))
877 return NULL;
878 return newmoduleobject(name);
879}
880
881static struct methodlist imp_methods[] = {
882 {"get_magic", imp_get_magic, 1},
883 {"get_suffixes", imp_get_suffixes, 1},
884 {"find_module", imp_find_module, 1},
885 {"init_builtin", imp_init_builtin, 1},
886 {"init_frozen", imp_init_frozen, 1},
887 {"is_builtin", imp_is_builtin, 1},
888 {"is_frozen", imp_is_frozen, 1},
889 {"load_compiled", imp_load_compiled, 1},
890 {"load_dynamic", imp_load_dynamic, 1},
891 {"load_source", imp_load_source, 1},
892 {"new_module", imp_new_module, 1},
893 {NULL, NULL} /* sentinel */
894};
895
896void
897initimp()
898{
899 object *m, *d, *v;
900
901 m = initmodule("imp", imp_methods);
902 d = getmoduledict(m);
903
904 v = newintobject(SEARCH_ERROR);
905 dictinsert(d, "SEARCH_ERROR", v);
906 XDECREF(v);
907
908 v = newintobject(PY_SOURCE);
909 dictinsert(d, "PY_SOURCE", v);
910 XDECREF(v);
911
912 v = newintobject(PY_COMPILED);
913 dictinsert(d, "PY_COMPILED", v);
914 XDECREF(v);
915
916 v = newintobject(C_EXTENSION);
917 dictinsert(d, "C_EXTENSION", v);
918 XDECREF(v);
919
920 if (err_occurred())
921 fatal("imp module initialization failed");
922}