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