Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Module definition and import implementation */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "allobjects.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 29 | #include "node.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 30 | #include "token.h" |
| 31 | #include "graminit.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | #include "import.h" |
| 33 | #include "errcode.h" |
| 34 | #include "sysmodule.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 35 | #include "pythonrun.h" |
Guido van Rossum | c405b7b | 1991-06-04 19:39:42 +0000 | [diff] [blame] | 36 | #include "marshal.h" |
| 37 | #include "compile.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 38 | #include "eval.h" |
Guido van Rossum | d8bac6d | 1992-02-26 15:19:13 +0000 | [diff] [blame] | 39 | #include "osdefs.h" |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 40 | #include "importdl.h" |
Guido van Rossum | c405b7b | 1991-06-04 19:39:42 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 42 | extern int verbose; /* Defined in pythonrun.c */ |
Guido van Rossum | 4cd8b5c | 1992-03-27 17:21:04 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 44 | extern long getmtime(); /* In getmtime.c */ |
Guido van Rossum | 21d335e | 1993-10-15 13:01:11 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 6c84969 | 1994-09-26 15:47:17 +0000 | [diff] [blame] | 46 | /* Magic word to reject .pyc files generated by other Python versions */ |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 47 | #define MAGIC 0x999903L /* Increment by one for each incompatible change */ |
Guido van Rossum | 3ddee71 | 1991-12-16 13:06:34 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 49 | object *import_modules; /* This becomes sys.modules */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | 66f1fa8 | 1991-04-03 19:03:52 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 52 | /* Initialize things */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 53 | |
| 54 | void |
| 55 | initimport() |
| 56 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 57 | if (import_modules != NULL) |
| 58 | fatal("duplicate initimport() call"); |
| 59 | if ((import_modules = newdictobject()) == NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 60 | fatal("no mem for dictionary of modules"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 64 | /* Un-initialize things, as good as we can */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 66 | void |
| 67 | doneimport() |
| 68 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 69 | if (import_modules != NULL) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 70 | int pos; |
| 71 | object *modname, *module; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 72 | /* Explicitly erase all modules; this is the safest way |
| 73 | to get rid of at least *some* circular dependencies */ |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 74 | pos = 0; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 75 | while (mappinggetnext(import_modules, |
| 76 | &pos, &modname, &module)) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 77 | if (is_moduleobject(module)) { |
| 78 | object *dict; |
| 79 | dict = getmoduledict(module); |
| 80 | if (dict != NULL && is_dictobject(dict)) |
| 81 | mappingclear(dict); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 84 | mappingclear(import_modules); |
| 85 | DECREF(import_modules); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 86 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 87 | import_modules = NULL; |
Guido van Rossum | 8d15b5d | 1990-10-26 14:58:58 +0000 | [diff] [blame] | 88 | } |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 89 | |
| 90 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 91 | /* Helper for pythonrun.c -- return magic number */ |
| 92 | |
| 93 | long |
| 94 | get_pyc_magic() |
| 95 | { |
| 96 | return MAGIC; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | /* Helper for sysmodule.c -- return modules dictionary */ |
| 101 | |
| 102 | object * |
| 103 | get_modules() |
| 104 | { |
| 105 | return import_modules; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /* Get the module object corresponding to a module name. |
| 110 | First check the modules dictionary if there's one there, |
| 111 | if not, create a new one and insert in in the modules dictionary. |
| 112 | Because the former action is most common, this does not return a |
| 113 | 'new' reference! */ |
| 114 | |
| 115 | object * |
| 116 | add_module(name) |
| 117 | char *name; |
| 118 | { |
| 119 | object *m; |
| 120 | |
| 121 | if ((m = dictlookup(import_modules, name)) != NULL && |
| 122 | is_moduleobject(m)) |
| 123 | return m; |
| 124 | m = newmoduleobject(name); |
| 125 | if (m == NULL) |
| 126 | return NULL; |
| 127 | if (dictinsert(import_modules, name, m) != 0) { |
| 128 | DECREF(m); |
| 129 | return NULL; |
| 130 | } |
| 131 | DECREF(m); /* Yes, it still exists, in modules! */ |
| 132 | |
| 133 | return m; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /* Execute a code object in a module and return its module object */ |
| 138 | |
| 139 | static object * |
| 140 | exec_code_module(name, co) |
| 141 | char *name; |
| 142 | codeobject *co; |
| 143 | { |
| 144 | object *m, *d, *v; |
| 145 | |
| 146 | m = add_module(name); |
| 147 | if (m == NULL) |
| 148 | return NULL; |
| 149 | d = getmoduledict(m); |
| 150 | v = eval_code((codeobject *)co, d, d, d, (object *)NULL); |
| 151 | if (v == NULL) |
| 152 | return NULL; |
| 153 | DECREF(v); |
| 154 | INCREF(m); |
| 155 | |
| 156 | return m; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /* Given a pathname for a Python source file, fill a buffer with the |
| 161 | pathname for the corresponding compiled file. Return the pathname |
| 162 | for the compiled file, or NULL if there's no space in the buffer. |
| 163 | Doesn't set an exception. */ |
| 164 | |
| 165 | static char * |
| 166 | make_compiled_pathname(pathname, buf, buflen) |
| 167 | char *pathname; |
| 168 | char *buf; |
| 169 | int buflen; |
| 170 | { |
| 171 | int len; |
| 172 | |
| 173 | len = strlen(pathname); |
| 174 | if (len+2 > buflen) |
| 175 | return NULL; |
| 176 | strcpy(buf, pathname); |
| 177 | strcpy(buf+len, "c"); |
| 178 | |
| 179 | return buf; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /* Given a pathname for a Python source file, its time of last |
| 184 | modification, and a pathname for a compiled file, check whether the |
| 185 | compiled file represents the same version of the source. If so, |
| 186 | return a FILE pointer for the compiled file, positioned just after |
| 187 | the header; if not, return NULL. |
| 188 | Doesn't set an exception. */ |
| 189 | |
| 190 | static FILE * |
| 191 | check_compiled_module(pathname, mtime, cpathname) |
| 192 | char *pathname; |
| 193 | long mtime; |
| 194 | char *cpathname; |
| 195 | { |
| 196 | FILE *fp; |
| 197 | long magic; |
| 198 | long pyc_mtime; |
| 199 | |
| 200 | fp = fopen(cpathname, "rb"); |
| 201 | if (fp == NULL) |
| 202 | return NULL; |
| 203 | magic = rd_long(fp); |
| 204 | if (magic != MAGIC) { |
| 205 | if (verbose) |
| 206 | fprintf(stderr, "# %s has bad magic\n", cpathname); |
| 207 | fclose(fp); |
| 208 | return NULL; |
| 209 | } |
| 210 | pyc_mtime = rd_long(fp); |
| 211 | if (pyc_mtime != mtime) { |
| 212 | if (verbose) |
| 213 | fprintf(stderr, "# %s has bad mtime\n", cpathname); |
| 214 | fclose(fp); |
| 215 | return NULL; |
| 216 | } |
| 217 | if (verbose) |
| 218 | fprintf(stderr, "# %s matches %s\n", cpathname, pathname); |
| 219 | return fp; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /* Read a code object from a file and check it for validity */ |
| 224 | |
| 225 | static codeobject * |
| 226 | read_compiled_module(fp) |
| 227 | FILE *fp; |
| 228 | { |
| 229 | object *co; |
| 230 | |
| 231 | co = rd_object(fp); |
| 232 | /* Ugly: rd_object() may return NULL with or without error */ |
| 233 | if (co == NULL || !is_codeobject(co)) { |
| 234 | if (!err_occurred()) |
| 235 | err_setstr(ImportError, |
| 236 | "Non-code object in .pyc file"); |
| 237 | XDECREF(co); |
| 238 | return NULL; |
| 239 | } |
| 240 | return (codeobject *)co; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /* Load a module from a compiled file, execute it, and return its |
| 245 | module object */ |
| 246 | |
| 247 | static object * |
| 248 | load_compiled_module(name, cpathname, fp) |
| 249 | char *name; |
| 250 | char *cpathname; |
| 251 | FILE *fp; |
| 252 | { |
| 253 | long magic; |
| 254 | codeobject *co; |
| 255 | object *m; |
| 256 | |
| 257 | magic = rd_long(fp); |
| 258 | if (magic != MAGIC) { |
| 259 | err_setstr(ImportError, "Bad magic number in .pyc file"); |
| 260 | return NULL; |
| 261 | } |
| 262 | (void) rd_long(fp); |
| 263 | co = read_compiled_module(fp); |
| 264 | if (co == NULL) |
| 265 | return NULL; |
| 266 | if (verbose) |
| 267 | fprintf(stderr, "import %s # precompiled from %s\n", |
| 268 | name, cpathname); |
| 269 | m = exec_code_module(name, co); |
| 270 | DECREF(co); |
| 271 | |
| 272 | return m; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | /* Parse a source file and return the corresponding code object */ |
| 277 | |
| 278 | static codeobject * |
| 279 | parse_source_module(pathname, fp) |
| 280 | char *pathname; |
| 281 | FILE *fp; |
| 282 | { |
| 283 | codeobject *co; |
| 284 | node *n; |
| 285 | |
| 286 | n = parse_file(fp, pathname, file_input); |
| 287 | if (n == NULL) |
| 288 | return NULL; |
| 289 | co = compile(n, pathname); |
| 290 | freetree(n); |
| 291 | |
| 292 | return co; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | /* Write a compiled module to a file, placing the time of last |
| 297 | modification of its source into the header. |
| 298 | Errors are ignored, if a write error occurs an attempt is made to |
| 299 | remove the file. */ |
| 300 | |
| 301 | static void |
| 302 | write_compiled_module(co, cpathname, mtime) |
| 303 | codeobject *co; |
| 304 | char *cpathname; |
| 305 | long mtime; |
| 306 | { |
| 307 | FILE *fp; |
| 308 | |
| 309 | fp = fopen(cpathname, "wb"); |
| 310 | if (fp == NULL) { |
| 311 | if (verbose) |
| 312 | fprintf(stderr, |
| 313 | "# can't create %s\n", cpathname); |
| 314 | return; |
| 315 | } |
| 316 | wr_long(MAGIC, fp); |
| 317 | /* First write a 0 for mtime */ |
| 318 | wr_long(0L, fp); |
| 319 | wr_object((object *)co, fp); |
| 320 | if (ferror(fp)) { |
| 321 | if (verbose) |
| 322 | fprintf(stderr, "# can't write %s\n", cpathname); |
| 323 | /* Don't keep partial file */ |
| 324 | fclose(fp); |
| 325 | (void) unlink(cpathname); |
| 326 | return; |
| 327 | } |
| 328 | /* Now write the true mtime */ |
| 329 | fseek(fp, 4L, 0); |
| 330 | wr_long(mtime, fp); |
| 331 | fflush(fp); |
| 332 | fclose(fp); |
| 333 | if (verbose) |
| 334 | fprintf(stderr, "# wrote %s\n", cpathname); |
| 335 | #ifdef macintosh |
| 336 | setfiletype(cpathname, 'PYTH', 'PYC '); |
| 337 | #endif |
| 338 | } |
| 339 | |
| 340 | |
| 341 | /* Load a source module from a given file and return its module |
| 342 | object. If there's a matching byte-compiled file, use that |
| 343 | instead. */ |
| 344 | |
| 345 | static object * |
| 346 | load_source_module(name, pathname, fp) |
| 347 | char *name; |
| 348 | char *pathname; |
| 349 | FILE *fp; |
| 350 | { |
| 351 | long mtime; |
| 352 | FILE *fpc; |
| 353 | char buf[MAXPATHLEN+1]; |
| 354 | char *cpathname; |
| 355 | codeobject *co; |
| 356 | object *m; |
| 357 | |
| 358 | mtime = getmtime(pathname); |
| 359 | cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1); |
| 360 | if (cpathname != NULL && |
| 361 | (fpc = check_compiled_module(pathname, mtime, cpathname))) { |
| 362 | co = read_compiled_module(fpc); |
| 363 | fclose(fpc); |
| 364 | if (co == NULL) |
| 365 | return NULL; |
| 366 | if (verbose) |
| 367 | fprintf(stderr, "import %s # precompiled from %s\n", |
| 368 | name, cpathname); |
| 369 | } |
| 370 | else { |
| 371 | co = parse_source_module(pathname, fp); |
| 372 | if (co == NULL) |
| 373 | return NULL; |
| 374 | if (verbose) |
| 375 | fprintf(stderr, "import %s # from %s\n", |
| 376 | name, pathname); |
| 377 | write_compiled_module(co, cpathname, mtime); |
| 378 | } |
| 379 | m = exec_code_module(name, co); |
| 380 | DECREF(co); |
| 381 | |
| 382 | return m; |
| 383 | } |
| 384 | |
| 385 | |
| 386 | /* Search the path (default sys.path) for a module. Return the |
| 387 | corresponding filedescr struct, and (via return arguments) the |
| 388 | pathname and an open file. Return NULL if the module is not found. */ |
| 389 | |
| 390 | static struct filedescr * |
| 391 | find_module(name, path, buf, buflen, p_fp) |
| 392 | char *name; |
| 393 | object *path; |
| 394 | /* Output parameters: */ |
| 395 | char *buf; |
| 396 | int buflen; |
| 397 | FILE **p_fp; |
| 398 | { |
| 399 | int i, npath, len, namelen; |
| 400 | struct filedescr *fdp; |
| 401 | FILE *fp; |
| 402 | |
| 403 | if (path == NULL) |
| 404 | path = sysget("path"); |
| 405 | if (path == NULL || !is_listobject(path)) { |
| 406 | err_setstr(ImportError, |
| 407 | "module search path must be list of directory names"); |
| 408 | return NULL; |
| 409 | } |
| 410 | npath = getlistsize(path); |
| 411 | namelen = strlen(name); |
| 412 | for (i = 0; i < npath; i++) { |
| 413 | object *v = getlistitem(path, i); |
| 414 | if (!is_stringobject(v)) |
| 415 | continue; |
| 416 | len = getstringsize(v); |
| 417 | if (len + 2 + namelen + import_maxsuffixsize >= buflen) |
| 418 | continue; /* Too long */ |
| 419 | strcpy(buf, getstringvalue(v)); |
| 420 | if (strlen(buf) != len) |
| 421 | continue; /* v contains '\0' */ |
| 422 | if (len > 0 && buf[len-1] != SEP) |
| 423 | buf[len++] = SEP; |
| 424 | strcpy(buf+len, name); |
| 425 | len += namelen; |
| 426 | for (fdp = import_filetab; fdp->suffix != NULL; fdp++) { |
| 427 | strcpy(buf+len, fdp->suffix); |
| 428 | if (verbose > 1) |
| 429 | fprintf(stderr, "# trying %s\n", buf); |
| 430 | fp = fopen(buf, fdp->mode); |
| 431 | if (fp != NULL) |
| 432 | break; |
| 433 | } |
| 434 | if (fp != NULL) |
| 435 | break; |
| 436 | } |
| 437 | if (fp == NULL) { |
| 438 | char buf[256]; |
| 439 | sprintf(buf, "No module named %.200s", name); |
| 440 | err_setstr(ImportError, buf); |
| 441 | return NULL; |
| 442 | } |
| 443 | |
| 444 | *p_fp = fp; |
| 445 | return fdp; |
| 446 | } |
| 447 | |
| 448 | |
| 449 | /* Load an external module using the default search path and return |
| 450 | its module object */ |
| 451 | |
| 452 | static object * |
| 453 | load_module(name) |
| 454 | char *name; |
| 455 | { |
| 456 | char buf[MAXPATHLEN+1]; |
| 457 | struct filedescr *fdp; |
| 458 | FILE *fp = NULL; |
| 459 | object *m = NULL; |
| 460 | |
| 461 | fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp); |
| 462 | if (fdp == NULL) |
| 463 | return NULL; |
| 464 | |
| 465 | switch (fdp->type) { |
| 466 | |
| 467 | case PY_SOURCE: |
| 468 | m = load_source_module(name, buf, fp); |
| 469 | break; |
| 470 | |
| 471 | case PY_COMPILED: |
| 472 | m = load_compiled_module(name, buf, fp); |
| 473 | break; |
| 474 | |
| 475 | case C_EXTENSION: |
| 476 | m = load_dynamic_module(name, buf); |
| 477 | break; |
| 478 | |
| 479 | default: |
| 480 | err_setstr(SystemError, |
| 481 | "find_module returned unexpected result"); |
| 482 | |
| 483 | } |
| 484 | fclose(fp); |
| 485 | |
| 486 | return m; |
| 487 | } |
| 488 | |
| 489 | |
| 490 | /* Initialize a built-in module. |
| 491 | Return 1 for succes, 0 if the module is not found, and -1 with |
| 492 | an exception set if the initialization failed. */ |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 493 | |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 494 | static int |
| 495 | init_builtin(name) |
| 496 | char *name; |
| 497 | { |
| 498 | int i; |
| 499 | for (i = 0; inittab[i].name != NULL; i++) { |
| 500 | if (strcmp(name, inittab[i].name) == 0) { |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 501 | if (inittab[i].initfunc == NULL) { |
| 502 | err_setstr(ImportError, |
| 503 | "cannot re-init internal module"); |
| 504 | return -1; |
| 505 | } |
Guido van Rossum | 4cd8b5c | 1992-03-27 17:21:04 +0000 | [diff] [blame] | 506 | if (verbose) |
| 507 | fprintf(stderr, "import %s # builtin\n", |
| 508 | name); |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 509 | (*inittab[i].initfunc)(); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 510 | if (err_occurred()) |
| 511 | return -1; |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 512 | return 1; |
| 513 | } |
| 514 | } |
| 515 | return 0; |
| 516 | } |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 517 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 518 | |
| 519 | /* Initialize a frozen module. |
| 520 | Return 1 for succes, 0 if the module is not found, and -1 with |
| 521 | an exception set if the initialization failed. */ |
| 522 | |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 523 | extern struct frozen { |
| 524 | char *name; |
| 525 | char *code; |
| 526 | int size; |
| 527 | } frozen_modules[]; |
| 528 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 529 | static int |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 530 | init_frozen(name) |
| 531 | char *name; |
| 532 | { |
| 533 | struct frozen *p; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 534 | object *co; |
| 535 | object *m; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 536 | for (p = frozen_modules; ; p++) { |
| 537 | if (p->name == NULL) |
| 538 | return 0; |
| 539 | if (strcmp(p->name, name) == 0) |
| 540 | break; |
| 541 | } |
| 542 | if (verbose) |
| 543 | fprintf(stderr, "import %s # frozen\n", name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 544 | co = rds_object(p->code, p->size); |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 545 | if (co == NULL) |
| 546 | return -1; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 547 | if (!is_codeobject(co)) { |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 548 | DECREF(co); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 549 | err_setstr(SystemError, "frozen object is not a code object"); |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 550 | return -1; |
| 551 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 552 | m = exec_code_module(name, (codeobject *)co); |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 553 | DECREF(co); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 554 | return m == NULL ? -1 : 1; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 555 | } |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 556 | |
| 557 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 558 | /* Import a module, either built-in, frozen, or external, and return |
| 559 | its module object */ |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 560 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 561 | object * |
| 562 | import_module(name) |
| 563 | char *name; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 564 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 565 | object *m; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 566 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 567 | if ((m = dictlookup(import_modules, name)) == NULL) { |
| 568 | int i; |
| 569 | if ((i = init_builtin(name)) || (i = init_frozen(name))) { |
| 570 | if (i < 0) |
| 571 | return NULL; |
| 572 | if ((m = dictlookup(import_modules, name)) == NULL) { |
| 573 | if (err_occurred() == NULL) |
| 574 | err_setstr(SystemError, |
| 575 | "built-in module not initialized properly"); |
| 576 | } |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 577 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 578 | else |
| 579 | m = load_module(name); |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 580 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 581 | |
| 582 | return m; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 585 | |
| 586 | /* Re-import a module of any kind and return its module object, WITH |
| 587 | INCREMENTED REFERENCE COUNT */ |
| 588 | |
| 589 | object * |
| 590 | reload_module(m) |
| 591 | object *m; |
| 592 | { |
| 593 | char *name; |
| 594 | int i; |
| 595 | |
| 596 | if (m == NULL || !is_moduleobject(m)) { |
| 597 | err_setstr(TypeError, "reload() argument must be module"); |
| 598 | return NULL; |
| 599 | } |
| 600 | name = getmodulename(m); |
| 601 | if (name == NULL) |
| 602 | return NULL; |
| 603 | if (m != dictlookup(import_modules, name)) { |
| 604 | err_setstr(ImportError, "reload() module not in sys.modules"); |
| 605 | return NULL; |
| 606 | } |
| 607 | /* Check for built-in and frozen modules */ |
| 608 | if ((i = init_builtin(name)) || (i = init_frozen(name))) { |
| 609 | if (i < 0) |
| 610 | return NULL; |
| 611 | } |
| 612 | else |
| 613 | m = load_module(name); |
| 614 | XINCREF(m); |
| 615 | return m; |
| 616 | } |
| 617 | |
| 618 | |
| 619 | /* Module 'imp' provides Python access to the primitives used for |
| 620 | importing modules. |
| 621 | */ |
| 622 | |
| 623 | static object * |
| 624 | imp_get_magic(self, args) |
| 625 | object *self; |
| 626 | object *args; |
| 627 | { |
| 628 | char buf[4]; |
| 629 | |
| 630 | if (!newgetargs(args, "")) |
| 631 | return NULL; |
| 632 | buf[0] = (MAGIC >> 0) & 0xff; |
| 633 | buf[1] = (MAGIC >> 8) & 0xff; |
| 634 | buf[3] = (MAGIC >> 16) & 0xff; |
| 635 | buf[4] = (MAGIC >> 24) & 0xff; |
| 636 | |
| 637 | return newsizedstringobject(buf, 4); |
| 638 | } |
| 639 | |
| 640 | static object * |
| 641 | imp_get_suffixes(self, args) |
| 642 | object *self; |
| 643 | object *args; |
| 644 | { |
| 645 | object *list; |
| 646 | struct filedescr *fdp; |
| 647 | |
| 648 | if (!newgetargs(args, "")) |
| 649 | return NULL; |
| 650 | list = newlistobject(0); |
| 651 | if (list == NULL) |
| 652 | return NULL; |
| 653 | for (fdp = import_filetab; fdp->suffix != NULL; fdp++) { |
| 654 | object *item = mkvalue("ssi", |
| 655 | fdp->suffix, fdp->mode, fdp->type); |
| 656 | if (item == NULL) { |
| 657 | DECREF(list); |
| 658 | return NULL; |
| 659 | } |
| 660 | if (addlistitem(list, item) < 0) { |
| 661 | DECREF(list); |
| 662 | DECREF(item); |
| 663 | return NULL; |
| 664 | } |
| 665 | DECREF(item); |
| 666 | } |
| 667 | return list; |
| 668 | } |
| 669 | |
| 670 | static object * |
| 671 | imp_find_module(self, args) |
| 672 | object *self; |
| 673 | object *args; |
| 674 | { |
| 675 | extern int fclose PROTO((FILE *)); |
| 676 | char *name; |
| 677 | object *path = NULL; |
| 678 | object *fob, *ret; |
| 679 | struct filedescr *fdp; |
| 680 | char pathname[MAXPATHLEN+1]; |
| 681 | FILE *fp; |
| 682 | if (!newgetargs(args, "s|O!", &name, &Listtype, &path)) |
| 683 | return NULL; |
| 684 | fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp); |
| 685 | if (fdp == NULL) |
| 686 | return NULL; |
| 687 | fob = newopenfileobject(fp, pathname, fdp->mode, fclose); |
| 688 | if (fob == NULL) { |
| 689 | fclose(fp); |
| 690 | return NULL; |
| 691 | } |
| 692 | ret = mkvalue("Os(ssi)", |
| 693 | fob, pathname, fdp->suffix, fdp->mode, fdp->type); |
| 694 | DECREF(fob); |
| 695 | return ret; |
| 696 | } |
| 697 | |
| 698 | static object * |
| 699 | imp_init_builtin(self, args) |
| 700 | object *self; |
| 701 | object *args; |
| 702 | { |
| 703 | char *name; |
| 704 | int ret; |
| 705 | object *m; |
| 706 | if (!newgetargs(args, "s", &name)) |
| 707 | return NULL; |
| 708 | ret = init_builtin(name); |
| 709 | if (ret < 0) |
| 710 | return NULL; |
| 711 | if (ret == 0) { |
| 712 | INCREF(None); |
| 713 | return None; |
| 714 | } |
| 715 | m = add_module(name); |
| 716 | XINCREF(m); |
| 717 | return m; |
| 718 | } |
| 719 | |
| 720 | static object * |
| 721 | imp_init_frozen(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_frozen(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 | |
| 742 | static object * |
| 743 | imp_is_builtin(self, args) |
| 744 | object *self; |
| 745 | object *args; |
| 746 | { |
| 747 | int i; |
| 748 | char *name; |
| 749 | if (!newgetargs(args, "s", &name)) |
| 750 | return NULL; |
| 751 | for (i = 0; inittab[i].name != NULL; i++) { |
| 752 | if (strcmp(name, inittab[i].name) == 0) { |
| 753 | if (inittab[i].initfunc == NULL) |
| 754 | return newintobject(-1); |
| 755 | else |
| 756 | return newintobject(1); |
| 757 | } |
| 758 | } |
| 759 | return newintobject(0); |
| 760 | } |
| 761 | |
| 762 | static object * |
| 763 | imp_is_frozen(self, args) |
| 764 | object *self; |
| 765 | object *args; |
| 766 | { |
| 767 | struct frozen *p; |
| 768 | char *name; |
| 769 | if (!newgetargs(args, "s", &name)) |
| 770 | return NULL; |
| 771 | for (p = frozen_modules; ; p++) { |
| 772 | if (p->name == NULL) |
| 773 | break; |
| 774 | if (strcmp(p->name, name) == 0) |
| 775 | return newintobject(1); |
| 776 | } |
| 777 | return newintobject(0); |
| 778 | } |
| 779 | |
| 780 | static FILE * |
| 781 | get_file(pathname, fob, mode) |
| 782 | char *pathname; |
| 783 | object *fob; |
| 784 | char *mode; |
| 785 | { |
| 786 | FILE *fp; |
| 787 | if (fob == NULL) { |
| 788 | fp = fopen(pathname, mode); |
| 789 | if (fp == NULL) |
| 790 | err_errno(IOError); |
| 791 | } |
| 792 | else { |
| 793 | fp = getfilefile(fob); |
| 794 | if (fp == NULL) |
| 795 | err_setstr(ValueError, "bad/closed file object"); |
| 796 | } |
| 797 | return fp; |
| 798 | } |
| 799 | |
| 800 | static object * |
| 801 | imp_load_compiled(self, args) |
| 802 | object *self; |
| 803 | object *args; |
| 804 | { |
| 805 | char *name; |
| 806 | char *pathname; |
| 807 | object *fob = NULL; |
| 808 | object *m; |
| 809 | FILE *fp; |
| 810 | if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob)) |
| 811 | return NULL; |
| 812 | fp = get_file(pathname, fob, "rb"); |
| 813 | if (fp == NULL) |
| 814 | return NULL; |
| 815 | m = load_compiled_module(name, pathname, fp); |
| 816 | if (fob == NULL) |
| 817 | fclose(fp); |
| 818 | return m; |
| 819 | } |
| 820 | |
| 821 | static object * |
| 822 | imp_load_dynamic(self, args) |
| 823 | object *self; |
| 824 | object *args; |
| 825 | { |
| 826 | char *name; |
| 827 | char *pathname; |
| 828 | object *dummy; |
| 829 | if (!newgetargs(args, "ss|O", &name, &pathname, &dummy)) |
| 830 | return NULL; |
| 831 | return load_dynamic_module(name, pathname); |
| 832 | } |
| 833 | |
| 834 | static object * |
| 835 | imp_load_source(self, args) |
| 836 | object *self; |
| 837 | object *args; |
| 838 | { |
| 839 | char *name; |
| 840 | char *pathname; |
| 841 | object *fob = NULL; |
| 842 | object *m; |
| 843 | FILE *fp; |
| 844 | if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob)) |
| 845 | return NULL; |
| 846 | fp = get_file(pathname, fob, "r"); |
| 847 | if (fp == NULL) |
| 848 | return NULL; |
| 849 | m = load_source_module(name, pathname, fp); |
| 850 | if (fob == NULL) |
| 851 | fclose(fp); |
| 852 | return m; |
| 853 | } |
| 854 | |
| 855 | static object * |
| 856 | imp_new_module(self, args) |
| 857 | object *self; |
| 858 | object *args; |
| 859 | { |
| 860 | char *name; |
| 861 | if (!newgetargs(args, "s", &name)) |
| 862 | return NULL; |
| 863 | return newmoduleobject(name); |
| 864 | } |
| 865 | |
| 866 | static struct methodlist imp_methods[] = { |
| 867 | {"get_magic", imp_get_magic, 1}, |
| 868 | {"get_suffixes", imp_get_suffixes, 1}, |
| 869 | {"find_module", imp_find_module, 1}, |
| 870 | {"init_builtin", imp_init_builtin, 1}, |
| 871 | {"init_frozen", imp_init_frozen, 1}, |
| 872 | {"is_builtin", imp_is_builtin, 1}, |
| 873 | {"is_frozen", imp_is_frozen, 1}, |
| 874 | {"load_compiled", imp_load_compiled, 1}, |
| 875 | {"load_dynamic", imp_load_dynamic, 1}, |
| 876 | {"load_source", imp_load_source, 1}, |
| 877 | {"new_module", imp_new_module, 1}, |
| 878 | {NULL, NULL} /* sentinel */ |
| 879 | }; |
| 880 | |
| 881 | void |
| 882 | initimp() |
| 883 | { |
| 884 | object *m, *d, *v; |
| 885 | |
| 886 | m = initmodule("imp", imp_methods); |
| 887 | d = getmoduledict(m); |
| 888 | |
| 889 | v = newintobject(SEARCH_ERROR); |
| 890 | dictinsert(d, "SEARCH_ERROR", v); |
| 891 | XDECREF(v); |
| 892 | |
| 893 | v = newintobject(PY_SOURCE); |
| 894 | dictinsert(d, "PY_SOURCE", v); |
| 895 | XDECREF(v); |
| 896 | |
| 897 | v = newintobject(PY_COMPILED); |
| 898 | dictinsert(d, "PY_COMPILED", v); |
| 899 | XDECREF(v); |
| 900 | |
| 901 | v = newintobject(C_EXTENSION); |
| 902 | dictinsert(d, "C_EXTENSION", v); |
| 903 | XDECREF(v); |
| 904 | |
| 905 | if (err_occurred()) |
| 906 | fatal("imp module initialization failed"); |
| 907 | } |