blob: 37cfdeb24e367d46c09881eda26681a18c9c5c2d [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
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Module definition and import implementation */
33
Guido van Rossum3f5da241990-12-20 15:06:42 +000034#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035
Guido van Rossum40f470f1996-05-23 22:51:04 +000036/* XXX Some of the following are duplicate with allobjects.h... */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038#include "token.h"
39#include "graminit.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000040#include "import.h"
41#include "errcode.h"
42#include "sysmodule.h"
Guido van Rossum6135a871995-01-09 17:53:26 +000043#include "bltinmodule.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000044#include "pythonrun.h"
Guido van Rossumc405b7b1991-06-04 19:39:42 +000045#include "marshal.h"
46#include "compile.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000047#include "eval.h"
Guido van Rossumd8bac6d1992-02-26 15:19:13 +000048#include "osdefs.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000049#include "importdl.h"
Jack Jansen9c96a921995-02-15 22:57:06 +000050#ifdef macintosh
Jack Jansen614cf811995-07-28 11:28:14 +000051/* 'argument' is a grammar symbol, but also used in some mac header files */
52#undef argument
Jack Jansen9c96a921995-02-15 22:57:06 +000053#include "macglue.h"
54#endif
Guido van Rossumc405b7b1991-06-04 19:39:42 +000055
Guido van Rossum80bb9651996-12-05 23:27:02 +000056#ifdef HAVE_UNISTD_H
57#include <unistd.h>
58#endif
59
Guido van Rossum74e6a111994-08-29 12:54:38 +000060extern long getmtime(); /* In getmtime.c */
Guido van Rossum21d335e1993-10-15 13:01:11 +000061
Guido van Rossum6c849691994-09-26 15:47:17 +000062/* Magic word to reject .pyc files generated by other Python versions */
Guido van Rossum7faeab31995-07-07 22:50:36 +000063/* Change for each incompatible change */
64/* The value of CR and LF is incorporated so if you ever read or write
65 a .pyc file in text mode the magic number will be wrong; also, the
66 Apple MPW compiler swaps their values, botching string constants */
67/* XXX Perhaps the magic number should be frozen and a version field
68 added to the .pyc file header? */
Guido van Rossumdd5db431997-01-17 21:06:11 +000069/* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */
Guido van Rossum99d18251997-01-24 03:44:53 +000070#define MAGIC (20121 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000071
Guido van Rossum1ae940a1995-01-02 19:04:15 +000072object *import_modules; /* This becomes sys.modules */
Guido van Rossum3f5da241990-12-20 15:06:42 +000073
Guido van Rossum66f1fa81991-04-03 19:03:52 +000074
Guido van Rossum1ae940a1995-01-02 19:04:15 +000075/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076
77void
78initimport()
79{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000080 if (import_modules != NULL)
81 fatal("duplicate initimport() call");
82 if ((import_modules = newdictobject()) == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +000083 fatal("no mem for dictionary of modules");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084}
85
Guido van Rossum3f5da241990-12-20 15:06:42 +000086
Guido van Rossum1ae940a1995-01-02 19:04:15 +000087/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +000088
Guido van Rossum3f5da241990-12-20 15:06:42 +000089void
90doneimport()
91{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000092 if (import_modules != NULL) {
Guido van Rossum0de81bf1995-01-26 00:41:28 +000093 object *tmp = import_modules;
94 import_modules = NULL;
95 /* This deletes all modules from sys.modules.
96 When a module is deallocated, it in turn clears its dictionary,
97 thus hopefully breaking any circular references between modules
98 and between a module's dictionary and its functions.
99 Note that "import" will fail while we are cleaning up.
100 */
101 mappingclear(tmp);
102 DECREF(tmp);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000103 }
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000104}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000105
106
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000107/* Helper for pythonrun.c -- return magic number */
108
109long
110get_pyc_magic()
111{
112 return MAGIC;
113}
114
115
116/* Helper for sysmodule.c -- return modules dictionary */
117
118object *
119get_modules()
120{
121 return import_modules;
122}
123
124
125/* Get the module object corresponding to a module name.
126 First check the modules dictionary if there's one there,
127 if not, create a new one and insert in in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000128 Because the former action is most common, THIS DOES NOT RETURN A
129 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000130
131object *
132add_module(name)
133 char *name;
134{
135 object *m;
136
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000137 if (import_modules == NULL) {
138 err_setstr(SystemError, "sys.modules has been deleted");
139 return NULL;
140 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000141 if ((m = dictlookup(import_modules, name)) != NULL &&
142 is_moduleobject(m))
143 return m;
144 m = newmoduleobject(name);
145 if (m == NULL)
146 return NULL;
147 if (dictinsert(import_modules, name, m) != 0) {
148 DECREF(m);
149 return NULL;
150 }
151 DECREF(m); /* Yes, it still exists, in modules! */
152
153 return m;
154}
155
156
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000157/* Execute a code object in a module and return the module object
158 WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000159
Jack Jansen9c96a921995-02-15 22:57:06 +0000160object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000161exec_code_module(name, co)
162 char *name;
Jack Jansen9c96a921995-02-15 22:57:06 +0000163 object *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000164{
165 object *m, *d, *v;
166
167 m = add_module(name);
168 if (m == NULL)
169 return NULL;
170 d = getmoduledict(m);
Guido van Rossum6135a871995-01-09 17:53:26 +0000171 if (dictlookup(d, "__builtins__") == NULL) {
Guido van Rossum94390ec1995-01-12 11:37:57 +0000172 if (dictinsert(d, "__builtins__", getbuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000173 return NULL;
174 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000175 /* Remember the filename as the __file__ attribute */
176 if (dictinsert(d, "__file__", ((codeobject *)co)->co_filename) != 0)
177 err_clear(); /* Not important enough to report */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000178 v = eval_code((codeobject *)co, d, d); /* XXX owner? */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000179 if (v == NULL)
180 return NULL;
181 DECREF(v);
182 INCREF(m);
183
184 return m;
185}
186
187
188/* Given a pathname for a Python source file, fill a buffer with the
189 pathname for the corresponding compiled file. Return the pathname
190 for the compiled file, or NULL if there's no space in the buffer.
191 Doesn't set an exception. */
192
193static char *
194make_compiled_pathname(pathname, buf, buflen)
195 char *pathname;
196 char *buf;
197 int buflen;
198{
199 int len;
200
201 len = strlen(pathname);
202 if (len+2 > buflen)
203 return NULL;
204 strcpy(buf, pathname);
205 strcpy(buf+len, "c");
206
207 return buf;
208}
209
210
211/* Given a pathname for a Python source file, its time of last
212 modification, and a pathname for a compiled file, check whether the
213 compiled file represents the same version of the source. If so,
214 return a FILE pointer for the compiled file, positioned just after
215 the header; if not, return NULL.
216 Doesn't set an exception. */
217
218static FILE *
219check_compiled_module(pathname, mtime, cpathname)
220 char *pathname;
221 long mtime;
222 char *cpathname;
223{
224 FILE *fp;
225 long magic;
226 long pyc_mtime;
227
228 fp = fopen(cpathname, "rb");
229 if (fp == NULL)
230 return NULL;
231 magic = rd_long(fp);
232 if (magic != MAGIC) {
233 if (verbose)
234 fprintf(stderr, "# %s has bad magic\n", cpathname);
235 fclose(fp);
236 return NULL;
237 }
238 pyc_mtime = rd_long(fp);
239 if (pyc_mtime != mtime) {
240 if (verbose)
241 fprintf(stderr, "# %s has bad mtime\n", cpathname);
242 fclose(fp);
243 return NULL;
244 }
245 if (verbose)
246 fprintf(stderr, "# %s matches %s\n", cpathname, pathname);
247 return fp;
248}
249
250
251/* Read a code object from a file and check it for validity */
252
253static codeobject *
254read_compiled_module(fp)
255 FILE *fp;
256{
257 object *co;
258
259 co = rd_object(fp);
260 /* Ugly: rd_object() may return NULL with or without error */
261 if (co == NULL || !is_codeobject(co)) {
262 if (!err_occurred())
263 err_setstr(ImportError,
264 "Non-code object in .pyc file");
265 XDECREF(co);
266 return NULL;
267 }
268 return (codeobject *)co;
269}
270
271
272/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000273 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000274
275static object *
276load_compiled_module(name, cpathname, fp)
277 char *name;
278 char *cpathname;
279 FILE *fp;
280{
281 long magic;
282 codeobject *co;
283 object *m;
284
285 magic = rd_long(fp);
286 if (magic != MAGIC) {
287 err_setstr(ImportError, "Bad magic number in .pyc file");
288 return NULL;
289 }
290 (void) rd_long(fp);
291 co = read_compiled_module(fp);
292 if (co == NULL)
293 return NULL;
294 if (verbose)
295 fprintf(stderr, "import %s # precompiled from %s\n",
296 name, cpathname);
Jack Jansen9c96a921995-02-15 22:57:06 +0000297 m = exec_code_module(name, (object *)co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000298 DECREF(co);
299
300 return m;
301}
302
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000303/* Parse a source file and return the corresponding code object */
304
305static codeobject *
306parse_source_module(pathname, fp)
307 char *pathname;
308 FILE *fp;
309{
310 codeobject *co;
311 node *n;
312
313 n = parse_file(fp, pathname, file_input);
314 if (n == NULL)
315 return NULL;
316 co = compile(n, pathname);
317 freetree(n);
318
319 return co;
320}
321
322
323/* Write a compiled module to a file, placing the time of last
324 modification of its source into the header.
325 Errors are ignored, if a write error occurs an attempt is made to
326 remove the file. */
327
328static void
329write_compiled_module(co, cpathname, mtime)
330 codeobject *co;
331 char *cpathname;
332 long mtime;
333{
334 FILE *fp;
335
336 fp = fopen(cpathname, "wb");
337 if (fp == NULL) {
338 if (verbose)
339 fprintf(stderr,
340 "# can't create %s\n", cpathname);
341 return;
342 }
343 wr_long(MAGIC, fp);
344 /* First write a 0 for mtime */
345 wr_long(0L, fp);
346 wr_object((object *)co, fp);
347 if (ferror(fp)) {
348 if (verbose)
349 fprintf(stderr, "# can't write %s\n", cpathname);
350 /* Don't keep partial file */
351 fclose(fp);
352 (void) unlink(cpathname);
353 return;
354 }
355 /* Now write the true mtime */
356 fseek(fp, 4L, 0);
357 wr_long(mtime, fp);
358 fflush(fp);
359 fclose(fp);
360 if (verbose)
361 fprintf(stderr, "# wrote %s\n", cpathname);
362#ifdef macintosh
Guido van Rossumbe1a6e21996-02-21 15:29:20 +0000363 setfiletype(cpathname, 'Pyth', 'PYC ');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000364#endif
365}
366
367
368/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000369 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
370 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000371
372static object *
373load_source_module(name, pathname, fp)
374 char *name;
375 char *pathname;
376 FILE *fp;
377{
378 long mtime;
379 FILE *fpc;
380 char buf[MAXPATHLEN+1];
381 char *cpathname;
382 codeobject *co;
383 object *m;
384
385 mtime = getmtime(pathname);
386 cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1);
387 if (cpathname != NULL &&
388 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
389 co = read_compiled_module(fpc);
390 fclose(fpc);
391 if (co == NULL)
392 return NULL;
393 if (verbose)
394 fprintf(stderr, "import %s # precompiled from %s\n",
395 name, cpathname);
396 }
397 else {
398 co = parse_source_module(pathname, fp);
399 if (co == NULL)
400 return NULL;
401 if (verbose)
402 fprintf(stderr, "import %s # from %s\n",
403 name, pathname);
404 write_compiled_module(co, cpathname, mtime);
405 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000406 m = exec_code_module(name, (object *)co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000407 DECREF(co);
408
409 return m;
410}
411
412
413/* Search the path (default sys.path) for a module. Return the
414 corresponding filedescr struct, and (via return arguments) the
415 pathname and an open file. Return NULL if the module is not found. */
416
417static struct filedescr *
418find_module(name, path, buf, buflen, p_fp)
419 char *name;
420 object *path;
421 /* Output parameters: */
422 char *buf;
423 int buflen;
424 FILE **p_fp;
425{
426 int i, npath, len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +0000427 struct filedescr *fdp = NULL;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000428 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000429
Guido van Rossumac279101996-08-22 23:10:58 +0000430#ifdef MS_COREDLL
Guido van Rossuma5a3db71996-04-09 02:39:59 +0000431 if ((fp=PyWin_FindRegisteredModule(name, &fdp, buf, buflen))!=NULL) {
432 *p_fp = fp;
433 return fdp;
434 }
435#endif
436
437
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000438 if (path == NULL)
439 path = sysget("path");
440 if (path == NULL || !is_listobject(path)) {
441 err_setstr(ImportError,
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000442 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000443 return NULL;
444 }
445 npath = getlistsize(path);
446 namelen = strlen(name);
447 for (i = 0; i < npath; i++) {
448 object *v = getlistitem(path, i);
449 if (!is_stringobject(v))
450 continue;
451 len = getstringsize(v);
452 if (len + 2 + namelen + import_maxsuffixsize >= buflen)
453 continue; /* Too long */
454 strcpy(buf, getstringvalue(v));
455 if (strlen(buf) != len)
456 continue; /* v contains '\0' */
Jack Jansen9c96a921995-02-15 22:57:06 +0000457#ifdef macintosh
458 if ( PyMac_FindResourceModule(name, buf) ) {
459 static struct filedescr resfiledescr = { "", "", PY_RESOURCE};
460
461 return &resfiledescr;
462 }
463#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000464 if (len > 0 && buf[len-1] != SEP)
465 buf[len++] = SEP;
Guido van Rossum40f470f1996-05-23 22:51:04 +0000466#ifdef IMPORT_8x3_NAMES
467 /* see if we are searching in directory dos_8x3 */
468 if (len > 7 && !strncmp(buf + len - 8, "dos_8x3", 7)){
469 int j;
470 char ch; /* limit name to eight lower-case characters */
471 for (j = 0; (ch = name[j]) && j < 8; j++)
472 if (isupper(ch))
473 buf[len++] = tolower(ch);
474 else
475 buf[len++] = ch;
476 }
Guido van Rossum0e41c8c1996-06-20 14:18:34 +0000477 else /* Not in dos_8x3, use the full name */
Guido van Rossum6f489d91996-06-28 20:15:15 +0000478#endif
Guido van Rossum0e41c8c1996-06-20 14:18:34 +0000479 {
Guido van Rossum40f470f1996-05-23 22:51:04 +0000480 strcpy(buf+len, name);
481 len += namelen;
482 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000483 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
484 strcpy(buf+len, fdp->suffix);
485 if (verbose > 1)
486 fprintf(stderr, "# trying %s\n", buf);
487 fp = fopen(buf, fdp->mode);
488 if (fp != NULL)
489 break;
490 }
491 if (fp != NULL)
492 break;
493 }
494 if (fp == NULL) {
495 char buf[256];
496 sprintf(buf, "No module named %.200s", name);
497 err_setstr(ImportError, buf);
498 return NULL;
499 }
500
501 *p_fp = fp;
502 return fdp;
503}
504
505
506/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000507 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000508
509static object *
510load_module(name)
511 char *name;
512{
513 char buf[MAXPATHLEN+1];
514 struct filedescr *fdp;
515 FILE *fp = NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000516 object *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000517
518 fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp);
519 if (fdp == NULL)
520 return NULL;
521
522 switch (fdp->type) {
523
524 case PY_SOURCE:
525 m = load_source_module(name, buf, fp);
526 break;
527
528 case PY_COMPILED:
529 m = load_compiled_module(name, buf, fp);
530 break;
531
532 case C_EXTENSION:
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000533 m = load_dynamic_module(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000534 break;
535
Jack Jansen9c96a921995-02-15 22:57:06 +0000536#ifdef macintosh
537 case PY_RESOURCE:
538 m = PyMac_LoadResourceModule(name, buf);
539 break;
540#endif
541
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000542 default:
543 err_setstr(SystemError,
544 "find_module returned unexpected result");
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000545 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000546
547 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000548 if ( fp )
549 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000550
551 return m;
552}
553
554
555/* Initialize a built-in module.
556 Return 1 for succes, 0 if the module is not found, and -1 with
557 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000558
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000559static int
560init_builtin(name)
561 char *name;
562{
563 int i;
564 for (i = 0; inittab[i].name != NULL; i++) {
565 if (strcmp(name, inittab[i].name) == 0) {
Guido van Rossum74e6a111994-08-29 12:54:38 +0000566 if (inittab[i].initfunc == NULL) {
567 err_setstr(ImportError,
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000568 "Cannot re-init internal module");
Guido van Rossum74e6a111994-08-29 12:54:38 +0000569 return -1;
570 }
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +0000571 if (verbose)
572 fprintf(stderr, "import %s # builtin\n",
573 name);
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000574 (*inittab[i].initfunc)();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000575 if (err_occurred())
576 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000577 return 1;
578 }
579 }
580 return 0;
581}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000582
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000583
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000584/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000585
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000586static struct _frozen *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000587find_frozen(name)
588 char *name;
589{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000590 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000591
592 for (p = frozen_modules; ; p++) {
593 if (p->name == NULL)
594 return NULL;
595 if (strcmp(p->name, name) == 0)
596 break;
597 }
598 return p;
599}
600
601static object *
602get_frozen_object(name)
603 char *name;
604{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000605 struct _frozen *p = find_frozen(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000606
607 if (p == NULL) {
608 err_setstr(ImportError, "No such frozen object");
609 return NULL;
610 }
Guido van Rossum1741d601996-08-08 18:52:59 +0000611 return rds_object((char *)p->code, p->size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000612}
613
614/* Initialize a frozen module.
615 Return 1 for succes, 0 if the module is not found, and -1 with
616 an exception set if the initialization failed.
617 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +0000618
619int
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000620init_frozen(name)
621 char *name;
622{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000623 struct _frozen *p = find_frozen(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000624 object *co;
625 object *m;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000626
627 if (p == NULL)
628 return 0;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000629 if (verbose)
630 fprintf(stderr, "import %s # frozen\n", name);
Guido van Rossum1741d601996-08-08 18:52:59 +0000631 co = rds_object((char *)p->code, p->size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000632 if (co == NULL)
633 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000634 if (!is_codeobject(co)) {
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000635 DECREF(co);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000636 err_setstr(TypeError, "frozen object is not a code object");
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000637 return -1;
638 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000639 m = exec_code_module(name, co);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000640 DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000641 if (m == NULL)
642 return -1;
643 DECREF(m);
644 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000645}
Guido van Rossum74e6a111994-08-29 12:54:38 +0000646
647
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000648/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000649 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +0000650
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000651object *
652import_module(name)
653 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000654{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000655 object *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000656
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000657 if (import_modules == NULL) {
658 err_setstr(SystemError, "sys.modules has been deleted");
659 return NULL;
660 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000661 if ((m = dictlookup(import_modules, name)) != NULL) {
662 INCREF(m);
663 }
664 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000665 int i;
666 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
667 if (i < 0)
668 return NULL;
669 if ((m = dictlookup(import_modules, name)) == NULL) {
670 if (err_occurred() == NULL)
671 err_setstr(SystemError,
672 "built-in module not initialized properly");
673 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000674 else
675 INCREF(m);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000676 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000677 else
678 m = load_module(name);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000679 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000680
681 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000682}
683
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000684
685/* Re-import a module of any kind and return its module object, WITH
686 INCREMENTED REFERENCE COUNT */
687
688object *
689reload_module(m)
690 object *m;
691{
692 char *name;
693 int i;
694
695 if (m == NULL || !is_moduleobject(m)) {
696 err_setstr(TypeError, "reload() argument must be module");
697 return NULL;
698 }
699 name = getmodulename(m);
700 if (name == NULL)
701 return NULL;
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000702 if (import_modules == NULL) {
703 err_setstr(SystemError, "sys.modules has been deleted");
704 return NULL;
705 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000706 if (m != dictlookup(import_modules, name)) {
707 err_setstr(ImportError, "reload() module not in sys.modules");
708 return NULL;
709 }
710 /* Check for built-in and frozen modules */
711 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
712 if (i < 0)
713 return NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000714 INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000715 }
716 else
717 m = load_module(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000718 return m;
719}
720
721
722/* Module 'imp' provides Python access to the primitives used for
723 importing modules.
724*/
725
726static object *
727imp_get_magic(self, args)
728 object *self;
729 object *args;
730{
731 char buf[4];
732
733 if (!newgetargs(args, ""))
734 return NULL;
735 buf[0] = (MAGIC >> 0) & 0xff;
736 buf[1] = (MAGIC >> 8) & 0xff;
Guido van Rossum90f0e071995-01-30 12:53:06 +0000737 buf[2] = (MAGIC >> 16) & 0xff;
738 buf[3] = (MAGIC >> 24) & 0xff;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000739
740 return newsizedstringobject(buf, 4);
741}
742
743static object *
744imp_get_suffixes(self, args)
745 object *self;
746 object *args;
747{
748 object *list;
749 struct filedescr *fdp;
750
751 if (!newgetargs(args, ""))
752 return NULL;
753 list = newlistobject(0);
754 if (list == NULL)
755 return NULL;
756 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
757 object *item = mkvalue("ssi",
758 fdp->suffix, fdp->mode, fdp->type);
759 if (item == NULL) {
760 DECREF(list);
761 return NULL;
762 }
763 if (addlistitem(list, item) < 0) {
764 DECREF(list);
765 DECREF(item);
766 return NULL;
767 }
768 DECREF(item);
769 }
770 return list;
771}
772
773static object *
774imp_find_module(self, args)
775 object *self;
776 object *args;
777{
778 extern int fclose PROTO((FILE *));
779 char *name;
780 object *path = NULL;
781 object *fob, *ret;
782 struct filedescr *fdp;
783 char pathname[MAXPATHLEN+1];
784 FILE *fp;
785 if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
786 return NULL;
787 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
788 if (fdp == NULL)
789 return NULL;
790 fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
791 if (fob == NULL) {
792 fclose(fp);
793 return NULL;
794 }
795 ret = mkvalue("Os(ssi)",
796 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
797 DECREF(fob);
798 return ret;
799}
800
801static object *
802imp_init_builtin(self, args)
803 object *self;
804 object *args;
805{
806 char *name;
807 int ret;
808 object *m;
809 if (!newgetargs(args, "s", &name))
810 return NULL;
811 ret = init_builtin(name);
812 if (ret < 0)
813 return NULL;
814 if (ret == 0) {
815 INCREF(None);
816 return None;
817 }
818 m = add_module(name);
819 XINCREF(m);
820 return m;
821}
822
823static object *
824imp_init_frozen(self, args)
825 object *self;
826 object *args;
827{
828 char *name;
829 int ret;
830 object *m;
831 if (!newgetargs(args, "s", &name))
832 return NULL;
833 ret = init_frozen(name);
834 if (ret < 0)
835 return NULL;
836 if (ret == 0) {
837 INCREF(None);
838 return None;
839 }
840 m = add_module(name);
841 XINCREF(m);
842 return m;
843}
844
845static object *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000846imp_get_frozen_object(self, args)
847 object *self;
848 object *args;
849{
850 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +0000851
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000852 if (!newgetargs(args, "s", &name))
853 return NULL;
854 return get_frozen_object(name);
855}
856
857static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000858imp_is_builtin(self, args)
859 object *self;
860 object *args;
861{
862 int i;
863 char *name;
864 if (!newgetargs(args, "s", &name))
865 return NULL;
866 for (i = 0; inittab[i].name != NULL; i++) {
867 if (strcmp(name, inittab[i].name) == 0) {
868 if (inittab[i].initfunc == NULL)
869 return newintobject(-1);
870 else
871 return newintobject(1);
872 }
873 }
874 return newintobject(0);
875}
876
877static object *
878imp_is_frozen(self, args)
879 object *self;
880 object *args;
881{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000882 struct _frozen *p;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000883 char *name;
884 if (!newgetargs(args, "s", &name))
885 return NULL;
886 for (p = frozen_modules; ; p++) {
887 if (p->name == NULL)
888 break;
889 if (strcmp(p->name, name) == 0)
890 return newintobject(1);
891 }
892 return newintobject(0);
893}
894
895static FILE *
896get_file(pathname, fob, mode)
897 char *pathname;
898 object *fob;
899 char *mode;
900{
901 FILE *fp;
902 if (fob == NULL) {
903 fp = fopen(pathname, mode);
904 if (fp == NULL)
905 err_errno(IOError);
906 }
907 else {
908 fp = getfilefile(fob);
909 if (fp == NULL)
910 err_setstr(ValueError, "bad/closed file object");
911 }
912 return fp;
913}
914
915static object *
916imp_load_compiled(self, args)
917 object *self;
918 object *args;
919{
920 char *name;
921 char *pathname;
922 object *fob = NULL;
923 object *m;
924 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000925 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000926 return NULL;
927 fp = get_file(pathname, fob, "rb");
928 if (fp == NULL)
929 return NULL;
930 m = load_compiled_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000931 return m;
932}
933
934static object *
935imp_load_dynamic(self, args)
936 object *self;
937 object *args;
938{
939 char *name;
940 char *pathname;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000941 object *fob = NULL;
942 object *m;
943 FILE *fp = NULL;
944 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000945 return NULL;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000946 if (fob)
947 fp = get_file(pathname, fob, "r");
948 m = load_dynamic_module(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +0000949 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000950}
951
952static object *
953imp_load_source(self, args)
954 object *self;
955 object *args;
956{
957 char *name;
958 char *pathname;
959 object *fob = NULL;
960 object *m;
961 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000962 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000963 return NULL;
964 fp = get_file(pathname, fob, "r");
965 if (fp == NULL)
966 return NULL;
967 m = load_source_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000968 return m;
969}
970
Jack Jansen9c96a921995-02-15 22:57:06 +0000971#ifdef macintosh
972static object *
973imp_load_resource(self, args)
974 object *self;
975 object *args;
976{
977 char *name;
978 char *pathname;
979 object *m;
980
981 if (!newgetargs(args, "ss", &name, &pathname))
982 return NULL;
983 m = PyMac_LoadResourceModule(name, pathname);
984 return m;
985}
986#endif /* macintosh */
987
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000988static object *
989imp_new_module(self, args)
990 object *self;
991 object *args;
992{
993 char *name;
994 if (!newgetargs(args, "s", &name))
995 return NULL;
996 return newmoduleobject(name);
997}
998
999static struct methodlist imp_methods[] = {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +00001000 {"get_frozen_object", imp_get_frozen_object, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001001 {"get_magic", imp_get_magic, 1},
1002 {"get_suffixes", imp_get_suffixes, 1},
1003 {"find_module", imp_find_module, 1},
1004 {"init_builtin", imp_init_builtin, 1},
1005 {"init_frozen", imp_init_frozen, 1},
1006 {"is_builtin", imp_is_builtin, 1},
1007 {"is_frozen", imp_is_frozen, 1},
1008 {"load_compiled", imp_load_compiled, 1},
1009 {"load_dynamic", imp_load_dynamic, 1},
1010 {"load_source", imp_load_source, 1},
1011 {"new_module", imp_new_module, 1},
Jack Jansen9c96a921995-02-15 22:57:06 +00001012#ifdef macintosh
1013 {"load_resource", imp_load_resource, 1},
1014#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001015 {NULL, NULL} /* sentinel */
1016};
1017
1018void
1019initimp()
1020{
1021 object *m, *d, *v;
1022
1023 m = initmodule("imp", imp_methods);
1024 d = getmoduledict(m);
1025
1026 v = newintobject(SEARCH_ERROR);
1027 dictinsert(d, "SEARCH_ERROR", v);
1028 XDECREF(v);
1029
1030 v = newintobject(PY_SOURCE);
1031 dictinsert(d, "PY_SOURCE", v);
1032 XDECREF(v);
1033
1034 v = newintobject(PY_COMPILED);
1035 dictinsert(d, "PY_COMPILED", v);
1036 XDECREF(v);
1037
1038 v = newintobject(C_EXTENSION);
1039 dictinsert(d, "C_EXTENSION", v);
1040 XDECREF(v);
1041
Jack Jansenae12e191995-06-18 20:06:44 +00001042#ifdef macintosh
1043 v = newintobject(PY_RESOURCE);
1044 dictinsert(d, "PY_RESOURCE", v);
1045 XDECREF(v);
1046#endif
1047
1048
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001049 if (err_occurred())
1050 fatal("imp module initialization failed");
1051}