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