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 | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 439 | extern FILE *PyWin_FindRegisteredModule(); |
Guido van Rossum | a5a3db7 | 1996-04-09 02:39:59 +0000 | [diff] [blame] | 440 | if ((fp=PyWin_FindRegisteredModule(name, &fdp, buf, buflen))!=NULL) { |
| 441 | *p_fp = fp; |
| 442 | return fdp; |
| 443 | } |
| 444 | #endif |
| 445 | |
| 446 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 447 | if (path == NULL) |
| 448 | path = sysget("path"); |
| 449 | if (path == NULL || !is_listobject(path)) { |
| 450 | err_setstr(ImportError, |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 451 | "sys.path must be a list of directory names"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 452 | return NULL; |
| 453 | } |
| 454 | npath = getlistsize(path); |
| 455 | namelen = strlen(name); |
| 456 | for (i = 0; i < npath; i++) { |
| 457 | object *v = getlistitem(path, i); |
| 458 | if (!is_stringobject(v)) |
| 459 | continue; |
| 460 | len = getstringsize(v); |
| 461 | if (len + 2 + namelen + import_maxsuffixsize >= buflen) |
| 462 | continue; /* Too long */ |
| 463 | strcpy(buf, getstringvalue(v)); |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 464 | if ((int)strlen(buf) != len) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 465 | continue; /* v contains '\0' */ |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 466 | #ifdef macintosh |
| 467 | if ( PyMac_FindResourceModule(name, buf) ) { |
| 468 | static struct filedescr resfiledescr = { "", "", PY_RESOURCE}; |
| 469 | |
| 470 | return &resfiledescr; |
| 471 | } |
| 472 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 473 | if (len > 0 && buf[len-1] != SEP) |
| 474 | buf[len++] = SEP; |
Guido van Rossum | 40f470f | 1996-05-23 22:51:04 +0000 | [diff] [blame] | 475 | #ifdef IMPORT_8x3_NAMES |
| 476 | /* see if we are searching in directory dos_8x3 */ |
| 477 | if (len > 7 && !strncmp(buf + len - 8, "dos_8x3", 7)){ |
| 478 | int j; |
| 479 | char ch; /* limit name to eight lower-case characters */ |
| 480 | for (j = 0; (ch = name[j]) && j < 8; j++) |
| 481 | if (isupper(ch)) |
| 482 | buf[len++] = tolower(ch); |
| 483 | else |
| 484 | buf[len++] = ch; |
| 485 | } |
Guido van Rossum | 0e41c8c | 1996-06-20 14:18:34 +0000 | [diff] [blame] | 486 | else /* Not in dos_8x3, use the full name */ |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 487 | #endif |
Guido van Rossum | 0e41c8c | 1996-06-20 14:18:34 +0000 | [diff] [blame] | 488 | { |
Guido van Rossum | 40f470f | 1996-05-23 22:51:04 +0000 | [diff] [blame] | 489 | strcpy(buf+len, name); |
| 490 | len += namelen; |
| 491 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 492 | for (fdp = import_filetab; fdp->suffix != NULL; fdp++) { |
| 493 | strcpy(buf+len, fdp->suffix); |
| 494 | if (verbose > 1) |
| 495 | fprintf(stderr, "# trying %s\n", buf); |
| 496 | fp = fopen(buf, fdp->mode); |
| 497 | if (fp != NULL) |
| 498 | break; |
| 499 | } |
| 500 | if (fp != NULL) |
| 501 | break; |
| 502 | } |
| 503 | if (fp == NULL) { |
| 504 | char buf[256]; |
| 505 | sprintf(buf, "No module named %.200s", name); |
| 506 | err_setstr(ImportError, buf); |
| 507 | return NULL; |
| 508 | } |
| 509 | |
| 510 | *p_fp = fp; |
| 511 | return fdp; |
| 512 | } |
| 513 | |
| 514 | |
| 515 | /* Load an external module using the default search path and return |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 516 | its module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 517 | |
| 518 | static object * |
| 519 | load_module(name) |
| 520 | char *name; |
| 521 | { |
| 522 | char buf[MAXPATHLEN+1]; |
| 523 | struct filedescr *fdp; |
| 524 | FILE *fp = NULL; |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 525 | object *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 526 | |
| 527 | fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp); |
| 528 | if (fdp == NULL) |
| 529 | return NULL; |
| 530 | |
| 531 | switch (fdp->type) { |
| 532 | |
| 533 | case PY_SOURCE: |
| 534 | m = load_source_module(name, buf, fp); |
| 535 | break; |
| 536 | |
| 537 | case PY_COMPILED: |
| 538 | m = load_compiled_module(name, buf, fp); |
| 539 | break; |
| 540 | |
| 541 | case C_EXTENSION: |
Sjoerd Mullender | fbe6d33 | 1995-06-12 15:51:34 +0000 | [diff] [blame] | 542 | m = load_dynamic_module(name, buf, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 543 | break; |
| 544 | |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 545 | #ifdef macintosh |
| 546 | case PY_RESOURCE: |
| 547 | m = PyMac_LoadResourceModule(name, buf); |
| 548 | break; |
| 549 | #endif |
| 550 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 551 | default: |
| 552 | err_setstr(SystemError, |
| 553 | "find_module returned unexpected result"); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 554 | m = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 555 | |
| 556 | } |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 557 | if ( fp ) |
| 558 | fclose(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 559 | |
| 560 | return m; |
| 561 | } |
| 562 | |
| 563 | |
| 564 | /* Initialize a built-in module. |
| 565 | Return 1 for succes, 0 if the module is not found, and -1 with |
| 566 | an exception set if the initialization failed. */ |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 567 | |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 568 | static int |
| 569 | init_builtin(name) |
| 570 | char *name; |
| 571 | { |
| 572 | int i; |
| 573 | for (i = 0; inittab[i].name != NULL; i++) { |
| 574 | if (strcmp(name, inittab[i].name) == 0) { |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 575 | if (inittab[i].initfunc == NULL) { |
| 576 | err_setstr(ImportError, |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 577 | "Cannot re-init internal module"); |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 578 | return -1; |
| 579 | } |
Guido van Rossum | 4cd8b5c | 1992-03-27 17:21:04 +0000 | [diff] [blame] | 580 | if (verbose) |
| 581 | fprintf(stderr, "import %s # builtin\n", |
| 582 | name); |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 583 | (*inittab[i].initfunc)(); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 584 | if (err_occurred()) |
| 585 | return -1; |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 586 | return 1; |
| 587 | } |
| 588 | } |
| 589 | return 0; |
| 590 | } |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 591 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 592 | |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 593 | /* Frozen modules */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 594 | |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 595 | static struct _frozen * |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 596 | find_frozen(name) |
| 597 | char *name; |
| 598 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 599 | struct _frozen *p; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 600 | |
| 601 | for (p = frozen_modules; ; p++) { |
| 602 | if (p->name == NULL) |
| 603 | return NULL; |
| 604 | if (strcmp(p->name, name) == 0) |
| 605 | break; |
| 606 | } |
| 607 | return p; |
| 608 | } |
| 609 | |
| 610 | static object * |
| 611 | get_frozen_object(name) |
| 612 | char *name; |
| 613 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 614 | struct _frozen *p = find_frozen(name); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 615 | |
| 616 | if (p == NULL) { |
| 617 | err_setstr(ImportError, "No such frozen object"); |
| 618 | return NULL; |
| 619 | } |
Guido van Rossum | 1741d60 | 1996-08-08 18:52:59 +0000 | [diff] [blame] | 620 | return rds_object((char *)p->code, p->size); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /* Initialize a frozen module. |
| 624 | Return 1 for succes, 0 if the module is not found, and -1 with |
| 625 | an exception set if the initialization failed. |
| 626 | This function is also used from frozenmain.c */ |
Guido van Rossum | 0b34490 | 1995-02-07 15:35:27 +0000 | [diff] [blame] | 627 | |
| 628 | int |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 629 | init_frozen(name) |
| 630 | char *name; |
| 631 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 632 | struct _frozen *p = find_frozen(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 633 | object *co; |
| 634 | object *m; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 635 | |
| 636 | if (p == NULL) |
| 637 | return 0; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 638 | if (verbose) |
| 639 | fprintf(stderr, "import %s # frozen\n", name); |
Guido van Rossum | 1741d60 | 1996-08-08 18:52:59 +0000 | [diff] [blame] | 640 | co = rds_object((char *)p->code, p->size); |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 641 | if (co == NULL) |
| 642 | return -1; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 643 | if (!is_codeobject(co)) { |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 644 | DECREF(co); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 645 | err_setstr(TypeError, "frozen object is not a code object"); |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 646 | return -1; |
| 647 | } |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 648 | m = exec_code_module(name, co); |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 649 | DECREF(co); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 650 | if (m == NULL) |
| 651 | return -1; |
| 652 | DECREF(m); |
| 653 | return 1; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 654 | } |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 655 | |
| 656 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 657 | /* Import a module, either built-in, frozen, or external, and return |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 658 | its module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 659 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 660 | object * |
| 661 | import_module(name) |
| 662 | char *name; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 663 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 664 | object *m; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 665 | |
Guido van Rossum | 0de81bf | 1995-01-26 00:41:28 +0000 | [diff] [blame] | 666 | if (import_modules == NULL) { |
| 667 | err_setstr(SystemError, "sys.modules has been deleted"); |
| 668 | return NULL; |
| 669 | } |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 670 | if ((m = dictlookup(import_modules, name)) != NULL) { |
| 671 | INCREF(m); |
| 672 | } |
| 673 | else { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 674 | int i; |
| 675 | if ((i = init_builtin(name)) || (i = init_frozen(name))) { |
| 676 | if (i < 0) |
| 677 | return NULL; |
| 678 | if ((m = dictlookup(import_modules, name)) == NULL) { |
| 679 | if (err_occurred() == NULL) |
| 680 | err_setstr(SystemError, |
| 681 | "built-in module not initialized properly"); |
| 682 | } |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 683 | else |
| 684 | INCREF(m); |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 685 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 686 | else |
| 687 | m = load_module(name); |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 688 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 689 | |
| 690 | return m; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 693 | |
| 694 | /* Re-import a module of any kind and return its module object, WITH |
| 695 | INCREMENTED REFERENCE COUNT */ |
| 696 | |
| 697 | object * |
| 698 | reload_module(m) |
| 699 | object *m; |
| 700 | { |
| 701 | char *name; |
| 702 | int i; |
| 703 | |
| 704 | if (m == NULL || !is_moduleobject(m)) { |
| 705 | err_setstr(TypeError, "reload() argument must be module"); |
| 706 | return NULL; |
| 707 | } |
| 708 | name = getmodulename(m); |
| 709 | if (name == NULL) |
| 710 | return NULL; |
Guido van Rossum | 0de81bf | 1995-01-26 00:41:28 +0000 | [diff] [blame] | 711 | if (import_modules == NULL) { |
| 712 | err_setstr(SystemError, "sys.modules has been deleted"); |
| 713 | return NULL; |
| 714 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 715 | if (m != dictlookup(import_modules, name)) { |
| 716 | err_setstr(ImportError, "reload() module not in sys.modules"); |
| 717 | return NULL; |
| 718 | } |
| 719 | /* Check for built-in and frozen modules */ |
| 720 | if ((i = init_builtin(name)) || (i = init_frozen(name))) { |
| 721 | if (i < 0) |
| 722 | return NULL; |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 723 | INCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 724 | } |
| 725 | else |
| 726 | m = load_module(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 727 | return m; |
| 728 | } |
| 729 | |
| 730 | |
| 731 | /* Module 'imp' provides Python access to the primitives used for |
| 732 | importing modules. |
| 733 | */ |
| 734 | |
| 735 | static object * |
| 736 | imp_get_magic(self, args) |
| 737 | object *self; |
| 738 | object *args; |
| 739 | { |
| 740 | char buf[4]; |
| 741 | |
| 742 | if (!newgetargs(args, "")) |
| 743 | return NULL; |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 744 | buf[0] = (char) ((MAGIC >> 0) & 0xff); |
| 745 | buf[1] = (char) ((MAGIC >> 8) & 0xff); |
| 746 | buf[2] = (char) ((MAGIC >> 16) & 0xff); |
| 747 | buf[3] = (char) ((MAGIC >> 24) & 0xff); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 748 | |
| 749 | return newsizedstringobject(buf, 4); |
| 750 | } |
| 751 | |
| 752 | static object * |
| 753 | imp_get_suffixes(self, args) |
| 754 | object *self; |
| 755 | object *args; |
| 756 | { |
| 757 | object *list; |
| 758 | struct filedescr *fdp; |
| 759 | |
| 760 | if (!newgetargs(args, "")) |
| 761 | return NULL; |
| 762 | list = newlistobject(0); |
| 763 | if (list == NULL) |
| 764 | return NULL; |
| 765 | for (fdp = import_filetab; fdp->suffix != NULL; fdp++) { |
| 766 | object *item = mkvalue("ssi", |
| 767 | fdp->suffix, fdp->mode, fdp->type); |
| 768 | if (item == NULL) { |
| 769 | DECREF(list); |
| 770 | return NULL; |
| 771 | } |
| 772 | if (addlistitem(list, item) < 0) { |
| 773 | DECREF(list); |
| 774 | DECREF(item); |
| 775 | return NULL; |
| 776 | } |
| 777 | DECREF(item); |
| 778 | } |
| 779 | return list; |
| 780 | } |
| 781 | |
| 782 | static object * |
| 783 | imp_find_module(self, args) |
| 784 | object *self; |
| 785 | object *args; |
| 786 | { |
| 787 | extern int fclose PROTO((FILE *)); |
| 788 | char *name; |
| 789 | object *path = NULL; |
| 790 | object *fob, *ret; |
| 791 | struct filedescr *fdp; |
| 792 | char pathname[MAXPATHLEN+1]; |
| 793 | FILE *fp; |
| 794 | if (!newgetargs(args, "s|O!", &name, &Listtype, &path)) |
| 795 | return NULL; |
| 796 | fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp); |
| 797 | if (fdp == NULL) |
| 798 | return NULL; |
| 799 | fob = newopenfileobject(fp, pathname, fdp->mode, fclose); |
| 800 | if (fob == NULL) { |
| 801 | fclose(fp); |
| 802 | return NULL; |
| 803 | } |
| 804 | ret = mkvalue("Os(ssi)", |
| 805 | fob, pathname, fdp->suffix, fdp->mode, fdp->type); |
| 806 | DECREF(fob); |
| 807 | return ret; |
| 808 | } |
| 809 | |
| 810 | static object * |
| 811 | imp_init_builtin(self, args) |
| 812 | object *self; |
| 813 | object *args; |
| 814 | { |
| 815 | char *name; |
| 816 | int ret; |
| 817 | object *m; |
| 818 | if (!newgetargs(args, "s", &name)) |
| 819 | return NULL; |
| 820 | ret = init_builtin(name); |
| 821 | if (ret < 0) |
| 822 | return NULL; |
| 823 | if (ret == 0) { |
| 824 | INCREF(None); |
| 825 | return None; |
| 826 | } |
| 827 | m = add_module(name); |
| 828 | XINCREF(m); |
| 829 | return m; |
| 830 | } |
| 831 | |
| 832 | static object * |
| 833 | imp_init_frozen(self, args) |
| 834 | object *self; |
| 835 | object *args; |
| 836 | { |
| 837 | char *name; |
| 838 | int ret; |
| 839 | object *m; |
| 840 | if (!newgetargs(args, "s", &name)) |
| 841 | return NULL; |
| 842 | ret = init_frozen(name); |
| 843 | if (ret < 0) |
| 844 | return NULL; |
| 845 | if (ret == 0) { |
| 846 | INCREF(None); |
| 847 | return None; |
| 848 | } |
| 849 | m = add_module(name); |
| 850 | XINCREF(m); |
| 851 | return m; |
| 852 | } |
| 853 | |
| 854 | static object * |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 855 | imp_get_frozen_object(self, args) |
| 856 | object *self; |
| 857 | object *args; |
| 858 | { |
| 859 | char *name; |
Jack Jansen | 95ffa23 | 1995-10-03 14:38:41 +0000 | [diff] [blame] | 860 | |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 861 | if (!newgetargs(args, "s", &name)) |
| 862 | return NULL; |
| 863 | return get_frozen_object(name); |
| 864 | } |
| 865 | |
| 866 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 867 | imp_is_builtin(self, args) |
| 868 | object *self; |
| 869 | object *args; |
| 870 | { |
| 871 | int i; |
| 872 | char *name; |
| 873 | if (!newgetargs(args, "s", &name)) |
| 874 | return NULL; |
| 875 | for (i = 0; inittab[i].name != NULL; i++) { |
| 876 | if (strcmp(name, inittab[i].name) == 0) { |
| 877 | if (inittab[i].initfunc == NULL) |
| 878 | return newintobject(-1); |
| 879 | else |
| 880 | return newintobject(1); |
| 881 | } |
| 882 | } |
| 883 | return newintobject(0); |
| 884 | } |
| 885 | |
| 886 | static object * |
| 887 | imp_is_frozen(self, args) |
| 888 | object *self; |
| 889 | object *args; |
| 890 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 891 | struct _frozen *p; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 892 | char *name; |
| 893 | if (!newgetargs(args, "s", &name)) |
| 894 | return NULL; |
| 895 | for (p = frozen_modules; ; p++) { |
| 896 | if (p->name == NULL) |
| 897 | break; |
| 898 | if (strcmp(p->name, name) == 0) |
| 899 | return newintobject(1); |
| 900 | } |
| 901 | return newintobject(0); |
| 902 | } |
| 903 | |
| 904 | static FILE * |
| 905 | get_file(pathname, fob, mode) |
| 906 | char *pathname; |
| 907 | object *fob; |
| 908 | char *mode; |
| 909 | { |
| 910 | FILE *fp; |
| 911 | if (fob == NULL) { |
| 912 | fp = fopen(pathname, mode); |
| 913 | if (fp == NULL) |
| 914 | err_errno(IOError); |
| 915 | } |
| 916 | else { |
| 917 | fp = getfilefile(fob); |
| 918 | if (fp == NULL) |
| 919 | err_setstr(ValueError, "bad/closed file object"); |
| 920 | } |
| 921 | return fp; |
| 922 | } |
| 923 | |
| 924 | static object * |
| 925 | imp_load_compiled(self, args) |
| 926 | object *self; |
| 927 | object *args; |
| 928 | { |
| 929 | char *name; |
| 930 | char *pathname; |
| 931 | object *fob = NULL; |
| 932 | object *m; |
| 933 | FILE *fp; |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 934 | if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 935 | return NULL; |
| 936 | fp = get_file(pathname, fob, "rb"); |
| 937 | if (fp == NULL) |
| 938 | return NULL; |
| 939 | m = load_compiled_module(name, pathname, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 940 | return m; |
| 941 | } |
| 942 | |
| 943 | static object * |
| 944 | imp_load_dynamic(self, args) |
| 945 | object *self; |
| 946 | object *args; |
| 947 | { |
| 948 | char *name; |
| 949 | char *pathname; |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 950 | object *fob = NULL; |
| 951 | object *m; |
| 952 | FILE *fp = NULL; |
| 953 | if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 954 | return NULL; |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 955 | if (fob) |
| 956 | fp = get_file(pathname, fob, "r"); |
| 957 | m = load_dynamic_module(name, pathname, fp); |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 958 | return m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | static object * |
| 962 | imp_load_source(self, args) |
| 963 | object *self; |
| 964 | object *args; |
| 965 | { |
| 966 | char *name; |
| 967 | char *pathname; |
| 968 | object *fob = NULL; |
| 969 | object *m; |
| 970 | FILE *fp; |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 971 | if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 972 | return NULL; |
| 973 | fp = get_file(pathname, fob, "r"); |
| 974 | if (fp == NULL) |
| 975 | return NULL; |
| 976 | m = load_source_module(name, pathname, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 977 | return m; |
| 978 | } |
| 979 | |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 980 | #ifdef macintosh |
| 981 | static object * |
| 982 | imp_load_resource(self, args) |
| 983 | object *self; |
| 984 | object *args; |
| 985 | { |
| 986 | char *name; |
| 987 | char *pathname; |
| 988 | object *m; |
| 989 | |
| 990 | if (!newgetargs(args, "ss", &name, &pathname)) |
| 991 | return NULL; |
| 992 | m = PyMac_LoadResourceModule(name, pathname); |
| 993 | return m; |
| 994 | } |
| 995 | #endif /* macintosh */ |
| 996 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 997 | static object * |
| 998 | imp_new_module(self, args) |
| 999 | object *self; |
| 1000 | object *args; |
| 1001 | { |
| 1002 | char *name; |
| 1003 | if (!newgetargs(args, "s", &name)) |
| 1004 | return NULL; |
| 1005 | return newmoduleobject(name); |
| 1006 | } |
| 1007 | |
| 1008 | static struct methodlist imp_methods[] = { |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1009 | {"get_frozen_object", imp_get_frozen_object, 1}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1010 | {"get_magic", imp_get_magic, 1}, |
| 1011 | {"get_suffixes", imp_get_suffixes, 1}, |
| 1012 | {"find_module", imp_find_module, 1}, |
| 1013 | {"init_builtin", imp_init_builtin, 1}, |
| 1014 | {"init_frozen", imp_init_frozen, 1}, |
| 1015 | {"is_builtin", imp_is_builtin, 1}, |
| 1016 | {"is_frozen", imp_is_frozen, 1}, |
| 1017 | {"load_compiled", imp_load_compiled, 1}, |
| 1018 | {"load_dynamic", imp_load_dynamic, 1}, |
| 1019 | {"load_source", imp_load_source, 1}, |
| 1020 | {"new_module", imp_new_module, 1}, |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 1021 | #ifdef macintosh |
| 1022 | {"load_resource", imp_load_resource, 1}, |
| 1023 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1024 | {NULL, NULL} /* sentinel */ |
| 1025 | }; |
| 1026 | |
| 1027 | void |
| 1028 | initimp() |
| 1029 | { |
| 1030 | object *m, *d, *v; |
| 1031 | |
| 1032 | m = initmodule("imp", imp_methods); |
| 1033 | d = getmoduledict(m); |
| 1034 | |
| 1035 | v = newintobject(SEARCH_ERROR); |
| 1036 | dictinsert(d, "SEARCH_ERROR", v); |
| 1037 | XDECREF(v); |
| 1038 | |
| 1039 | v = newintobject(PY_SOURCE); |
| 1040 | dictinsert(d, "PY_SOURCE", v); |
| 1041 | XDECREF(v); |
| 1042 | |
| 1043 | v = newintobject(PY_COMPILED); |
| 1044 | dictinsert(d, "PY_COMPILED", v); |
| 1045 | XDECREF(v); |
| 1046 | |
| 1047 | v = newintobject(C_EXTENSION); |
| 1048 | dictinsert(d, "C_EXTENSION", v); |
| 1049 | XDECREF(v); |
| 1050 | |
Jack Jansen | ae12e19 | 1995-06-18 20:06:44 +0000 | [diff] [blame] | 1051 | #ifdef macintosh |
| 1052 | v = newintobject(PY_RESOURCE); |
| 1053 | dictinsert(d, "PY_RESOURCE", v); |
| 1054 | XDECREF(v); |
| 1055 | #endif |
| 1056 | |
| 1057 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1058 | if (err_occurred()) |
| 1059 | fatal("imp module initialization failed"); |
| 1060 | } |