blob: 35c0d9d44d5a26a4e9080a831a3f3934c2597cba [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 Rossum0b344901995-02-07 15:35:27 +0000533/* This function is also used from frozenmain.c */
534
535int
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000536init_frozen(name)
537 char *name;
538{
539 struct frozen *p;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000540 object *co;
541 object *m;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000542 for (p = frozen_modules; ; p++) {
543 if (p->name == NULL)
544 return 0;
545 if (strcmp(p->name, name) == 0)
546 break;
547 }
548 if (verbose)
549 fprintf(stderr, "import %s # frozen\n", name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000550 co = rds_object(p->code, p->size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000551 if (co == NULL)
552 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000553 if (!is_codeobject(co)) {
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000554 DECREF(co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000555 err_setstr(SystemError, "frozen object is not a code object");
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000556 return -1;
557 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000558 m = exec_code_module(name, (codeobject *)co);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000559 DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000560 if (m == NULL)
561 return -1;
562 DECREF(m);
563 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000564}
Guido van Rossum74e6a111994-08-29 12:54:38 +0000565
566
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000567/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000568 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +0000569
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000570object *
571import_module(name)
572 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000573{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000574 object *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000575
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000576 if (import_modules == NULL) {
577 err_setstr(SystemError, "sys.modules has been deleted");
578 return NULL;
579 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000580 if ((m = dictlookup(import_modules, name)) != NULL) {
581 INCREF(m);
582 }
583 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000584 int i;
585 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
586 if (i < 0)
587 return NULL;
588 if ((m = dictlookup(import_modules, name)) == NULL) {
589 if (err_occurred() == NULL)
590 err_setstr(SystemError,
591 "built-in module not initialized properly");
592 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000593 else
594 INCREF(m);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000595 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000596 else
597 m = load_module(name);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000598 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000599
600 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000601}
602
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000603
604/* Re-import a module of any kind and return its module object, WITH
605 INCREMENTED REFERENCE COUNT */
606
607object *
608reload_module(m)
609 object *m;
610{
611 char *name;
612 int i;
613
614 if (m == NULL || !is_moduleobject(m)) {
615 err_setstr(TypeError, "reload() argument must be module");
616 return NULL;
617 }
618 name = getmodulename(m);
619 if (name == NULL)
620 return NULL;
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000621 if (import_modules == NULL) {
622 err_setstr(SystemError, "sys.modules has been deleted");
623 return NULL;
624 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000625 if (m != dictlookup(import_modules, name)) {
626 err_setstr(ImportError, "reload() module not in sys.modules");
627 return NULL;
628 }
629 /* Check for built-in and frozen modules */
630 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
631 if (i < 0)
632 return NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000633 INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000634 }
635 else
636 m = load_module(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000637 return m;
638}
639
640
641/* Module 'imp' provides Python access to the primitives used for
642 importing modules.
643*/
644
645static object *
646imp_get_magic(self, args)
647 object *self;
648 object *args;
649{
650 char buf[4];
651
652 if (!newgetargs(args, ""))
653 return NULL;
654 buf[0] = (MAGIC >> 0) & 0xff;
655 buf[1] = (MAGIC >> 8) & 0xff;
Guido van Rossum90f0e071995-01-30 12:53:06 +0000656 buf[2] = (MAGIC >> 16) & 0xff;
657 buf[3] = (MAGIC >> 24) & 0xff;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000658
659 return newsizedstringobject(buf, 4);
660}
661
662static object *
663imp_get_suffixes(self, args)
664 object *self;
665 object *args;
666{
667 object *list;
668 struct filedescr *fdp;
669
670 if (!newgetargs(args, ""))
671 return NULL;
672 list = newlistobject(0);
673 if (list == NULL)
674 return NULL;
675 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
676 object *item = mkvalue("ssi",
677 fdp->suffix, fdp->mode, fdp->type);
678 if (item == NULL) {
679 DECREF(list);
680 return NULL;
681 }
682 if (addlistitem(list, item) < 0) {
683 DECREF(list);
684 DECREF(item);
685 return NULL;
686 }
687 DECREF(item);
688 }
689 return list;
690}
691
692static object *
693imp_find_module(self, args)
694 object *self;
695 object *args;
696{
697 extern int fclose PROTO((FILE *));
698 char *name;
699 object *path = NULL;
700 object *fob, *ret;
701 struct filedescr *fdp;
702 char pathname[MAXPATHLEN+1];
703 FILE *fp;
704 if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
705 return NULL;
706 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
707 if (fdp == NULL)
708 return NULL;
709 fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
710 if (fob == NULL) {
711 fclose(fp);
712 return NULL;
713 }
714 ret = mkvalue("Os(ssi)",
715 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
716 DECREF(fob);
717 return ret;
718}
719
720static object *
721imp_init_builtin(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_builtin(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_init_frozen(self, args)
744 object *self;
745 object *args;
746{
747 char *name;
748 int ret;
749 object *m;
750 if (!newgetargs(args, "s", &name))
751 return NULL;
752 ret = init_frozen(name);
753 if (ret < 0)
754 return NULL;
755 if (ret == 0) {
756 INCREF(None);
757 return None;
758 }
759 m = add_module(name);
760 XINCREF(m);
761 return m;
762}
763
764static object *
765imp_is_builtin(self, args)
766 object *self;
767 object *args;
768{
769 int i;
770 char *name;
771 if (!newgetargs(args, "s", &name))
772 return NULL;
773 for (i = 0; inittab[i].name != NULL; i++) {
774 if (strcmp(name, inittab[i].name) == 0) {
775 if (inittab[i].initfunc == NULL)
776 return newintobject(-1);
777 else
778 return newintobject(1);
779 }
780 }
781 return newintobject(0);
782}
783
784static object *
785imp_is_frozen(self, args)
786 object *self;
787 object *args;
788{
789 struct frozen *p;
790 char *name;
791 if (!newgetargs(args, "s", &name))
792 return NULL;
793 for (p = frozen_modules; ; p++) {
794 if (p->name == NULL)
795 break;
796 if (strcmp(p->name, name) == 0)
797 return newintobject(1);
798 }
799 return newintobject(0);
800}
801
802static FILE *
803get_file(pathname, fob, mode)
804 char *pathname;
805 object *fob;
806 char *mode;
807{
808 FILE *fp;
809 if (fob == NULL) {
810 fp = fopen(pathname, mode);
811 if (fp == NULL)
812 err_errno(IOError);
813 }
814 else {
815 fp = getfilefile(fob);
816 if (fp == NULL)
817 err_setstr(ValueError, "bad/closed file object");
818 }
819 return fp;
820}
821
822static object *
823imp_load_compiled(self, args)
824 object *self;
825 object *args;
826{
827 char *name;
828 char *pathname;
829 object *fob = NULL;
830 object *m;
831 FILE *fp;
832 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
833 return NULL;
834 fp = get_file(pathname, fob, "rb");
835 if (fp == NULL)
836 return NULL;
837 m = load_compiled_module(name, pathname, fp);
838 if (fob == NULL)
839 fclose(fp);
840 return m;
841}
842
843static object *
844imp_load_dynamic(self, args)
845 object *self;
846 object *args;
847{
848 char *name;
849 char *pathname;
850 object *dummy;
851 if (!newgetargs(args, "ss|O", &name, &pathname, &dummy))
852 return NULL;
853 return load_dynamic_module(name, pathname);
854}
855
856static object *
857imp_load_source(self, args)
858 object *self;
859 object *args;
860{
861 char *name;
862 char *pathname;
863 object *fob = NULL;
864 object *m;
865 FILE *fp;
866 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
867 return NULL;
868 fp = get_file(pathname, fob, "r");
869 if (fp == NULL)
870 return NULL;
871 m = load_source_module(name, pathname, fp);
872 if (fob == NULL)
873 fclose(fp);
874 return m;
875}
876
877static object *
878imp_new_module(self, args)
879 object *self;
880 object *args;
881{
882 char *name;
883 if (!newgetargs(args, "s", &name))
884 return NULL;
885 return newmoduleobject(name);
886}
887
888static struct methodlist imp_methods[] = {
889 {"get_magic", imp_get_magic, 1},
890 {"get_suffixes", imp_get_suffixes, 1},
891 {"find_module", imp_find_module, 1},
892 {"init_builtin", imp_init_builtin, 1},
893 {"init_frozen", imp_init_frozen, 1},
894 {"is_builtin", imp_is_builtin, 1},
895 {"is_frozen", imp_is_frozen, 1},
896 {"load_compiled", imp_load_compiled, 1},
897 {"load_dynamic", imp_load_dynamic, 1},
898 {"load_source", imp_load_source, 1},
899 {"new_module", imp_new_module, 1},
900 {NULL, NULL} /* sentinel */
901};
902
903void
904initimp()
905{
906 object *m, *d, *v;
907
908 m = initmodule("imp", imp_methods);
909 d = getmoduledict(m);
910
911 v = newintobject(SEARCH_ERROR);
912 dictinsert(d, "SEARCH_ERROR", v);
913 XDECREF(v);
914
915 v = newintobject(PY_SOURCE);
916 dictinsert(d, "PY_SOURCE", v);
917 XDECREF(v);
918
919 v = newintobject(PY_COMPILED);
920 dictinsert(d, "PY_COMPILED", v);
921 XDECREF(v);
922
923 v = newintobject(C_EXTENSION);
924 dictinsert(d, "C_EXTENSION", v);
925 XDECREF(v);
926
927 if (err_occurred())
928 fatal("imp module initialization failed");
929}