blob: de1772cc80e878d0fbc03dd7dc869284c86f0cfe [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 Rossum8861b741996-07-30 16:49:37 +000069#define MAGIC (5892 | ((long)'\r'<<16) | ((long)'\n'<<24))
Guido van Rossum3ddee711991-12-16 13:06:34 +000070
Guido van Rossum1ae940a1995-01-02 19:04:15 +000071object *import_modules; /* This becomes sys.modules */
Guido van Rossum3f5da241990-12-20 15:06:42 +000072
Guido van Rossum66f1fa81991-04-03 19:03:52 +000073
Guido van Rossum1ae940a1995-01-02 19:04:15 +000074/* Initialize things */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075
76void
77initimport()
78{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000079 if (import_modules != NULL)
80 fatal("duplicate initimport() call");
81 if ((import_modules = newdictobject()) == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +000082 fatal("no mem for dictionary of modules");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083}
84
Guido van Rossum3f5da241990-12-20 15:06:42 +000085
Guido van Rossum1ae940a1995-01-02 19:04:15 +000086/* Un-initialize things, as good as we can */
Guido van Rossum3f5da241990-12-20 15:06:42 +000087
Guido van Rossum3f5da241990-12-20 15:06:42 +000088void
89doneimport()
90{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000091 if (import_modules != NULL) {
Guido van Rossum0de81bf1995-01-26 00:41:28 +000092 object *tmp = import_modules;
93 import_modules = NULL;
94 /* This deletes all modules from sys.modules.
95 When a module is deallocated, it in turn clears its dictionary,
96 thus hopefully breaking any circular references between modules
97 and between a module's dictionary and its functions.
98 Note that "import" will fail while we are cleaning up.
99 */
100 mappingclear(tmp);
101 DECREF(tmp);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000102 }
Guido van Rossum8d15b5d1990-10-26 14:58:58 +0000103}
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000104
105
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000106/* Helper for pythonrun.c -- return magic number */
107
108long
109get_pyc_magic()
110{
111 return MAGIC;
112}
113
114
115/* Helper for sysmodule.c -- return modules dictionary */
116
117object *
118get_modules()
119{
120 return import_modules;
121}
122
123
124/* Get the module object corresponding to a module name.
125 First check the modules dictionary if there's one there,
126 if not, create a new one and insert in in the modules dictionary.
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000127 Because the former action is most common, THIS DOES NOT RETURN A
128 'NEW' REFERENCE! */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000129
130object *
131add_module(name)
132 char *name;
133{
134 object *m;
135
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000136 if (import_modules == NULL) {
137 err_setstr(SystemError, "sys.modules has been deleted");
138 return NULL;
139 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000140 if ((m = dictlookup(import_modules, name)) != NULL &&
141 is_moduleobject(m))
142 return m;
143 m = newmoduleobject(name);
144 if (m == NULL)
145 return NULL;
146 if (dictinsert(import_modules, name, m) != 0) {
147 DECREF(m);
148 return NULL;
149 }
150 DECREF(m); /* Yes, it still exists, in modules! */
151
152 return m;
153}
154
155
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000156/* Execute a code object in a module and return the module object
157 WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000158
Jack Jansen9c96a921995-02-15 22:57:06 +0000159object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000160exec_code_module(name, co)
161 char *name;
Jack Jansen9c96a921995-02-15 22:57:06 +0000162 object *co;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000163{
164 object *m, *d, *v;
165
166 m = add_module(name);
167 if (m == NULL)
168 return NULL;
169 d = getmoduledict(m);
Guido van Rossum6135a871995-01-09 17:53:26 +0000170 if (dictlookup(d, "__builtins__") == NULL) {
Guido van Rossum94390ec1995-01-12 11:37:57 +0000171 if (dictinsert(d, "__builtins__", getbuiltins()) != 0)
Guido van Rossum6135a871995-01-09 17:53:26 +0000172 return NULL;
173 }
Guido van Rossum9c9a07c1996-05-16 20:43:40 +0000174 /* Remember the filename as the __file__ attribute */
175 if (dictinsert(d, "__file__", ((codeobject *)co)->co_filename) != 0)
176 err_clear(); /* Not important enough to report */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000177 v = eval_code((codeobject *)co, d, d); /* XXX owner? */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000178 if (v == NULL)
179 return NULL;
180 DECREF(v);
181 INCREF(m);
182
183 return m;
184}
185
186
187/* Given a pathname for a Python source file, fill a buffer with the
188 pathname for the corresponding compiled file. Return the pathname
189 for the compiled file, or NULL if there's no space in the buffer.
190 Doesn't set an exception. */
191
192static char *
193make_compiled_pathname(pathname, buf, buflen)
194 char *pathname;
195 char *buf;
196 int buflen;
197{
198 int len;
199
200 len = strlen(pathname);
201 if (len+2 > buflen)
202 return NULL;
203 strcpy(buf, pathname);
204 strcpy(buf+len, "c");
205
206 return buf;
207}
208
209
210/* Given a pathname for a Python source file, its time of last
211 modification, and a pathname for a compiled file, check whether the
212 compiled file represents the same version of the source. If so,
213 return a FILE pointer for the compiled file, positioned just after
214 the header; if not, return NULL.
215 Doesn't set an exception. */
216
217static FILE *
218check_compiled_module(pathname, mtime, cpathname)
219 char *pathname;
220 long mtime;
221 char *cpathname;
222{
223 FILE *fp;
224 long magic;
225 long pyc_mtime;
226
227 fp = fopen(cpathname, "rb");
228 if (fp == NULL)
229 return NULL;
230 magic = rd_long(fp);
231 if (magic != MAGIC) {
232 if (verbose)
233 fprintf(stderr, "# %s has bad magic\n", cpathname);
234 fclose(fp);
235 return NULL;
236 }
237 pyc_mtime = rd_long(fp);
238 if (pyc_mtime != mtime) {
239 if (verbose)
240 fprintf(stderr, "# %s has bad mtime\n", cpathname);
241 fclose(fp);
242 return NULL;
243 }
244 if (verbose)
245 fprintf(stderr, "# %s matches %s\n", cpathname, pathname);
246 return fp;
247}
248
249
250/* Read a code object from a file and check it for validity */
251
252static codeobject *
253read_compiled_module(fp)
254 FILE *fp;
255{
256 object *co;
257
258 co = rd_object(fp);
259 /* Ugly: rd_object() may return NULL with or without error */
260 if (co == NULL || !is_codeobject(co)) {
261 if (!err_occurred())
262 err_setstr(ImportError,
263 "Non-code object in .pyc file");
264 XDECREF(co);
265 return NULL;
266 }
267 return (codeobject *)co;
268}
269
270
271/* Load a module from a compiled file, execute it, and return its
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000272 module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000273
274static object *
275load_compiled_module(name, cpathname, fp)
276 char *name;
277 char *cpathname;
278 FILE *fp;
279{
280 long magic;
281 codeobject *co;
282 object *m;
283
284 magic = rd_long(fp);
285 if (magic != MAGIC) {
286 err_setstr(ImportError, "Bad magic number in .pyc file");
287 return NULL;
288 }
289 (void) rd_long(fp);
290 co = read_compiled_module(fp);
291 if (co == NULL)
292 return NULL;
293 if (verbose)
294 fprintf(stderr, "import %s # precompiled from %s\n",
295 name, cpathname);
Jack Jansen9c96a921995-02-15 22:57:06 +0000296 m = exec_code_module(name, (object *)co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000297 DECREF(co);
298
299 return m;
300}
301
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000302/* Parse a source file and return the corresponding code object */
303
304static codeobject *
305parse_source_module(pathname, fp)
306 char *pathname;
307 FILE *fp;
308{
309 codeobject *co;
310 node *n;
311
312 n = parse_file(fp, pathname, file_input);
313 if (n == NULL)
314 return NULL;
315 co = compile(n, pathname);
316 freetree(n);
317
318 return co;
319}
320
321
322/* Write a compiled module to a file, placing the time of last
323 modification of its source into the header.
324 Errors are ignored, if a write error occurs an attempt is made to
325 remove the file. */
326
327static void
328write_compiled_module(co, cpathname, mtime)
329 codeobject *co;
330 char *cpathname;
331 long mtime;
332{
333 FILE *fp;
334
335 fp = fopen(cpathname, "wb");
336 if (fp == NULL) {
337 if (verbose)
338 fprintf(stderr,
339 "# can't create %s\n", cpathname);
340 return;
341 }
342 wr_long(MAGIC, fp);
343 /* First write a 0 for mtime */
344 wr_long(0L, fp);
345 wr_object((object *)co, fp);
346 if (ferror(fp)) {
347 if (verbose)
348 fprintf(stderr, "# can't write %s\n", cpathname);
349 /* Don't keep partial file */
350 fclose(fp);
351 (void) unlink(cpathname);
352 return;
353 }
354 /* Now write the true mtime */
355 fseek(fp, 4L, 0);
356 wr_long(mtime, fp);
357 fflush(fp);
358 fclose(fp);
359 if (verbose)
360 fprintf(stderr, "# wrote %s\n", cpathname);
361#ifdef macintosh
Guido van Rossumbe1a6e21996-02-21 15:29:20 +0000362 setfiletype(cpathname, 'Pyth', 'PYC ');
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000363#endif
364}
365
366
367/* Load a source module from a given file and return its module
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000368 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
369 byte-compiled file, use that instead. */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000370
371static object *
372load_source_module(name, pathname, fp)
373 char *name;
374 char *pathname;
375 FILE *fp;
376{
377 long mtime;
378 FILE *fpc;
379 char buf[MAXPATHLEN+1];
380 char *cpathname;
381 codeobject *co;
382 object *m;
383
384 mtime = getmtime(pathname);
385 cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1);
386 if (cpathname != NULL &&
387 (fpc = check_compiled_module(pathname, mtime, cpathname))) {
388 co = read_compiled_module(fpc);
389 fclose(fpc);
390 if (co == NULL)
391 return NULL;
392 if (verbose)
393 fprintf(stderr, "import %s # precompiled from %s\n",
394 name, cpathname);
395 }
396 else {
397 co = parse_source_module(pathname, fp);
398 if (co == NULL)
399 return NULL;
400 if (verbose)
401 fprintf(stderr, "import %s # from %s\n",
402 name, pathname);
403 write_compiled_module(co, cpathname, mtime);
404 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000405 m = exec_code_module(name, (object *)co);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000406 DECREF(co);
407
408 return m;
409}
410
411
412/* Search the path (default sys.path) for a module. Return the
413 corresponding filedescr struct, and (via return arguments) the
414 pathname and an open file. Return NULL if the module is not found. */
415
416static struct filedescr *
417find_module(name, path, buf, buflen, p_fp)
418 char *name;
419 object *path;
420 /* Output parameters: */
421 char *buf;
422 int buflen;
423 FILE **p_fp;
424{
425 int i, npath, len, namelen;
Guido van Rossum80bb9651996-12-05 23:27:02 +0000426 struct filedescr *fdp = NULL;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000427 FILE *fp = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000428
Guido van Rossumac279101996-08-22 23:10:58 +0000429#ifdef MS_COREDLL
Guido van Rossuma5a3db71996-04-09 02:39:59 +0000430 if ((fp=PyWin_FindRegisteredModule(name, &fdp, buf, buflen))!=NULL) {
431 *p_fp = fp;
432 return fdp;
433 }
434#endif
435
436
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000437 if (path == NULL)
438 path = sysget("path");
439 if (path == NULL || !is_listobject(path)) {
440 err_setstr(ImportError,
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000441 "sys.path must be a list of directory names");
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000442 return NULL;
443 }
444 npath = getlistsize(path);
445 namelen = strlen(name);
446 for (i = 0; i < npath; i++) {
447 object *v = getlistitem(path, i);
448 if (!is_stringobject(v))
449 continue;
450 len = getstringsize(v);
451 if (len + 2 + namelen + import_maxsuffixsize >= buflen)
452 continue; /* Too long */
453 strcpy(buf, getstringvalue(v));
454 if (strlen(buf) != len)
455 continue; /* v contains '\0' */
Jack Jansen9c96a921995-02-15 22:57:06 +0000456#ifdef macintosh
457 if ( PyMac_FindResourceModule(name, buf) ) {
458 static struct filedescr resfiledescr = { "", "", PY_RESOURCE};
459
460 return &resfiledescr;
461 }
462#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000463 if (len > 0 && buf[len-1] != SEP)
464 buf[len++] = SEP;
Guido van Rossum40f470f1996-05-23 22:51:04 +0000465#ifdef IMPORT_8x3_NAMES
466 /* see if we are searching in directory dos_8x3 */
467 if (len > 7 && !strncmp(buf + len - 8, "dos_8x3", 7)){
468 int j;
469 char ch; /* limit name to eight lower-case characters */
470 for (j = 0; (ch = name[j]) && j < 8; j++)
471 if (isupper(ch))
472 buf[len++] = tolower(ch);
473 else
474 buf[len++] = ch;
475 }
Guido van Rossum0e41c8c1996-06-20 14:18:34 +0000476 else /* Not in dos_8x3, use the full name */
Guido van Rossum6f489d91996-06-28 20:15:15 +0000477#endif
Guido van Rossum0e41c8c1996-06-20 14:18:34 +0000478 {
Guido van Rossum40f470f1996-05-23 22:51:04 +0000479 strcpy(buf+len, name);
480 len += namelen;
481 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000482 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
483 strcpy(buf+len, fdp->suffix);
484 if (verbose > 1)
485 fprintf(stderr, "# trying %s\n", buf);
486 fp = fopen(buf, fdp->mode);
487 if (fp != NULL)
488 break;
489 }
490 if (fp != NULL)
491 break;
492 }
493 if (fp == NULL) {
494 char buf[256];
495 sprintf(buf, "No module named %.200s", name);
496 err_setstr(ImportError, buf);
497 return NULL;
498 }
499
500 *p_fp = fp;
501 return fdp;
502}
503
504
505/* Load an external module using the default search path and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000506 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000507
508static object *
509load_module(name)
510 char *name;
511{
512 char buf[MAXPATHLEN+1];
513 struct filedescr *fdp;
514 FILE *fp = NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000515 object *m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000516
517 fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp);
518 if (fdp == NULL)
519 return NULL;
520
521 switch (fdp->type) {
522
523 case PY_SOURCE:
524 m = load_source_module(name, buf, fp);
525 break;
526
527 case PY_COMPILED:
528 m = load_compiled_module(name, buf, fp);
529 break;
530
531 case C_EXTENSION:
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +0000532 m = load_dynamic_module(name, buf, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000533 break;
534
Jack Jansen9c96a921995-02-15 22:57:06 +0000535#ifdef macintosh
536 case PY_RESOURCE:
537 m = PyMac_LoadResourceModule(name, buf);
538 break;
539#endif
540
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000541 default:
542 err_setstr(SystemError,
543 "find_module returned unexpected result");
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000544 m = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000545
546 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000547 if ( fp )
548 fclose(fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000549
550 return m;
551}
552
553
554/* Initialize a built-in module.
555 Return 1 for succes, 0 if the module is not found, and -1 with
556 an exception set if the initialization failed. */
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000557
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000558static int
559init_builtin(name)
560 char *name;
561{
562 int i;
563 for (i = 0; inittab[i].name != NULL; i++) {
564 if (strcmp(name, inittab[i].name) == 0) {
Guido van Rossum74e6a111994-08-29 12:54:38 +0000565 if (inittab[i].initfunc == NULL) {
566 err_setstr(ImportError,
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000567 "Cannot re-init internal module");
Guido van Rossum74e6a111994-08-29 12:54:38 +0000568 return -1;
569 }
Guido van Rossum4cd8b5c1992-03-27 17:21:04 +0000570 if (verbose)
571 fprintf(stderr, "import %s # builtin\n",
572 name);
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000573 (*inittab[i].initfunc)();
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000574 if (err_occurred())
575 return -1;
Guido van Rossum7f133ed1991-02-19 12:23:57 +0000576 return 1;
577 }
578 }
579 return 0;
580}
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000581
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000582
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000583/* Frozen modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000584
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000585static struct _frozen *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000586find_frozen(name)
587 char *name;
588{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000589 struct _frozen *p;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000590
591 for (p = frozen_modules; ; p++) {
592 if (p->name == NULL)
593 return NULL;
594 if (strcmp(p->name, name) == 0)
595 break;
596 }
597 return p;
598}
599
600static object *
601get_frozen_object(name)
602 char *name;
603{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000604 struct _frozen *p = find_frozen(name);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000605
606 if (p == NULL) {
607 err_setstr(ImportError, "No such frozen object");
608 return NULL;
609 }
Guido van Rossum1741d601996-08-08 18:52:59 +0000610 return rds_object((char *)p->code, p->size);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000611}
612
613/* Initialize a frozen module.
614 Return 1 for succes, 0 if the module is not found, and -1 with
615 an exception set if the initialization failed.
616 This function is also used from frozenmain.c */
Guido van Rossum0b344901995-02-07 15:35:27 +0000617
618int
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000619init_frozen(name)
620 char *name;
621{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000622 struct _frozen *p = find_frozen(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000623 object *co;
624 object *m;
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000625
626 if (p == NULL)
627 return 0;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000628 if (verbose)
629 fprintf(stderr, "import %s # frozen\n", name);
Guido van Rossum1741d601996-08-08 18:52:59 +0000630 co = rds_object((char *)p->code, p->size);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000631 if (co == NULL)
632 return -1;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000633 if (!is_codeobject(co)) {
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000634 DECREF(co);
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000635 err_setstr(TypeError, "frozen object is not a code object");
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000636 return -1;
637 }
Jack Jansen9c96a921995-02-15 22:57:06 +0000638 m = exec_code_module(name, co);
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000639 DECREF(co);
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000640 if (m == NULL)
641 return -1;
642 DECREF(m);
643 return 1;
Guido van Rossumf56e3db1993-04-01 20:59:32 +0000644}
Guido van Rossum74e6a111994-08-29 12:54:38 +0000645
646
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000647/* Import a module, either built-in, frozen, or external, and return
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000648 its module object WITH INCREMENTED REFERENCE COUNT */
Guido van Rossum74e6a111994-08-29 12:54:38 +0000649
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000650object *
651import_module(name)
652 char *name;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000653{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000654 object *m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000655
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000656 if (import_modules == NULL) {
657 err_setstr(SystemError, "sys.modules has been deleted");
658 return NULL;
659 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000660 if ((m = dictlookup(import_modules, name)) != NULL) {
661 INCREF(m);
662 }
663 else {
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000664 int i;
665 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
666 if (i < 0)
667 return NULL;
668 if ((m = dictlookup(import_modules, name)) == NULL) {
669 if (err_occurred() == NULL)
670 err_setstr(SystemError,
671 "built-in module not initialized properly");
672 }
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000673 else
674 INCREF(m);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000675 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000676 else
677 m = load_module(name);
Guido van Rossum74e6a111994-08-29 12:54:38 +0000678 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000679
680 return m;
Guido van Rossum74e6a111994-08-29 12:54:38 +0000681}
682
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000683
684/* Re-import a module of any kind and return its module object, WITH
685 INCREMENTED REFERENCE COUNT */
686
687object *
688reload_module(m)
689 object *m;
690{
691 char *name;
692 int i;
693
694 if (m == NULL || !is_moduleobject(m)) {
695 err_setstr(TypeError, "reload() argument must be module");
696 return NULL;
697 }
698 name = getmodulename(m);
699 if (name == NULL)
700 return NULL;
Guido van Rossum0de81bf1995-01-26 00:41:28 +0000701 if (import_modules == NULL) {
702 err_setstr(SystemError, "sys.modules has been deleted");
703 return NULL;
704 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000705 if (m != dictlookup(import_modules, name)) {
706 err_setstr(ImportError, "reload() module not in sys.modules");
707 return NULL;
708 }
709 /* Check for built-in and frozen modules */
710 if ((i = init_builtin(name)) || (i = init_frozen(name))) {
711 if (i < 0)
712 return NULL;
Guido van Rossum7f9fa971995-01-20 16:53:12 +0000713 INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000714 }
715 else
716 m = load_module(name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000717 return m;
718}
719
720
721/* Module 'imp' provides Python access to the primitives used for
722 importing modules.
723*/
724
725static object *
726imp_get_magic(self, args)
727 object *self;
728 object *args;
729{
730 char buf[4];
731
732 if (!newgetargs(args, ""))
733 return NULL;
734 buf[0] = (MAGIC >> 0) & 0xff;
735 buf[1] = (MAGIC >> 8) & 0xff;
Guido van Rossum90f0e071995-01-30 12:53:06 +0000736 buf[2] = (MAGIC >> 16) & 0xff;
737 buf[3] = (MAGIC >> 24) & 0xff;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000738
739 return newsizedstringobject(buf, 4);
740}
741
742static object *
743imp_get_suffixes(self, args)
744 object *self;
745 object *args;
746{
747 object *list;
748 struct filedescr *fdp;
749
750 if (!newgetargs(args, ""))
751 return NULL;
752 list = newlistobject(0);
753 if (list == NULL)
754 return NULL;
755 for (fdp = import_filetab; fdp->suffix != NULL; fdp++) {
756 object *item = mkvalue("ssi",
757 fdp->suffix, fdp->mode, fdp->type);
758 if (item == NULL) {
759 DECREF(list);
760 return NULL;
761 }
762 if (addlistitem(list, item) < 0) {
763 DECREF(list);
764 DECREF(item);
765 return NULL;
766 }
767 DECREF(item);
768 }
769 return list;
770}
771
772static object *
773imp_find_module(self, args)
774 object *self;
775 object *args;
776{
777 extern int fclose PROTO((FILE *));
778 char *name;
779 object *path = NULL;
780 object *fob, *ret;
781 struct filedescr *fdp;
782 char pathname[MAXPATHLEN+1];
783 FILE *fp;
784 if (!newgetargs(args, "s|O!", &name, &Listtype, &path))
785 return NULL;
786 fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp);
787 if (fdp == NULL)
788 return NULL;
789 fob = newopenfileobject(fp, pathname, fdp->mode, fclose);
790 if (fob == NULL) {
791 fclose(fp);
792 return NULL;
793 }
794 ret = mkvalue("Os(ssi)",
795 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
796 DECREF(fob);
797 return ret;
798}
799
800static object *
801imp_init_builtin(self, args)
802 object *self;
803 object *args;
804{
805 char *name;
806 int ret;
807 object *m;
808 if (!newgetargs(args, "s", &name))
809 return NULL;
810 ret = init_builtin(name);
811 if (ret < 0)
812 return NULL;
813 if (ret == 0) {
814 INCREF(None);
815 return None;
816 }
817 m = add_module(name);
818 XINCREF(m);
819 return m;
820}
821
822static object *
823imp_init_frozen(self, args)
824 object *self;
825 object *args;
826{
827 char *name;
828 int ret;
829 object *m;
830 if (!newgetargs(args, "s", &name))
831 return NULL;
832 ret = init_frozen(name);
833 if (ret < 0)
834 return NULL;
835 if (ret == 0) {
836 INCREF(None);
837 return None;
838 }
839 m = add_module(name);
840 XINCREF(m);
841 return m;
842}
843
844static object *
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000845imp_get_frozen_object(self, args)
846 object *self;
847 object *args;
848{
849 char *name;
Jack Jansen95ffa231995-10-03 14:38:41 +0000850
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000851 if (!newgetargs(args, "s", &name))
852 return NULL;
853 return get_frozen_object(name);
854}
855
856static object *
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000857imp_is_builtin(self, args)
858 object *self;
859 object *args;
860{
861 int i;
862 char *name;
863 if (!newgetargs(args, "s", &name))
864 return NULL;
865 for (i = 0; inittab[i].name != NULL; i++) {
866 if (strcmp(name, inittab[i].name) == 0) {
867 if (inittab[i].initfunc == NULL)
868 return newintobject(-1);
869 else
870 return newintobject(1);
871 }
872 }
873 return newintobject(0);
874}
875
876static object *
877imp_is_frozen(self, args)
878 object *self;
879 object *args;
880{
Guido van Rossumcfd0a221996-06-17 17:06:34 +0000881 struct _frozen *p;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000882 char *name;
883 if (!newgetargs(args, "s", &name))
884 return NULL;
885 for (p = frozen_modules; ; p++) {
886 if (p->name == NULL)
887 break;
888 if (strcmp(p->name, name) == 0)
889 return newintobject(1);
890 }
891 return newintobject(0);
892}
893
894static FILE *
895get_file(pathname, fob, mode)
896 char *pathname;
897 object *fob;
898 char *mode;
899{
900 FILE *fp;
901 if (fob == NULL) {
902 fp = fopen(pathname, mode);
903 if (fp == NULL)
904 err_errno(IOError);
905 }
906 else {
907 fp = getfilefile(fob);
908 if (fp == NULL)
909 err_setstr(ValueError, "bad/closed file object");
910 }
911 return fp;
912}
913
914static object *
915imp_load_compiled(self, args)
916 object *self;
917 object *args;
918{
919 char *name;
920 char *pathname;
921 object *fob = NULL;
922 object *m;
923 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000924 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000925 return NULL;
926 fp = get_file(pathname, fob, "rb");
927 if (fp == NULL)
928 return NULL;
929 m = load_compiled_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000930 return m;
931}
932
933static object *
934imp_load_dynamic(self, args)
935 object *self;
936 object *args;
937{
938 char *name;
939 char *pathname;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000940 object *fob = NULL;
941 object *m;
942 FILE *fp = NULL;
943 if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000944 return NULL;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000945 if (fob)
946 fp = get_file(pathname, fob, "r");
947 m = load_dynamic_module(name, pathname, fp);
Guido van Rossum7faeab31995-07-07 22:50:36 +0000948 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000949}
950
951static object *
952imp_load_source(self, args)
953 object *self;
954 object *args;
955{
956 char *name;
957 char *pathname;
958 object *fob = NULL;
959 object *m;
960 FILE *fp;
Guido van Rossum7faeab31995-07-07 22:50:36 +0000961 if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob))
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000962 return NULL;
963 fp = get_file(pathname, fob, "r");
964 if (fp == NULL)
965 return NULL;
966 m = load_source_module(name, pathname, fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000967 return m;
968}
969
Jack Jansen9c96a921995-02-15 22:57:06 +0000970#ifdef macintosh
971static object *
972imp_load_resource(self, args)
973 object *self;
974 object *args;
975{
976 char *name;
977 char *pathname;
978 object *m;
979
980 if (!newgetargs(args, "ss", &name, &pathname))
981 return NULL;
982 m = PyMac_LoadResourceModule(name, pathname);
983 return m;
984}
985#endif /* macintosh */
986
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000987static object *
988imp_new_module(self, args)
989 object *self;
990 object *args;
991{
992 char *name;
993 if (!newgetargs(args, "s", &name))
994 return NULL;
995 return newmoduleobject(name);
996}
997
998static struct methodlist imp_methods[] = {
Guido van Rossum6ec1efb1995-08-04 04:08:57 +0000999 {"get_frozen_object", imp_get_frozen_object, 1},
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001000 {"get_magic", imp_get_magic, 1},
1001 {"get_suffixes", imp_get_suffixes, 1},
1002 {"find_module", imp_find_module, 1},
1003 {"init_builtin", imp_init_builtin, 1},
1004 {"init_frozen", imp_init_frozen, 1},
1005 {"is_builtin", imp_is_builtin, 1},
1006 {"is_frozen", imp_is_frozen, 1},
1007 {"load_compiled", imp_load_compiled, 1},
1008 {"load_dynamic", imp_load_dynamic, 1},
1009 {"load_source", imp_load_source, 1},
1010 {"new_module", imp_new_module, 1},
Jack Jansen9c96a921995-02-15 22:57:06 +00001011#ifdef macintosh
1012 {"load_resource", imp_load_resource, 1},
1013#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001014 {NULL, NULL} /* sentinel */
1015};
1016
1017void
1018initimp()
1019{
1020 object *m, *d, *v;
1021
1022 m = initmodule("imp", imp_methods);
1023 d = getmoduledict(m);
1024
1025 v = newintobject(SEARCH_ERROR);
1026 dictinsert(d, "SEARCH_ERROR", v);
1027 XDECREF(v);
1028
1029 v = newintobject(PY_SOURCE);
1030 dictinsert(d, "PY_SOURCE", v);
1031 XDECREF(v);
1032
1033 v = newintobject(PY_COMPILED);
1034 dictinsert(d, "PY_COMPILED", v);
1035 XDECREF(v);
1036
1037 v = newintobject(C_EXTENSION);
1038 dictinsert(d, "C_EXTENSION", v);
1039 XDECREF(v);
1040
Jack Jansenae12e191995-06-18 20:06:44 +00001041#ifdef macintosh
1042 v = newintobject(PY_RESOURCE);
1043 dictinsert(d, "PY_RESOURCE", v);
1044 XDECREF(v);
1045#endif
1046
1047
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001048 if (err_occurred())
1049 fatal("imp module initialization failed");
1050}