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