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