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