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