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