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 | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 36 | #include "node.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 37 | #include "token.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 38 | #include "errcode.h" |
Guido van Rossum | c405b7b | 1991-06-04 19:39:42 +0000 | [diff] [blame] | 39 | #include "marshal.h" |
| 40 | #include "compile.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 41 | #include "eval.h" |
Guido van Rossum | d8bac6d | 1992-02-26 15:19:13 +0000 | [diff] [blame] | 42 | #include "osdefs.h" |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 43 | #include "importdl.h" |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 44 | #ifdef macintosh |
| 45 | #include "macglue.h" |
| 46 | #endif |
Guido van Rossum | c405b7b | 1991-06-04 19:39:42 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 48 | #ifdef HAVE_UNISTD_H |
| 49 | #include <unistd.h> |
| 50 | #endif |
| 51 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 52 | /* We expect that stat exists on most systems. |
| 53 | It's confirmed on Unix, Mac and Windows. |
| 54 | If you don't have it, add #define DONT_HAVE_STAT to your config.h. */ |
| 55 | #ifndef DONT_HAVE_STAT |
| 56 | #define HAVE_STAT |
| 57 | |
| 58 | #include <sys/types.h> |
| 59 | #include <sys/stat.h> |
| 60 | |
Guido van Rossum | 595d7ba | 1997-12-05 21:45:29 +0000 | [diff] [blame] | 61 | #if defined(PYCC_VACPP) |
| 62 | /* VisualAge C/C++ Failed to Define MountType Field in sys/stat.h */ |
| 63 | #define S_IFMT (S_IFDIR|S_IFCHR|S_IFREG) |
| 64 | #endif |
| 65 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 66 | #ifndef S_ISDIR |
| 67 | #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) |
| 68 | #endif |
| 69 | |
| 70 | #endif |
| 71 | |
| 72 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 73 | extern long PyOS_GetLastModificationTime(); /* In getmtime.c */ |
Guido van Rossum | 21d335e | 1993-10-15 13:01:11 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 6c84969 | 1994-09-26 15:47:17 +0000 | [diff] [blame] | 75 | /* Magic word to reject .pyc files generated by other Python versions */ |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 76 | /* Change for each incompatible change */ |
| 77 | /* The value of CR and LF is incorporated so if you ever read or write |
| 78 | a .pyc file in text mode the magic number will be wrong; also, the |
| 79 | Apple MPW compiler swaps their values, botching string constants */ |
| 80 | /* XXX Perhaps the magic number should be frozen and a version field |
| 81 | added to the .pyc file header? */ |
Guido van Rossum | dd5db43 | 1997-01-17 21:06:11 +0000 | [diff] [blame] | 82 | /* 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] | 83 | #define MAGIC (20121 | ((long)'\r'<<16) | ((long)'\n'<<24)) |
Guido van Rossum | 3ddee71 | 1991-12-16 13:06:34 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 85 | /* See _PyImport_FixupExtension() below */ |
| 86 | static PyObject *extensions = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | 771c6c8 | 1997-10-31 18:37:24 +0000 | [diff] [blame] | 88 | /* This table is defined in config.c: */ |
| 89 | extern struct _inittab _PyImport_Inittab[]; |
| 90 | |
| 91 | struct _inittab *PyImport_Inittab = _PyImport_Inittab; |
Guido van Rossum | 66f1fa8 | 1991-04-03 19:03:52 +0000 | [diff] [blame] | 92 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 93 | /* Initialize things */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 94 | |
| 95 | void |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 96 | _PyImport_Init() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 97 | { |
Guido van Rossum | 0824f63 | 1997-03-11 18:37:35 +0000 | [diff] [blame] | 98 | if (Py_OptimizeFlag) { |
| 99 | /* Replace ".pyc" with ".pyo" in import_filetab */ |
| 100 | struct filedescr *p; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 101 | for (p = _PyImport_Filetab; p->suffix != NULL; p++) { |
Guido van Rossum | 0824f63 | 1997-03-11 18:37:35 +0000 | [diff] [blame] | 102 | if (strcmp(p->suffix, ".pyc") == 0) |
| 103 | p->suffix = ".pyo"; |
| 104 | } |
| 105 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 108 | void |
| 109 | _PyImport_Fini() |
| 110 | { |
| 111 | Py_XDECREF(extensions); |
| 112 | extensions = NULL; |
| 113 | } |
| 114 | |
| 115 | |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 116 | /* Locking primitives to prevent parallel imports of the same module |
| 117 | in different threads to return with a partially loaded module. |
| 118 | These calls are serialized by the global interpreter lock. */ |
| 119 | |
| 120 | #ifdef WITH_THREAD |
| 121 | |
| 122 | #include "thread.h" |
| 123 | |
| 124 | static type_lock import_lock = 0; |
| 125 | static long import_lock_thread = -1; |
| 126 | static int import_lock_level = 0; |
| 127 | |
| 128 | static void |
| 129 | lock_import() |
| 130 | { |
| 131 | long me = get_thread_ident(); |
| 132 | if (me == -1) |
| 133 | return; /* Too bad */ |
| 134 | if (import_lock == NULL) |
| 135 | import_lock = allocate_lock(); |
| 136 | if (import_lock_thread == me) { |
| 137 | import_lock_level++; |
| 138 | return; |
| 139 | } |
| 140 | if (import_lock_thread != -1 || !acquire_lock(import_lock, 0)) { |
| 141 | PyThreadState *tstate = PyEval_SaveThread(); |
| 142 | acquire_lock(import_lock, 1); |
| 143 | PyEval_RestoreThread(tstate); |
| 144 | } |
| 145 | import_lock_thread = me; |
| 146 | import_lock_level = 1; |
| 147 | } |
| 148 | |
| 149 | static void |
| 150 | unlock_import() |
| 151 | { |
| 152 | long me = get_thread_ident(); |
| 153 | if (me == -1) |
| 154 | return; /* Too bad */ |
| 155 | if (import_lock_thread != me) |
| 156 | Py_FatalError("unlock_import: not holding the import lock"); |
| 157 | import_lock_level--; |
| 158 | if (import_lock_level == 0) { |
| 159 | import_lock_thread = -1; |
| 160 | release_lock(import_lock); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | #else |
| 165 | |
| 166 | #define lock_import() |
| 167 | #define unlock_import() |
| 168 | |
| 169 | #endif |
| 170 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 171 | /* Helper for sys */ |
| 172 | |
| 173 | PyObject * |
| 174 | PyImport_GetModuleDict() |
| 175 | { |
| 176 | PyInterpreterState *interp = PyThreadState_Get()->interp; |
| 177 | if (interp->modules == NULL) |
| 178 | Py_FatalError("PyImport_GetModuleDict: no module dictionary!"); |
| 179 | return interp->modules; |
| 180 | } |
| 181 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 182 | |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 183 | /* List of names to clear in sys */ |
| 184 | static char* sys_deletes[] = { |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 185 | "path", "argv", "ps1", "ps2", "exitfunc", |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 186 | "exc_type", "exc_value", "exc_traceback", |
| 187 | "last_type", "last_value", "last_traceback", |
| 188 | NULL |
| 189 | }; |
| 190 | |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 191 | static char* sys_files[] = { |
| 192 | "stdin", "__stdin__", |
| 193 | "stdout", "__stdout__", |
| 194 | "stderr", "__stderr__", |
| 195 | NULL |
| 196 | }; |
| 197 | |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 199 | /* Un-initialize things, as good as we can */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 200 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 201 | void |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 202 | PyImport_Cleanup() |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 203 | { |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 204 | int pos, ndone; |
| 205 | char *name; |
| 206 | PyObject *key, *value, *dict; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 207 | PyInterpreterState *interp = PyThreadState_Get()->interp; |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 208 | PyObject *modules = interp->modules; |
| 209 | |
| 210 | if (modules == NULL) |
| 211 | return; /* Already done */ |
| 212 | |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 213 | /* Delete some special variables first. These are common |
| 214 | places where user values hide and people complain when their |
| 215 | destructors fail. Since the modules containing them are |
| 216 | deleted *last* of all, they would come too late in the normal |
| 217 | destruction order. Sigh. */ |
| 218 | |
| 219 | value = PyDict_GetItemString(modules, "__builtin__"); |
| 220 | if (value != NULL && PyModule_Check(value)) { |
| 221 | dict = PyModule_GetDict(value); |
| 222 | if (Py_VerboseFlag) |
| 223 | fprintf(stderr, "# clear __builtin__._\n"); |
| 224 | PyDict_SetItemString(dict, "_", Py_None); |
| 225 | } |
| 226 | value = PyDict_GetItemString(modules, "sys"); |
| 227 | if (value != NULL && PyModule_Check(value)) { |
| 228 | char **p; |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 229 | PyObject *v; |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 230 | dict = PyModule_GetDict(value); |
| 231 | for (p = sys_deletes; *p != NULL; p++) { |
| 232 | if (Py_VerboseFlag) |
| 233 | fprintf(stderr, "# clear sys.%s\n", *p); |
| 234 | PyDict_SetItemString(dict, *p, Py_None); |
| 235 | } |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 236 | for (p = sys_files; *p != NULL; p+=2) { |
| 237 | if (Py_VerboseFlag) |
| 238 | fprintf(stderr, "# restore sys.%s\n", *p); |
| 239 | v = PyDict_GetItemString(dict, *(p+1)); |
| 240 | if (v == NULL) |
| 241 | v = Py_None; |
| 242 | PyDict_SetItemString(dict, *p, v); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /* First, delete __main__ */ |
| 247 | value = PyDict_GetItemString(modules, "__main__"); |
| 248 | if (value != NULL && PyModule_Check(value)) { |
| 249 | if (Py_VerboseFlag) |
| 250 | fprintf(stderr, "# cleanup __main__\n"); |
| 251 | _PyModule_Clear(value); |
| 252 | PyDict_SetItemString(modules, "__main__", Py_None); |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 255 | /* The special treatment of __builtin__ here is because even |
| 256 | when it's not referenced as a module, its dictionary is |
| 257 | referenced by almost every module's __builtins__. Since |
| 258 | deleting a module clears its dictionary (even if there are |
| 259 | references left to it), we need to delete the __builtin__ |
| 260 | module last. Likewise, we don't delete sys until the very |
| 261 | end because it is implicitly referenced (e.g. by print). |
| 262 | |
| 263 | Also note that we 'delete' modules by replacing their entry |
| 264 | in the modules dict with None, rather than really deleting |
| 265 | them; this avoids a rehash of the modules dictionary and |
| 266 | also marks them as "non existent" so they won't be |
| 267 | re-imported. */ |
| 268 | |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 269 | /* Next, repeatedly delete modules with a reference count of |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 270 | one (skipping __builtin__ and sys) and delete them */ |
| 271 | do { |
| 272 | ndone = 0; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 273 | pos = 0; |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 274 | while (PyDict_Next(modules, &pos, &key, &value)) { |
| 275 | if (value->ob_refcnt != 1) |
| 276 | continue; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 277 | if (PyModule_Check(value)) { |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 278 | name = PyString_AsString(key); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 279 | if (strcmp(name, "__builtin__") == 0) |
| 280 | continue; |
| 281 | if (strcmp(name, "sys") == 0) |
| 282 | continue; |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 283 | if (Py_VerboseFlag) |
| 284 | fprintf(stderr, |
| 285 | "# cleanup[1] %s\n", name); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 286 | _PyModule_Clear(value); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 287 | PyDict_SetItem(modules, key, Py_None); |
| 288 | ndone++; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 291 | } while (ndone > 0); |
| 292 | |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 293 | /* Next, delete all modules (still skipping __builtin__ and sys) */ |
| 294 | pos = 0; |
| 295 | while (PyDict_Next(modules, &pos, &key, &value)) { |
| 296 | if (PyModule_Check(value)) { |
| 297 | name = PyString_AsString(key); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 298 | if (strcmp(name, "__builtin__") == 0) |
| 299 | continue; |
| 300 | if (strcmp(name, "sys") == 0) |
| 301 | continue; |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 302 | if (Py_VerboseFlag) |
| 303 | fprintf(stderr, "# cleanup[2] %s\n", name); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 304 | _PyModule_Clear(value); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 305 | PyDict_SetItem(modules, key, Py_None); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /* Next, delete sys and __builtin__ (in that order) */ |
| 310 | value = PyDict_GetItemString(modules, "sys"); |
| 311 | if (value != NULL && PyModule_Check(value)) { |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 312 | if (Py_VerboseFlag) |
| 313 | fprintf(stderr, "# cleanup sys\n"); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 314 | _PyModule_Clear(value); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 315 | PyDict_SetItemString(modules, "sys", Py_None); |
| 316 | } |
| 317 | value = PyDict_GetItemString(modules, "__builtin__"); |
| 318 | if (value != NULL && PyModule_Check(value)) { |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 319 | if (Py_VerboseFlag) |
| 320 | fprintf(stderr, "# cleanup __builtin__\n"); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 321 | _PyModule_Clear(value); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 322 | PyDict_SetItemString(modules, "__builtin__", Py_None); |
| 323 | } |
| 324 | |
| 325 | /* Finally, clear and delete the modules directory */ |
| 326 | PyDict_Clear(modules); |
| 327 | interp->modules = NULL; |
| 328 | Py_DECREF(modules); |
Guido van Rossum | 8d15b5d | 1990-10-26 14:58:58 +0000 | [diff] [blame] | 329 | } |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 330 | |
| 331 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 332 | /* Helper for pythonrun.c -- return magic number */ |
| 333 | |
| 334 | long |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 335 | PyImport_GetMagicNumber() |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 336 | { |
| 337 | return MAGIC; |
| 338 | } |
| 339 | |
| 340 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 341 | /* Magic for extension modules (built-in as well as dynamically |
| 342 | loaded). To prevent initializing an extension module more than |
| 343 | once, we keep a static dictionary 'extensions' keyed by module name |
| 344 | (for built-in modules) or by filename (for dynamically loaded |
| 345 | modules), containing these modules. A copy od the module's |
| 346 | dictionary is stored by calling _PyImport_FixupExtension() |
| 347 | immediately after the module initialization function succeeds. A |
| 348 | copy can be retrieved from there by calling |
| 349 | _PyImport_FindExtension(). */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 350 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 351 | PyObject * |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 352 | _PyImport_FixupExtension(name, filename) |
| 353 | char *name; |
| 354 | char *filename; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 355 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 356 | PyObject *modules, *mod, *dict, *copy; |
| 357 | if (extensions == NULL) { |
| 358 | extensions = PyDict_New(); |
| 359 | if (extensions == NULL) |
| 360 | return NULL; |
| 361 | } |
| 362 | modules = PyImport_GetModuleDict(); |
| 363 | mod = PyDict_GetItemString(modules, name); |
| 364 | if (mod == NULL || !PyModule_Check(mod)) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 365 | PyErr_Format(PyExc_SystemError, |
| 366 | "_PyImport_FixupExtension: module %.200s not loaded", name); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 367 | return NULL; |
| 368 | } |
| 369 | dict = PyModule_GetDict(mod); |
| 370 | if (dict == NULL) |
| 371 | return NULL; |
| 372 | copy = PyObject_CallMethod(dict, "copy", ""); |
| 373 | if (copy == NULL) |
| 374 | return NULL; |
| 375 | PyDict_SetItemString(extensions, filename, copy); |
| 376 | Py_DECREF(copy); |
| 377 | return copy; |
| 378 | } |
| 379 | |
| 380 | PyObject * |
| 381 | _PyImport_FindExtension(name, filename) |
| 382 | char *name; |
| 383 | char *filename; |
| 384 | { |
| 385 | PyObject *dict, *mod, *mdict, *result; |
| 386 | if (extensions == NULL) |
| 387 | return NULL; |
| 388 | dict = PyDict_GetItemString(extensions, filename); |
| 389 | if (dict == NULL) |
| 390 | return NULL; |
| 391 | mod = PyImport_AddModule(name); |
| 392 | if (mod == NULL) |
| 393 | return NULL; |
| 394 | mdict = PyModule_GetDict(mod); |
| 395 | if (mdict == NULL) |
| 396 | return NULL; |
| 397 | result = PyObject_CallMethod(mdict, "update", "O", dict); |
| 398 | if (result == NULL) |
| 399 | return NULL; |
| 400 | Py_DECREF(result); |
| 401 | if (Py_VerboseFlag) |
| 402 | fprintf(stderr, "import %s # previously loaded (%s)\n", |
| 403 | name, filename); |
| 404 | return mod; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | |
| 408 | /* Get the module object corresponding to a module name. |
| 409 | First check the modules dictionary if there's one there, |
| 410 | 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] | 411 | Because the former action is most common, THIS DOES NOT RETURN A |
| 412 | 'NEW' REFERENCE! */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 413 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 414 | PyObject * |
| 415 | PyImport_AddModule(name) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 416 | char *name; |
| 417 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 418 | PyObject *modules = PyImport_GetModuleDict(); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 419 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 420 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 421 | if ((m = PyDict_GetItemString(modules, name)) != NULL && |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 422 | PyModule_Check(m)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 423 | return m; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 424 | m = PyModule_New(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 425 | if (m == NULL) |
| 426 | return NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 427 | if (PyDict_SetItemString(modules, name, m) != 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 428 | Py_DECREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 429 | return NULL; |
| 430 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 431 | Py_DECREF(m); /* Yes, it still exists, in modules! */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 432 | |
| 433 | return m; |
| 434 | } |
| 435 | |
| 436 | |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 437 | /* Execute a code object in a module and return the module object |
| 438 | WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 439 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 440 | PyObject * |
| 441 | PyImport_ExecCodeModule(name, co) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 442 | char *name; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 443 | PyObject *co; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 444 | { |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 445 | return PyImport_ExecCodeModuleEx(name, co, (char *)NULL); |
| 446 | } |
| 447 | |
| 448 | PyObject * |
| 449 | PyImport_ExecCodeModuleEx(name, co, pathname) |
| 450 | char *name; |
| 451 | PyObject *co; |
| 452 | char *pathname; |
| 453 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 454 | PyObject *modules = PyImport_GetModuleDict(); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 455 | PyObject *m, *d, *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 456 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 457 | m = PyImport_AddModule(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 458 | if (m == NULL) |
| 459 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 460 | d = PyModule_GetDict(m); |
| 461 | if (PyDict_GetItemString(d, "__builtins__") == NULL) { |
| 462 | if (PyDict_SetItemString(d, "__builtins__", |
| 463 | PyEval_GetBuiltins()) != 0) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 464 | return NULL; |
| 465 | } |
Guido van Rossum | 9c9a07c | 1996-05-16 20:43:40 +0000 | [diff] [blame] | 466 | /* Remember the filename as the __file__ attribute */ |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 467 | v = NULL; |
| 468 | if (pathname != NULL) { |
| 469 | v = PyString_FromString(pathname); |
| 470 | if (v == NULL) |
| 471 | PyErr_Clear(); |
| 472 | } |
| 473 | if (v == NULL) { |
| 474 | v = ((PyCodeObject *)co)->co_filename; |
| 475 | Py_INCREF(v); |
| 476 | } |
| 477 | if (PyDict_SetItemString(d, "__file__", v) != 0) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 478 | PyErr_Clear(); /* Not important enough to report */ |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 479 | Py_DECREF(v); |
| 480 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 481 | v = PyEval_EvalCode((PyCodeObject *)co, d, d); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 482 | if (v == NULL) |
| 483 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 484 | Py_DECREF(v); |
Guido van Rossum | b65e85c | 1997-07-10 18:00:45 +0000 | [diff] [blame] | 485 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 486 | if ((m = PyDict_GetItemString(modules, name)) == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 487 | PyErr_Format(PyExc_ImportError, |
| 488 | "Loaded module %.200s not found in sys.modules", |
| 489 | name); |
Guido van Rossum | b65e85c | 1997-07-10 18:00:45 +0000 | [diff] [blame] | 490 | return NULL; |
| 491 | } |
| 492 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 493 | Py_INCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 494 | |
| 495 | return m; |
| 496 | } |
| 497 | |
| 498 | |
| 499 | /* Given a pathname for a Python source file, fill a buffer with the |
| 500 | pathname for the corresponding compiled file. Return the pathname |
| 501 | for the compiled file, or NULL if there's no space in the buffer. |
| 502 | Doesn't set an exception. */ |
| 503 | |
| 504 | static char * |
| 505 | make_compiled_pathname(pathname, buf, buflen) |
| 506 | char *pathname; |
| 507 | char *buf; |
| 508 | int buflen; |
| 509 | { |
| 510 | int len; |
| 511 | |
| 512 | len = strlen(pathname); |
| 513 | if (len+2 > buflen) |
| 514 | return NULL; |
| 515 | strcpy(buf, pathname); |
Guido van Rossum | 0824f63 | 1997-03-11 18:37:35 +0000 | [diff] [blame] | 516 | strcpy(buf+len, Py_OptimizeFlag ? "o" : "c"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 517 | |
| 518 | return buf; |
| 519 | } |
| 520 | |
| 521 | |
| 522 | /* Given a pathname for a Python source file, its time of last |
| 523 | modification, and a pathname for a compiled file, check whether the |
| 524 | compiled file represents the same version of the source. If so, |
| 525 | return a FILE pointer for the compiled file, positioned just after |
| 526 | the header; if not, return NULL. |
| 527 | Doesn't set an exception. */ |
| 528 | |
| 529 | static FILE * |
| 530 | check_compiled_module(pathname, mtime, cpathname) |
| 531 | char *pathname; |
| 532 | long mtime; |
| 533 | char *cpathname; |
| 534 | { |
| 535 | FILE *fp; |
| 536 | long magic; |
| 537 | long pyc_mtime; |
| 538 | |
| 539 | fp = fopen(cpathname, "rb"); |
| 540 | if (fp == NULL) |
| 541 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 542 | magic = PyMarshal_ReadLongFromFile(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 543 | if (magic != MAGIC) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 544 | if (Py_VerboseFlag) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 545 | fprintf(stderr, "# %s has bad magic\n", cpathname); |
| 546 | fclose(fp); |
| 547 | return NULL; |
| 548 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 549 | pyc_mtime = PyMarshal_ReadLongFromFile(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 550 | if (pyc_mtime != mtime) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 551 | if (Py_VerboseFlag) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 552 | fprintf(stderr, "# %s has bad mtime\n", cpathname); |
| 553 | fclose(fp); |
| 554 | return NULL; |
| 555 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 556 | if (Py_VerboseFlag) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 557 | fprintf(stderr, "# %s matches %s\n", cpathname, pathname); |
| 558 | return fp; |
| 559 | } |
| 560 | |
| 561 | |
| 562 | /* Read a code object from a file and check it for validity */ |
| 563 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 564 | static PyCodeObject * |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 565 | read_compiled_module(cpathname, fp) |
| 566 | char *cpathname; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 567 | FILE *fp; |
| 568 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 569 | PyObject *co; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 570 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 571 | co = PyMarshal_ReadObjectFromFile(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 572 | /* Ugly: rd_object() may return NULL with or without error */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 573 | if (co == NULL || !PyCode_Check(co)) { |
| 574 | if (!PyErr_Occurred()) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 575 | PyErr_Format(PyExc_ImportError, |
| 576 | "Non-code object in %.200s", cpathname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 577 | Py_XDECREF(co); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 578 | return NULL; |
| 579 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 580 | return (PyCodeObject *)co; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | |
| 584 | /* 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] | 585 | module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 586 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 587 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 588 | load_compiled_module(name, cpathname, fp) |
| 589 | char *name; |
| 590 | char *cpathname; |
| 591 | FILE *fp; |
| 592 | { |
| 593 | long magic; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 594 | PyCodeObject *co; |
| 595 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 596 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 597 | magic = PyMarshal_ReadLongFromFile(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 598 | if (magic != MAGIC) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 599 | PyErr_Format(PyExc_ImportError, |
| 600 | "Bad magic number in %.200s", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 601 | return NULL; |
| 602 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 603 | (void) PyMarshal_ReadLongFromFile(fp); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 604 | co = read_compiled_module(cpathname, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 605 | if (co == NULL) |
| 606 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 607 | if (Py_VerboseFlag) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 608 | fprintf(stderr, "import %s # precompiled from %s\n", |
| 609 | name, cpathname); |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 610 | m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 611 | Py_DECREF(co); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 612 | |
| 613 | return m; |
| 614 | } |
| 615 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 616 | /* Parse a source file and return the corresponding code object */ |
| 617 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 618 | static PyCodeObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 619 | parse_source_module(pathname, fp) |
| 620 | char *pathname; |
| 621 | FILE *fp; |
| 622 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 623 | PyCodeObject *co; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 624 | node *n; |
| 625 | |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 626 | n = PyParser_SimpleParseFile(fp, pathname, Py_file_input); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 627 | if (n == NULL) |
| 628 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 629 | co = PyNode_Compile(n, pathname); |
| 630 | PyNode_Free(n); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 631 | |
| 632 | return co; |
| 633 | } |
| 634 | |
| 635 | |
| 636 | /* Write a compiled module to a file, placing the time of last |
| 637 | modification of its source into the header. |
| 638 | Errors are ignored, if a write error occurs an attempt is made to |
| 639 | remove the file. */ |
| 640 | |
| 641 | static void |
| 642 | write_compiled_module(co, cpathname, mtime) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 643 | PyCodeObject *co; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 644 | char *cpathname; |
| 645 | long mtime; |
| 646 | { |
| 647 | FILE *fp; |
| 648 | |
| 649 | fp = fopen(cpathname, "wb"); |
| 650 | if (fp == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 651 | if (Py_VerboseFlag) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 652 | fprintf(stderr, |
| 653 | "# can't create %s\n", cpathname); |
| 654 | return; |
| 655 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 656 | PyMarshal_WriteLongToFile(MAGIC, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 657 | /* First write a 0 for mtime */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 658 | PyMarshal_WriteLongToFile(0L, fp); |
| 659 | PyMarshal_WriteObjectToFile((PyObject *)co, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 660 | if (ferror(fp)) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 661 | if (Py_VerboseFlag) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 662 | fprintf(stderr, "# can't write %s\n", cpathname); |
| 663 | /* Don't keep partial file */ |
| 664 | fclose(fp); |
| 665 | (void) unlink(cpathname); |
| 666 | return; |
| 667 | } |
| 668 | /* Now write the true mtime */ |
| 669 | fseek(fp, 4L, 0); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 670 | PyMarshal_WriteLongToFile(mtime, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 671 | fflush(fp); |
| 672 | fclose(fp); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 673 | if (Py_VerboseFlag) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 674 | fprintf(stderr, "# wrote %s\n", cpathname); |
| 675 | #ifdef macintosh |
Guido van Rossum | be1a6e2 | 1996-02-21 15:29:20 +0000 | [diff] [blame] | 676 | setfiletype(cpathname, 'Pyth', 'PYC '); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 677 | #endif |
| 678 | } |
| 679 | |
| 680 | |
| 681 | /* 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] | 682 | object WITH INCREMENTED REFERENCE COUNT. If there's a matching |
| 683 | byte-compiled file, use that instead. */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 684 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 685 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 686 | load_source_module(name, pathname, fp) |
| 687 | char *name; |
| 688 | char *pathname; |
| 689 | FILE *fp; |
| 690 | { |
| 691 | long mtime; |
| 692 | FILE *fpc; |
| 693 | char buf[MAXPATHLEN+1]; |
| 694 | char *cpathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 695 | PyCodeObject *co; |
| 696 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 697 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 698 | mtime = PyOS_GetLastModificationTime(pathname, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 699 | cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1); |
| 700 | if (cpathname != NULL && |
| 701 | (fpc = check_compiled_module(pathname, mtime, cpathname))) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 702 | co = read_compiled_module(cpathname, fpc); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 703 | fclose(fpc); |
| 704 | if (co == NULL) |
| 705 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 706 | if (Py_VerboseFlag) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 707 | fprintf(stderr, "import %s # precompiled from %s\n", |
| 708 | name, cpathname); |
| 709 | } |
| 710 | else { |
| 711 | co = parse_source_module(pathname, fp); |
| 712 | if (co == NULL) |
| 713 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 714 | if (Py_VerboseFlag) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 715 | fprintf(stderr, "import %s # from %s\n", |
| 716 | name, pathname); |
| 717 | write_compiled_module(co, cpathname, mtime); |
| 718 | } |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 719 | m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 720 | Py_DECREF(co); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 721 | |
| 722 | return m; |
| 723 | } |
| 724 | |
| 725 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 726 | /* Forward */ |
| 727 | static PyObject *load_module Py_PROTO((char *, FILE *, char *, int)); |
| 728 | static struct filedescr *find_module Py_PROTO((char *, PyObject *, |
| 729 | char *, int, FILE **)); |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 730 | static struct _frozen *find_frozen Py_PROTO((char *name)); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 731 | |
| 732 | /* Load a package and return its module object WITH INCREMENTED |
| 733 | REFERENCE COUNT */ |
| 734 | |
| 735 | static PyObject * |
| 736 | load_package(name, pathname) |
| 737 | char *name; |
| 738 | char *pathname; |
| 739 | { |
| 740 | PyObject *m, *d, *file, *path; |
| 741 | int err; |
| 742 | char buf[MAXPATHLEN+1]; |
| 743 | FILE *fp = NULL; |
| 744 | struct filedescr *fdp; |
| 745 | |
| 746 | m = PyImport_AddModule(name); |
| 747 | if (m == NULL) |
| 748 | return NULL; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 749 | if (Py_VerboseFlag) |
| 750 | fprintf(stderr, "import %s # directory %s\n", |
| 751 | name, pathname); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 752 | d = PyModule_GetDict(m); |
| 753 | file = PyString_FromString(pathname); |
| 754 | if (file == NULL) |
| 755 | return NULL; |
| 756 | path = Py_BuildValue("[O]", file); |
| 757 | if (path == NULL) { |
| 758 | Py_DECREF(file); |
| 759 | return NULL; |
| 760 | } |
| 761 | err = PyDict_SetItemString(d, "__file__", file); |
| 762 | if (err == 0) |
| 763 | err = PyDict_SetItemString(d, "__path__", path); |
| 764 | if (err != 0) { |
| 765 | m = NULL; |
| 766 | goto cleanup; |
| 767 | } |
| 768 | buf[0] = '\0'; |
| 769 | fdp = find_module("__init__", path, buf, sizeof(buf), &fp); |
| 770 | if (fdp == NULL) { |
| 771 | if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 772 | PyErr_Clear(); |
| 773 | } |
| 774 | else |
| 775 | m = NULL; |
| 776 | goto cleanup; |
| 777 | } |
| 778 | m = load_module(name, fp, buf, fdp->type); |
| 779 | if (fp != NULL) |
| 780 | fclose(fp); |
| 781 | cleanup: |
| 782 | Py_XINCREF(m); |
| 783 | Py_XDECREF(path); |
| 784 | Py_XDECREF(file); |
| 785 | return m; |
| 786 | } |
| 787 | |
| 788 | |
| 789 | /* Helper to test for built-in module */ |
| 790 | |
| 791 | static int |
| 792 | is_builtin(name) |
| 793 | char *name; |
| 794 | { |
| 795 | int i; |
Guido van Rossum | 771c6c8 | 1997-10-31 18:37:24 +0000 | [diff] [blame] | 796 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) { |
| 797 | if (strcmp(name, PyImport_Inittab[i].name) == 0) { |
| 798 | if (PyImport_Inittab[i].initfunc == NULL) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 799 | return -1; |
| 800 | else |
| 801 | return 1; |
| 802 | } |
| 803 | } |
| 804 | return 0; |
| 805 | } |
| 806 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 807 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 808 | /* Search the path (default sys.path) for a module. Return the |
| 809 | corresponding filedescr struct, and (via return arguments) the |
| 810 | pathname and an open file. Return NULL if the module is not found. */ |
| 811 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 812 | #ifdef MS_COREDLL |
| 813 | extern FILE *PyWin_FindRegisteredModule(); |
| 814 | #endif |
| 815 | |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 816 | #ifdef CHECK_IMPORT_CASE |
| 817 | static int check_case(char *, int, int, char *); |
| 818 | #endif |
| 819 | |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 820 | static int find_init_module Py_PROTO((char *)); /* Forward */ |
| 821 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 822 | static struct filedescr * |
| 823 | find_module(name, path, buf, buflen, p_fp) |
| 824 | char *name; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 825 | PyObject *path; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 826 | /* Output parameters: */ |
| 827 | char *buf; |
| 828 | int buflen; |
| 829 | FILE **p_fp; |
| 830 | { |
| 831 | int i, npath, len, namelen; |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 832 | struct _frozen *f; |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 833 | struct filedescr *fdp = NULL; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 834 | FILE *fp = NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 835 | struct stat statbuf; |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 836 | static struct filedescr fd_frozen = {"", "", PY_FROZEN}; |
| 837 | static struct filedescr fd_builtin = {"", "", C_BUILTIN}; |
| 838 | static struct filedescr fd_package = {"", "", PKG_DIRECTORY}; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 839 | |
| 840 | if (path == NULL) { |
| 841 | if (is_builtin(name)) { |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 842 | strcpy(buf, name); |
| 843 | return &fd_builtin; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 844 | } |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 845 | if ((f = find_frozen(name)) != NULL) { |
| 846 | strcpy(buf, name); |
| 847 | return &fd_frozen; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 848 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 849 | |
Guido van Rossum | ac27910 | 1996-08-22 23:10:58 +0000 | [diff] [blame] | 850 | #ifdef MS_COREDLL |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 851 | fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen); |
| 852 | if (fp != NULL) { |
| 853 | *p_fp = fp; |
| 854 | return fdp; |
| 855 | } |
Guido van Rossum | a5a3db7 | 1996-04-09 02:39:59 +0000 | [diff] [blame] | 856 | #endif |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 857 | path = PySys_GetObject("path"); |
| 858 | } |
| 859 | else if (PyString_Check(path)) { |
| 860 | /* Submodule of frozen package */ |
Guido van Rossum | 39b0f89 | 1998-04-10 21:52:06 +0000 | [diff] [blame] | 861 | if (PyString_Size(path) + 1 + strlen(name) >= (unsigned int)buflen) { |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 862 | PyErr_SetString(PyExc_ImportError, |
| 863 | "full frozen module name too long"); |
| 864 | return NULL; |
| 865 | } |
| 866 | strcpy(buf, PyString_AsString(path)); |
| 867 | strcat(buf, "."); |
| 868 | strcat(buf, name); |
| 869 | if (find_frozen(buf) != NULL) |
| 870 | return &fd_frozen; |
| 871 | PyErr_Format(PyExc_ImportError, |
| 872 | "frozen module %.200s not found", buf); |
| 873 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 874 | } |
Guido van Rossum | a5a3db7 | 1996-04-09 02:39:59 +0000 | [diff] [blame] | 875 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 876 | if (path == NULL || !PyList_Check(path)) { |
| 877 | PyErr_SetString(PyExc_ImportError, |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 878 | "sys.path must be a list of directory names"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 879 | return NULL; |
| 880 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 881 | npath = PyList_Size(path); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 882 | namelen = strlen(name); |
| 883 | for (i = 0; i < npath; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 884 | PyObject *v = PyList_GetItem(path, i); |
| 885 | if (!PyString_Check(v)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 886 | continue; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 887 | len = PyString_Size(v); |
Guido van Rossum | ef3d02e | 1997-07-21 14:54:36 +0000 | [diff] [blame] | 888 | if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 889 | continue; /* Too long */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 890 | strcpy(buf, PyString_AsString(v)); |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 891 | if ((int)strlen(buf) != len) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 892 | continue; /* v contains '\0' */ |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 893 | #ifdef macintosh |
Guido van Rossum | 741689d | 1997-08-12 14:53:39 +0000 | [diff] [blame] | 894 | #ifdef INTERN_STRINGS |
| 895 | /* |
| 896 | ** Speedup: each sys.path item is interned, and |
| 897 | ** FindResourceModule remembers which items refer to |
| 898 | ** folders (so we don't have to bother trying to look |
| 899 | ** into them for resources). |
| 900 | */ |
| 901 | PyString_InternInPlace(&PyList_GET_ITEM(path, i)); |
| 902 | v = PyList_GET_ITEM(path, i); |
| 903 | #endif |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 904 | if (PyMac_FindResourceModule((PyStringObject *)v, name, buf)) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 905 | static struct filedescr resfiledescr = |
| 906 | {"", "", PY_RESOURCE}; |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 907 | |
| 908 | return &resfiledescr; |
| 909 | } |
| 910 | #endif |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 911 | if (len > 0 && buf[len-1] != SEP |
| 912 | #ifdef ALTSEP |
| 913 | && buf[len-1] != ALTSEP |
| 914 | #endif |
| 915 | ) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 916 | buf[len++] = SEP; |
Guido van Rossum | 40f470f | 1996-05-23 22:51:04 +0000 | [diff] [blame] | 917 | #ifdef IMPORT_8x3_NAMES |
Guido van Rossum | e8c27bb | 1997-12-17 18:57:16 +0000 | [diff] [blame] | 918 | /* see if we are searching in directory dos-8x3 */ |
| 919 | if (len > 7 && !strncmp(buf + len - 8, "dos-8x3", 7)){ |
Guido van Rossum | 40f470f | 1996-05-23 22:51:04 +0000 | [diff] [blame] | 920 | int j; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 921 | char ch; /* limit name to 8 lower-case characters */ |
Guido van Rossum | 40f470f | 1996-05-23 22:51:04 +0000 | [diff] [blame] | 922 | for (j = 0; (ch = name[j]) && j < 8; j++) |
| 923 | if (isupper(ch)) |
| 924 | buf[len++] = tolower(ch); |
| 925 | else |
| 926 | buf[len++] = ch; |
| 927 | } |
Guido van Rossum | e8c27bb | 1997-12-17 18:57:16 +0000 | [diff] [blame] | 928 | else /* Not in dos-8x3, use the full name */ |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 929 | #endif |
Guido van Rossum | 0e41c8c | 1996-06-20 14:18:34 +0000 | [diff] [blame] | 930 | { |
Guido van Rossum | 40f470f | 1996-05-23 22:51:04 +0000 | [diff] [blame] | 931 | strcpy(buf+len, name); |
| 932 | len += namelen; |
| 933 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 934 | #ifdef HAVE_STAT |
| 935 | if (stat(buf, &statbuf) == 0) { |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 936 | if (S_ISDIR(statbuf.st_mode)) { |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 937 | if (find_init_module(buf)) { |
| 938 | #ifdef CHECK_IMPORT_CASE |
| 939 | if (!check_case(buf, len, namelen, |
| 940 | name)) |
| 941 | return NULL; |
| 942 | #endif |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 943 | return &fd_package; |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 944 | } |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 945 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 946 | } |
| 947 | #else |
| 948 | /* XXX How are you going to test for directories? */ |
| 949 | #endif |
Guido van Rossum | 9a61dc9 | 1997-10-08 15:25:08 +0000 | [diff] [blame] | 950 | #ifdef macintosh |
| 951 | fdp = PyMac_FindModuleExtension(buf, &len, name); |
| 952 | if (fdp) |
| 953 | fp = fopen(buf, fdp->mode); |
| 954 | #else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 955 | for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 956 | strcpy(buf+len, fdp->suffix); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 957 | if (Py_VerboseFlag > 1) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 958 | fprintf(stderr, "# trying %s\n", buf); |
| 959 | fp = fopen(buf, fdp->mode); |
| 960 | if (fp != NULL) |
| 961 | break; |
| 962 | } |
Guido van Rossum | 741689d | 1997-08-12 14:53:39 +0000 | [diff] [blame] | 963 | #endif /* !macintosh */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 964 | if (fp != NULL) |
| 965 | break; |
| 966 | } |
| 967 | if (fp == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 968 | PyErr_Format(PyExc_ImportError, |
| 969 | "No module named %.200s", name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 970 | return NULL; |
| 971 | } |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 972 | #ifdef CHECK_IMPORT_CASE |
| 973 | if (!check_case(buf, len, namelen, name)) { |
| 974 | fclose(fp); |
| 975 | return NULL; |
| 976 | } |
| 977 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 978 | |
| 979 | *p_fp = fp; |
| 980 | return fdp; |
| 981 | } |
| 982 | |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 983 | #ifdef CHECK_IMPORT_CASE |
| 984 | |
| 985 | #ifdef MS_WIN32 |
| 986 | #include <windows.h> |
| 987 | static int |
| 988 | check_case(char *buf, int len, int namelen, char *name) |
| 989 | { |
| 990 | WIN32_FIND_DATA data; |
| 991 | HANDLE h; |
| 992 | if (getenv("PYTHONCASEOK") != NULL) |
| 993 | return 1; |
| 994 | h = FindFirstFile(buf, &data); |
| 995 | if (h == INVALID_HANDLE_VALUE) { |
| 996 | PyErr_Format(PyExc_NameError, |
| 997 | "Can't find file for module %.100s\n(filename %.300s)", |
| 998 | name, buf); |
| 999 | return 0; |
| 1000 | } |
| 1001 | FindClose(h); |
| 1002 | if (strncmp(data.cFileName, name, namelen) != 0) { |
| 1003 | strcpy(buf+len-namelen, data.cFileName); |
| 1004 | PyErr_Format(PyExc_NameError, |
| 1005 | "Case mismatch for module name %.100s\n(filename %.300s)", |
| 1006 | name, buf); |
| 1007 | return 0; |
| 1008 | } |
| 1009 | return 1; |
| 1010 | } |
| 1011 | #endif /* MS_WIN32 */ |
| 1012 | |
| 1013 | #ifdef macintosh |
| 1014 | #include <TextUtils.h> |
| 1015 | static int |
| 1016 | check_case(char *buf, int len, int namelen, char *name) |
| 1017 | { |
| 1018 | FSSpec fss; |
| 1019 | OSErr err; |
| 1020 | unsigned char mybuf[MAXPATHLEN+1]; |
| 1021 | |
| 1022 | strcpy((char *)mybuf, buf); |
| 1023 | c2pstr((char *)mybuf); |
| 1024 | err = FSMakeFSSpec(0, 0, mybuf, &fss); |
| 1025 | if (err) { |
| 1026 | PyErr_Format(PyExc_NameError, |
| 1027 | "Can't find file for module %.100s\n(filename %.300s)", |
| 1028 | name, buf); |
| 1029 | return 0; |
| 1030 | } |
| 1031 | p2cstr(fss.name); |
| 1032 | if ( strncmp(name, (char *)fss.name, namelen) != 0 ) { |
| 1033 | PyErr_Format(PyExc_NameError, |
| 1034 | "Case mismatch for module name %.100s\n(filename %.300s)", |
| 1035 | name, fss.name); |
| 1036 | return 0; |
| 1037 | } |
| 1038 | return 1; |
| 1039 | } |
| 1040 | #endif /* macintosh */ |
| 1041 | |
Guido van Rossum | 4d1b3b9 | 1998-02-13 23:27:59 +0000 | [diff] [blame] | 1042 | #ifdef DJGPP |
Guido van Rossum | aeca79b | 1998-02-18 16:21:00 +0000 | [diff] [blame] | 1043 | #include <dir.h> |
| 1044 | |
Guido van Rossum | 4d1b3b9 | 1998-02-13 23:27:59 +0000 | [diff] [blame] | 1045 | static int |
| 1046 | check_case(char *buf, int len, int namelen, char *name) |
| 1047 | { |
| 1048 | struct ffblk ffblk; |
| 1049 | int done; |
| 1050 | |
| 1051 | if (getenv("PYTHONCASEOK") != NULL) |
| 1052 | return 1; |
Guido van Rossum | aeca79b | 1998-02-18 16:21:00 +0000 | [diff] [blame] | 1053 | done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC); |
Guido van Rossum | 4d1b3b9 | 1998-02-13 23:27:59 +0000 | [diff] [blame] | 1054 | if (done) { |
| 1055 | PyErr_Format(PyExc_NameError, |
| 1056 | "Can't find file for module %.100s\n(filename %.300s)", |
| 1057 | name, buf); |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | if (strncmp(ffblk.ff_name, name, namelen) != 0) { |
| 1062 | strcpy(buf+len-namelen, ffblk.ff_name); |
| 1063 | PyErr_Format(PyExc_NameError, |
| 1064 | "Case mismatch for module name %.100s\n(filename %.300s)", |
| 1065 | name, buf); |
| 1066 | return 0; |
| 1067 | } |
| 1068 | return 1; |
| 1069 | } |
| 1070 | #endif |
| 1071 | |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1072 | #endif CHECK_IMPORT_CASE |
| 1073 | |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1074 | #ifdef HAVE_STAT |
| 1075 | /* Helper to look for __init__.py or __init__.py[co] in potential package */ |
| 1076 | static int |
| 1077 | find_init_module(buf) |
| 1078 | char *buf; |
| 1079 | { |
| 1080 | int save_len = strlen(buf); |
| 1081 | int i = save_len; |
| 1082 | struct stat statbuf; |
| 1083 | |
| 1084 | if (save_len + 13 >= MAXPATHLEN) |
| 1085 | return 0; |
| 1086 | buf[i++] = SEP; |
| 1087 | strcpy(buf+i, "__init__.py"); |
| 1088 | if (stat(buf, &statbuf) == 0) { |
| 1089 | buf[save_len] = '\0'; |
| 1090 | return 1; |
| 1091 | } |
| 1092 | i += strlen(buf+i); |
| 1093 | if (Py_OptimizeFlag) |
| 1094 | strcpy(buf+i, "o"); |
| 1095 | else |
| 1096 | strcpy(buf+i, "c"); |
| 1097 | if (stat(buf, &statbuf) == 0) { |
| 1098 | buf[save_len] = '\0'; |
| 1099 | return 1; |
| 1100 | } |
| 1101 | buf[save_len] = '\0'; |
| 1102 | return 0; |
| 1103 | } |
| 1104 | #endif /* HAVE_STAT */ |
| 1105 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1106 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1107 | static int init_builtin Py_PROTO((char *)); /* Forward */ |
| 1108 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1109 | /* Load an external module using the default search path and return |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1110 | its module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1111 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1112 | static PyObject * |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1113 | load_module(name, fp, buf, type) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1114 | char *name; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1115 | FILE *fp; |
| 1116 | char *buf; |
| 1117 | int type; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1118 | { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1119 | PyObject *modules; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1120 | PyObject *m; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1121 | int err; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1122 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1123 | /* First check that there's an open file (if we need one) */ |
| 1124 | switch (type) { |
| 1125 | case PY_SOURCE: |
| 1126 | case PY_COMPILED: |
| 1127 | if (fp == NULL) { |
| 1128 | PyErr_Format(PyExc_ValueError, |
| 1129 | "file object required for import (type code %d)", |
| 1130 | type); |
| 1131 | return NULL; |
| 1132 | } |
| 1133 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1134 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1135 | switch (type) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1136 | |
| 1137 | case PY_SOURCE: |
| 1138 | m = load_source_module(name, buf, fp); |
| 1139 | break; |
| 1140 | |
| 1141 | case PY_COMPILED: |
| 1142 | m = load_compiled_module(name, buf, fp); |
| 1143 | break; |
| 1144 | |
| 1145 | case C_EXTENSION: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1146 | m = _PyImport_LoadDynamicModule(name, buf, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1147 | break; |
| 1148 | |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 1149 | #ifdef macintosh |
| 1150 | case PY_RESOURCE: |
| 1151 | m = PyMac_LoadResourceModule(name, buf); |
| 1152 | break; |
| 1153 | #endif |
| 1154 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1155 | case PKG_DIRECTORY: |
| 1156 | m = load_package(name, buf); |
| 1157 | break; |
| 1158 | |
| 1159 | case C_BUILTIN: |
| 1160 | case PY_FROZEN: |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1161 | if (buf != NULL && buf[0] != '\0') |
| 1162 | name = buf; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1163 | if (type == C_BUILTIN) |
| 1164 | err = init_builtin(name); |
| 1165 | else |
| 1166 | err = PyImport_ImportFrozenModule(name); |
| 1167 | if (err < 0) |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 1168 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1169 | if (err == 0) { |
| 1170 | PyErr_Format(PyExc_ImportError, |
| 1171 | "Purported %s module %.200s not found", |
| 1172 | type == C_BUILTIN ? |
| 1173 | "builtin" : "frozen", |
| 1174 | name); |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 1175 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1176 | } |
| 1177 | modules = PyImport_GetModuleDict(); |
| 1178 | m = PyDict_GetItemString(modules, name); |
| 1179 | if (m == NULL) { |
| 1180 | PyErr_Format( |
| 1181 | PyExc_ImportError, |
| 1182 | "%s module %.200s not properly initialized", |
| 1183 | type == C_BUILTIN ? |
| 1184 | "builtin" : "frozen", |
| 1185 | name); |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 1186 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1187 | } |
| 1188 | Py_INCREF(m); |
| 1189 | break; |
| 1190 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1191 | default: |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1192 | PyErr_Format(PyExc_ImportError, |
| 1193 | "Don't know how to import %.200s (type code %d)", |
| 1194 | name, type); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1195 | m = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1196 | |
| 1197 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1198 | |
| 1199 | return m; |
| 1200 | } |
| 1201 | |
| 1202 | |
| 1203 | /* Initialize a built-in module. |
| 1204 | Return 1 for succes, 0 if the module is not found, and -1 with |
| 1205 | an exception set if the initialization failed. */ |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1206 | |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1207 | static int |
| 1208 | init_builtin(name) |
| 1209 | char *name; |
| 1210 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1211 | struct _inittab *p; |
| 1212 | PyObject *mod; |
| 1213 | |
| 1214 | if ((mod = _PyImport_FindExtension(name, name)) != NULL) |
| 1215 | return 1; |
| 1216 | |
Guido van Rossum | 771c6c8 | 1997-10-31 18:37:24 +0000 | [diff] [blame] | 1217 | for (p = PyImport_Inittab; p->name != NULL; p++) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1218 | if (strcmp(name, p->name) == 0) { |
| 1219 | if (p->initfunc == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1220 | PyErr_Format(PyExc_ImportError, |
| 1221 | "Cannot re-init internal module %.200s", |
| 1222 | name); |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1223 | return -1; |
| 1224 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1225 | if (Py_VerboseFlag) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1226 | fprintf(stderr, "import %s # builtin\n", name); |
| 1227 | (*p->initfunc)(); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1228 | if (PyErr_Occurred()) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1229 | return -1; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1230 | if (_PyImport_FixupExtension(name, name) == NULL) |
| 1231 | return -1; |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1232 | return 1; |
| 1233 | } |
| 1234 | } |
| 1235 | return 0; |
| 1236 | } |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1237 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1238 | |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1239 | /* Frozen modules */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1240 | |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1241 | static struct _frozen * |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1242 | find_frozen(name) |
| 1243 | char *name; |
| 1244 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1245 | struct _frozen *p; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1246 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1247 | for (p = PyImport_FrozenModules; ; p++) { |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1248 | if (p->name == NULL) |
| 1249 | return NULL; |
| 1250 | if (strcmp(p->name, name) == 0) |
| 1251 | break; |
| 1252 | } |
| 1253 | return p; |
| 1254 | } |
| 1255 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1256 | static PyObject * |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1257 | get_frozen_object(name) |
| 1258 | char *name; |
| 1259 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1260 | struct _frozen *p = find_frozen(name); |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1261 | int size; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1262 | |
| 1263 | if (p == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1264 | PyErr_Format(PyExc_ImportError, |
| 1265 | "No such frozen object named %.200s", |
| 1266 | name); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1267 | return NULL; |
| 1268 | } |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1269 | size = p->size; |
| 1270 | if (size < 0) |
| 1271 | size = -size; |
| 1272 | return PyMarshal_ReadObjectFromString((char *)p->code, size); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | /* Initialize a frozen module. |
| 1276 | Return 1 for succes, 0 if the module is not found, and -1 with |
| 1277 | an exception set if the initialization failed. |
| 1278 | This function is also used from frozenmain.c */ |
Guido van Rossum | 0b34490 | 1995-02-07 15:35:27 +0000 | [diff] [blame] | 1279 | |
| 1280 | int |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1281 | PyImport_ImportFrozenModule(name) |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1282 | char *name; |
| 1283 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1284 | struct _frozen *p = find_frozen(name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1285 | PyObject *co; |
| 1286 | PyObject *m; |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1287 | int ispackage; |
| 1288 | int size; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1289 | |
| 1290 | if (p == NULL) |
| 1291 | return 0; |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1292 | size = p->size; |
| 1293 | ispackage = (size < 0); |
| 1294 | if (ispackage) |
| 1295 | size = -size; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1296 | if (Py_VerboseFlag) |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1297 | fprintf(stderr, "import %s # frozen%s\n", |
| 1298 | name, ispackage ? " package" : ""); |
| 1299 | co = PyMarshal_ReadObjectFromString((char *)p->code, size); |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1300 | if (co == NULL) |
| 1301 | return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1302 | if (!PyCode_Check(co)) { |
| 1303 | Py_DECREF(co); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1304 | PyErr_Format(PyExc_TypeError, |
| 1305 | "frozen object %.200s is not a code object", |
| 1306 | name); |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1307 | return -1; |
| 1308 | } |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1309 | if (ispackage) { |
| 1310 | /* Set __path__ to the package name */ |
| 1311 | PyObject *d, *s; |
| 1312 | int err; |
| 1313 | m = PyImport_AddModule(name); |
| 1314 | if (m == NULL) |
| 1315 | return -1; |
| 1316 | d = PyModule_GetDict(m); |
| 1317 | s = PyString_InternFromString(name); |
| 1318 | if (s == NULL) |
| 1319 | return -1; |
| 1320 | err = PyDict_SetItemString(d, "__path__", s); |
| 1321 | Py_DECREF(s); |
| 1322 | if (err != 0) |
| 1323 | return err; |
| 1324 | } |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 1325 | m = PyImport_ExecCodeModuleEx(name, co, "<frozen>"); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1326 | Py_DECREF(co); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1327 | if (m == NULL) |
| 1328 | return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1329 | Py_DECREF(m); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1330 | return 1; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1331 | } |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1332 | |
| 1333 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1334 | /* Import a module, either built-in, frozen, or external, and return |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1335 | its module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1336 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1337 | PyObject * |
| 1338 | PyImport_ImportModule(name) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1339 | char *name; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1340 | { |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1341 | static PyObject *fromlist = NULL; |
| 1342 | if (fromlist == NULL && strchr(name, '.') != NULL) { |
| 1343 | fromlist = Py_BuildValue("[s]", "*"); |
| 1344 | if (fromlist == NULL) |
| 1345 | return NULL; |
| 1346 | } |
| 1347 | return PyImport_ImportModuleEx(name, NULL, NULL, fromlist); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1350 | /* Forward declarations for helper routines */ |
| 1351 | static PyObject *get_parent Py_PROTO((PyObject *globals, |
| 1352 | char *buf, int *p_buflen)); |
| 1353 | static PyObject *load_next Py_PROTO((PyObject *mod, PyObject *altmod, |
| 1354 | char **p_name, char *buf, int *p_buflen)); |
Guido van Rossum | f5f5fdb | 1997-09-06 20:29:52 +0000 | [diff] [blame] | 1355 | static int mark_miss Py_PROTO((char *name)); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1356 | static int ensure_fromlist Py_PROTO((PyObject *mod, PyObject *fromlist, |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1357 | char *buf, int buflen, int recursive)); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1358 | static PyObject * import_submodule Py_PROTO((PyObject *mod, |
| 1359 | char *name, char *fullname)); |
| 1360 | |
| 1361 | /* The Magnum Opus of dotted-name import :-) */ |
| 1362 | |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 1363 | static PyObject * |
| 1364 | import_module_ex(name, globals, locals, fromlist) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1365 | char *name; |
| 1366 | PyObject *globals; |
| 1367 | PyObject *locals; |
| 1368 | PyObject *fromlist; |
| 1369 | { |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1370 | char buf[MAXPATHLEN+1]; |
| 1371 | int buflen = 0; |
| 1372 | PyObject *parent, *head, *next, *tail; |
| 1373 | |
| 1374 | parent = get_parent(globals, buf, &buflen); |
| 1375 | if (parent == NULL) |
| 1376 | return NULL; |
| 1377 | |
| 1378 | head = load_next(parent, Py_None, &name, buf, &buflen); |
| 1379 | if (head == NULL) |
| 1380 | return NULL; |
| 1381 | |
| 1382 | tail = head; |
| 1383 | Py_INCREF(tail); |
| 1384 | while (name) { |
| 1385 | next = load_next(tail, tail, &name, buf, &buflen); |
| 1386 | Py_DECREF(tail); |
| 1387 | if (next == NULL) { |
| 1388 | Py_DECREF(head); |
| 1389 | return NULL; |
| 1390 | } |
| 1391 | tail = next; |
| 1392 | } |
| 1393 | |
| 1394 | if (fromlist != NULL) { |
| 1395 | if (fromlist == Py_None || !PyObject_IsTrue(fromlist)) |
| 1396 | fromlist = NULL; |
| 1397 | } |
| 1398 | |
| 1399 | if (fromlist == NULL) { |
| 1400 | Py_DECREF(tail); |
| 1401 | return head; |
| 1402 | } |
| 1403 | |
| 1404 | Py_DECREF(head); |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1405 | if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) { |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1406 | Py_DECREF(tail); |
| 1407 | return NULL; |
| 1408 | } |
| 1409 | |
| 1410 | return tail; |
| 1411 | } |
| 1412 | |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 1413 | PyObject * |
| 1414 | PyImport_ImportModuleEx(name, globals, locals, fromlist) |
| 1415 | char *name; |
| 1416 | PyObject *globals; |
| 1417 | PyObject *locals; |
| 1418 | PyObject *fromlist; |
| 1419 | { |
| 1420 | PyObject *result; |
| 1421 | lock_import(); |
Guido van Rossum | d65911b | 1998-03-03 22:33:27 +0000 | [diff] [blame] | 1422 | result = import_module_ex(name, globals, locals, fromlist); |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 1423 | unlock_import(); |
| 1424 | return result; |
| 1425 | } |
| 1426 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1427 | static PyObject * |
| 1428 | get_parent(globals, buf, p_buflen) |
| 1429 | PyObject *globals; |
| 1430 | char *buf; |
| 1431 | int *p_buflen; |
| 1432 | { |
| 1433 | static PyObject *namestr = NULL; |
| 1434 | static PyObject *pathstr = NULL; |
| 1435 | PyObject *modname, *modpath, *modules, *parent; |
| 1436 | |
| 1437 | if (globals == NULL || !PyDict_Check(globals)) |
| 1438 | return Py_None; |
| 1439 | |
| 1440 | if (namestr == NULL) { |
| 1441 | namestr = PyString_InternFromString("__name__"); |
| 1442 | if (namestr == NULL) |
| 1443 | return NULL; |
| 1444 | } |
| 1445 | if (pathstr == NULL) { |
| 1446 | pathstr = PyString_InternFromString("__path__"); |
| 1447 | if (pathstr == NULL) |
| 1448 | return NULL; |
| 1449 | } |
| 1450 | |
| 1451 | *buf = '\0'; |
| 1452 | *p_buflen = 0; |
| 1453 | modname = PyDict_GetItem(globals, namestr); |
| 1454 | if (modname == NULL || !PyString_Check(modname)) |
| 1455 | return Py_None; |
| 1456 | |
| 1457 | modpath = PyDict_GetItem(globals, pathstr); |
| 1458 | if (modpath != NULL) { |
| 1459 | int len = PyString_GET_SIZE(modname); |
| 1460 | if (len > MAXPATHLEN) { |
| 1461 | PyErr_SetString(PyExc_ValueError, |
| 1462 | "Module name too long"); |
| 1463 | return NULL; |
| 1464 | } |
| 1465 | strcpy(buf, PyString_AS_STRING(modname)); |
| 1466 | *p_buflen = len; |
| 1467 | } |
| 1468 | else { |
| 1469 | char *start = PyString_AS_STRING(modname); |
| 1470 | char *lastdot = strrchr(start, '.'); |
| 1471 | int len; |
| 1472 | if (lastdot == NULL) |
| 1473 | return Py_None; |
| 1474 | len = lastdot - start; |
| 1475 | if (len >= MAXPATHLEN) { |
| 1476 | PyErr_SetString(PyExc_ValueError, |
| 1477 | "Module name too long"); |
| 1478 | return NULL; |
| 1479 | } |
| 1480 | strncpy(buf, start, len); |
| 1481 | buf[len] = '\0'; |
| 1482 | *p_buflen = len; |
| 1483 | } |
| 1484 | |
| 1485 | modules = PyImport_GetModuleDict(); |
| 1486 | parent = PyDict_GetItemString(modules, buf); |
| 1487 | if (parent == NULL) |
| 1488 | parent = Py_None; |
| 1489 | return parent; |
| 1490 | /* We expect, but can't guarantee, if parent != None, that: |
| 1491 | - parent.__name__ == buf |
| 1492 | - parent.__dict__ is globals |
| 1493 | If this is violated... Who cares? */ |
| 1494 | } |
| 1495 | |
| 1496 | static PyObject * |
| 1497 | load_next(mod, altmod, p_name, buf, p_buflen) |
| 1498 | PyObject *mod; |
| 1499 | PyObject *altmod; /* Either None or same as mod */ |
| 1500 | char **p_name; |
| 1501 | char *buf; |
| 1502 | int *p_buflen; |
| 1503 | { |
| 1504 | char *name = *p_name; |
| 1505 | char *dot = strchr(name, '.'); |
| 1506 | int len; |
| 1507 | char *p; |
| 1508 | PyObject *result; |
| 1509 | |
| 1510 | if (dot == NULL) { |
| 1511 | *p_name = NULL; |
| 1512 | len = strlen(name); |
| 1513 | } |
| 1514 | else { |
| 1515 | *p_name = dot+1; |
| 1516 | len = dot-name; |
| 1517 | } |
| 1518 | |
| 1519 | p = buf + *p_buflen; |
| 1520 | if (p != buf) |
| 1521 | *p++ = '.'; |
| 1522 | if (p+len-buf >= MAXPATHLEN) { |
| 1523 | PyErr_SetString(PyExc_ValueError, |
| 1524 | "Module name too long"); |
| 1525 | return NULL; |
| 1526 | } |
| 1527 | strncpy(p, name, len); |
| 1528 | p[len] = '\0'; |
| 1529 | *p_buflen = p+len-buf; |
| 1530 | |
| 1531 | result = import_submodule(mod, p, buf); |
| 1532 | if (result == Py_None && altmod != mod) { |
| 1533 | Py_DECREF(result); |
Guido van Rossum | f5f5fdb | 1997-09-06 20:29:52 +0000 | [diff] [blame] | 1534 | /* Here, altmod must be None and mod must not be None */ |
Guido van Rossum | 0c81945 | 1997-09-07 06:16:57 +0000 | [diff] [blame] | 1535 | result = import_submodule(altmod, p, p); |
Guido van Rossum | f5f5fdb | 1997-09-06 20:29:52 +0000 | [diff] [blame] | 1536 | if (result != NULL && result != Py_None) { |
| 1537 | if (mark_miss(buf) != 0) { |
| 1538 | Py_DECREF(result); |
| 1539 | return NULL; |
| 1540 | } |
| 1541 | strncpy(buf, name, len); |
| 1542 | buf[len] = '\0'; |
| 1543 | *p_buflen = len; |
| 1544 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1545 | } |
| 1546 | if (result == NULL) |
| 1547 | return NULL; |
| 1548 | |
| 1549 | if (result == Py_None) { |
| 1550 | Py_DECREF(result); |
| 1551 | PyErr_Format(PyExc_ImportError, |
| 1552 | "No module named %.200s", name); |
| 1553 | return NULL; |
| 1554 | } |
| 1555 | |
| 1556 | return result; |
| 1557 | } |
| 1558 | |
| 1559 | static int |
Guido van Rossum | f5f5fdb | 1997-09-06 20:29:52 +0000 | [diff] [blame] | 1560 | mark_miss(name) |
| 1561 | char *name; |
| 1562 | { |
| 1563 | PyObject *modules = PyImport_GetModuleDict(); |
| 1564 | return PyDict_SetItemString(modules, name, Py_None); |
| 1565 | } |
| 1566 | |
| 1567 | static int |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1568 | ensure_fromlist(mod, fromlist, buf, buflen, recursive) |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1569 | PyObject *mod; |
| 1570 | PyObject *fromlist; |
| 1571 | char *buf; |
| 1572 | int buflen; |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1573 | int recursive; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1574 | { |
| 1575 | int i; |
| 1576 | |
| 1577 | if (!PyObject_HasAttrString(mod, "__path__")) |
| 1578 | return 1; |
| 1579 | |
| 1580 | for (i = 0; ; i++) { |
| 1581 | PyObject *item = PySequence_GetItem(fromlist, i); |
| 1582 | int hasit; |
| 1583 | if (item == NULL) { |
| 1584 | if (PyErr_ExceptionMatches(PyExc_IndexError)) { |
| 1585 | PyErr_Clear(); |
| 1586 | return 1; |
| 1587 | } |
| 1588 | return 0; |
| 1589 | } |
| 1590 | if (!PyString_Check(item)) { |
| 1591 | PyErr_SetString(PyExc_TypeError, |
| 1592 | "Item in ``from list'' not a string"); |
| 1593 | Py_DECREF(item); |
| 1594 | return 0; |
| 1595 | } |
| 1596 | if (PyString_AS_STRING(item)[0] == '*') { |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1597 | PyObject *all; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1598 | Py_DECREF(item); |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1599 | /* See if the package defines __all__ */ |
| 1600 | if (recursive) |
| 1601 | continue; /* Avoid endless recursion */ |
| 1602 | all = PyObject_GetAttrString(mod, "__all__"); |
| 1603 | if (all == NULL) |
| 1604 | PyErr_Clear(); |
| 1605 | else { |
| 1606 | if (!ensure_fromlist(mod, all, buf, buflen, 1)) |
| 1607 | return 0; |
| 1608 | Py_DECREF(all); |
| 1609 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1610 | continue; |
| 1611 | } |
| 1612 | hasit = PyObject_HasAttr(mod, item); |
| 1613 | if (!hasit) { |
| 1614 | char *subname = PyString_AS_STRING(item); |
| 1615 | PyObject *submod; |
| 1616 | char *p; |
| 1617 | if (buflen + strlen(subname) >= MAXPATHLEN) { |
| 1618 | PyErr_SetString(PyExc_ValueError, |
| 1619 | "Module name too long"); |
| 1620 | Py_DECREF(item); |
| 1621 | return 0; |
| 1622 | } |
| 1623 | p = buf + buflen; |
| 1624 | *p++ = '.'; |
| 1625 | strcpy(p, subname); |
| 1626 | submod = import_submodule(mod, subname, buf); |
| 1627 | Py_XDECREF(submod); |
| 1628 | if (submod == NULL) { |
| 1629 | Py_DECREF(item); |
| 1630 | return 0; |
| 1631 | } |
| 1632 | } |
| 1633 | Py_DECREF(item); |
| 1634 | } |
| 1635 | |
Guido van Rossum | a7f2e81 | 1997-10-03 15:33:32 +0000 | [diff] [blame] | 1636 | /* NOTREACHED */ |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | static PyObject * |
| 1640 | import_submodule(mod, subname, fullname) |
| 1641 | PyObject *mod; /* May be None */ |
| 1642 | char *subname; |
| 1643 | char *fullname; |
| 1644 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1645 | PyObject *modules = PyImport_GetModuleDict(); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1646 | PyObject *m; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1647 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1648 | /* Require: |
| 1649 | if mod == None: subname == fullname |
| 1650 | else: mod.__name__ + "." + subname == fullname |
| 1651 | */ |
| 1652 | |
| 1653 | if ((m = PyDict_GetItemString(modules, fullname)) != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1654 | Py_INCREF(m); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1655 | } |
| 1656 | else { |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1657 | PyObject *path; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1658 | char buf[MAXPATHLEN+1]; |
| 1659 | struct filedescr *fdp; |
| 1660 | FILE *fp = NULL; |
| 1661 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1662 | path = PyObject_GetAttrString(mod, "__path__"); |
| 1663 | if (path == NULL) |
| 1664 | PyErr_Clear(); |
| 1665 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1666 | buf[0] = '\0'; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1667 | fdp = find_module(subname, path, |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1668 | buf, MAXPATHLEN+1, &fp); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1669 | if (fdp == NULL) { |
| 1670 | if (!PyErr_ExceptionMatches(PyExc_ImportError)) |
| 1671 | return NULL; |
| 1672 | PyErr_Clear(); |
| 1673 | Py_INCREF(Py_None); |
| 1674 | return Py_None; |
| 1675 | } |
| 1676 | m = load_module(fullname, fp, buf, fdp->type); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1677 | if (fp) |
| 1678 | fclose(fp); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1679 | if (m != NULL && mod != Py_None) { |
| 1680 | if (PyObject_SetAttrString(mod, subname, m) < 0) { |
| 1681 | Py_DECREF(m); |
| 1682 | m = NULL; |
| 1683 | } |
| 1684 | } |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1685 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1686 | |
| 1687 | return m; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1690 | |
| 1691 | /* Re-import a module of any kind and return its module object, WITH |
| 1692 | INCREMENTED REFERENCE COUNT */ |
| 1693 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1694 | PyObject * |
| 1695 | PyImport_ReloadModule(m) |
| 1696 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1697 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1698 | PyObject *modules = PyImport_GetModuleDict(); |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 1699 | PyObject *path = NULL; |
| 1700 | char *name, *subname; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1701 | char buf[MAXPATHLEN+1]; |
| 1702 | struct filedescr *fdp; |
| 1703 | FILE *fp = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1704 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1705 | if (m == NULL || !PyModule_Check(m)) { |
| 1706 | PyErr_SetString(PyExc_TypeError, |
| 1707 | "reload() argument must be module"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1708 | return NULL; |
| 1709 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1710 | name = PyModule_GetName(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1711 | if (name == NULL) |
| 1712 | return NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1713 | if (m != PyDict_GetItemString(modules, name)) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1714 | PyErr_Format(PyExc_ImportError, |
| 1715 | "reload(): module %.200s not in sys.modules", |
| 1716 | name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1717 | return NULL; |
| 1718 | } |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 1719 | subname = strrchr(name, '.'); |
| 1720 | if (subname == NULL) |
| 1721 | subname = name; |
| 1722 | else { |
| 1723 | PyObject *parentname, *parent; |
| 1724 | parentname = PyString_FromStringAndSize(name, (subname-name)); |
| 1725 | if (parentname == NULL) |
| 1726 | return NULL; |
| 1727 | parent = PyDict_GetItem(modules, parentname); |
| 1728 | if (parent == NULL) { |
| 1729 | PyErr_Format(PyExc_ImportError, |
| 1730 | "reload(): parent %.200s not in sys.modules", |
| 1731 | name); |
| 1732 | return NULL; |
| 1733 | } |
| 1734 | subname++; |
| 1735 | path = PyObject_GetAttrString(parent, "__path__"); |
| 1736 | if (path == NULL) |
| 1737 | PyErr_Clear(); |
| 1738 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1739 | buf[0] = '\0'; |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 1740 | fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp); |
| 1741 | Py_XDECREF(path); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1742 | if (fdp == NULL) |
| 1743 | return NULL; |
| 1744 | m = load_module(name, fp, buf, fdp->type); |
| 1745 | if (fp) |
| 1746 | fclose(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1747 | return m; |
| 1748 | } |
| 1749 | |
| 1750 | |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1751 | /* Higher-level import emulator which emulates the "import" statement |
| 1752 | more accurately -- it invokes the __import__() function from the |
| 1753 | builtins of the current globals. This means that the import is |
| 1754 | done using whatever import hooks are installed in the current |
| 1755 | environment, e.g. by "ni" or "rexec". */ |
| 1756 | |
| 1757 | PyObject * |
| 1758 | PyImport_Import(module_name) |
| 1759 | PyObject *module_name; |
| 1760 | { |
| 1761 | static PyObject *silly_list = NULL; |
| 1762 | static PyObject *builtins_str = NULL; |
| 1763 | static PyObject *import_str = NULL; |
| 1764 | static PyObject *standard_builtins = NULL; |
| 1765 | PyObject *globals = NULL; |
| 1766 | PyObject *import = NULL; |
| 1767 | PyObject *builtins = NULL; |
| 1768 | PyObject *r = NULL; |
| 1769 | |
| 1770 | /* Initialize constant string objects */ |
| 1771 | if (silly_list == NULL) { |
| 1772 | import_str = PyString_InternFromString("__import__"); |
| 1773 | if (import_str == NULL) |
| 1774 | return NULL; |
| 1775 | builtins_str = PyString_InternFromString("__builtins__"); |
| 1776 | if (builtins_str == NULL) |
| 1777 | return NULL; |
| 1778 | silly_list = Py_BuildValue("[s]", "__doc__"); |
| 1779 | if (silly_list == NULL) |
| 1780 | return NULL; |
| 1781 | } |
| 1782 | |
| 1783 | /* Get the builtins from current globals */ |
| 1784 | globals = PyEval_GetGlobals(); |
| 1785 | if(globals != NULL) { |
| 1786 | builtins = PyObject_GetItem(globals, builtins_str); |
| 1787 | if (builtins == NULL) |
| 1788 | goto err; |
| 1789 | } |
| 1790 | else { |
| 1791 | /* No globals -- use standard builtins, and fake globals */ |
| 1792 | PyErr_Clear(); |
| 1793 | |
| 1794 | if (standard_builtins == NULL) { |
| 1795 | standard_builtins = |
| 1796 | PyImport_ImportModule("__builtin__"); |
| 1797 | if (standard_builtins == NULL) |
| 1798 | return NULL; |
| 1799 | } |
| 1800 | |
| 1801 | builtins = standard_builtins; |
| 1802 | Py_INCREF(builtins); |
| 1803 | globals = Py_BuildValue("{OO}", builtins_str, builtins); |
| 1804 | if (globals == NULL) |
| 1805 | goto err; |
| 1806 | } |
| 1807 | |
| 1808 | /* Get the __import__ function from the builtins */ |
| 1809 | if (PyDict_Check(builtins)) |
| 1810 | import=PyObject_GetItem(builtins, import_str); |
| 1811 | else |
| 1812 | import=PyObject_GetAttr(builtins, import_str); |
| 1813 | if (import == NULL) |
| 1814 | goto err; |
| 1815 | |
| 1816 | /* Call the _import__ function with the proper argument list */ |
| 1817 | r = PyObject_CallFunction(import, "OOOO", |
| 1818 | module_name, globals, globals, silly_list); |
| 1819 | |
| 1820 | err: |
| 1821 | Py_XDECREF(globals); |
| 1822 | Py_XDECREF(builtins); |
| 1823 | Py_XDECREF(import); |
| 1824 | |
| 1825 | return r; |
| 1826 | } |
| 1827 | |
| 1828 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1829 | /* Module 'imp' provides Python access to the primitives used for |
| 1830 | importing modules. |
| 1831 | */ |
| 1832 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1833 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1834 | imp_get_magic(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1835 | PyObject *self; |
| 1836 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1837 | { |
| 1838 | char buf[4]; |
| 1839 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1840 | if (!PyArg_ParseTuple(args, "")) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1841 | return NULL; |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 1842 | buf[0] = (char) ((MAGIC >> 0) & 0xff); |
| 1843 | buf[1] = (char) ((MAGIC >> 8) & 0xff); |
| 1844 | buf[2] = (char) ((MAGIC >> 16) & 0xff); |
| 1845 | buf[3] = (char) ((MAGIC >> 24) & 0xff); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1846 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1847 | return PyString_FromStringAndSize(buf, 4); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1850 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1851 | imp_get_suffixes(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1852 | PyObject *self; |
| 1853 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1854 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1855 | PyObject *list; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1856 | struct filedescr *fdp; |
| 1857 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1858 | if (!PyArg_ParseTuple(args, "")) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1859 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1860 | list = PyList_New(0); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1861 | if (list == NULL) |
| 1862 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1863 | for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { |
| 1864 | PyObject *item = Py_BuildValue("ssi", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1865 | fdp->suffix, fdp->mode, fdp->type); |
| 1866 | if (item == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1867 | Py_DECREF(list); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1868 | return NULL; |
| 1869 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1870 | if (PyList_Append(list, item) < 0) { |
| 1871 | Py_DECREF(list); |
| 1872 | Py_DECREF(item); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1873 | return NULL; |
| 1874 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1875 | Py_DECREF(item); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1876 | } |
| 1877 | return list; |
| 1878 | } |
| 1879 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1880 | static PyObject * |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1881 | call_find_module(name, path) |
| 1882 | char *name; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1883 | PyObject *path; /* list or None or NULL */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1884 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1885 | extern int fclose Py_PROTO((FILE *)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1886 | PyObject *fob, *ret; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1887 | struct filedescr *fdp; |
| 1888 | char pathname[MAXPATHLEN+1]; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1889 | FILE *fp = NULL; |
| 1890 | |
| 1891 | pathname[0] = '\0'; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1892 | if (path == Py_None) |
| 1893 | path = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1894 | fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp); |
| 1895 | if (fdp == NULL) |
| 1896 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1897 | if (fp != NULL) { |
| 1898 | fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose); |
| 1899 | if (fob == NULL) { |
| 1900 | fclose(fp); |
| 1901 | return NULL; |
| 1902 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1903 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1904 | else { |
| 1905 | fob = Py_None; |
| 1906 | Py_INCREF(fob); |
| 1907 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1908 | ret = Py_BuildValue("Os(ssi)", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1909 | fob, pathname, fdp->suffix, fdp->mode, fdp->type); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1910 | Py_DECREF(fob); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1911 | return ret; |
| 1912 | } |
| 1913 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1914 | static PyObject * |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1915 | imp_find_module(self, args) |
| 1916 | PyObject *self; |
| 1917 | PyObject *args; |
| 1918 | { |
| 1919 | char *name; |
| 1920 | PyObject *path = NULL; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1921 | if (!PyArg_ParseTuple(args, "s|O", &name, &path)) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1922 | return NULL; |
| 1923 | return call_find_module(name, path); |
| 1924 | } |
| 1925 | |
| 1926 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1927 | imp_init_builtin(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1928 | PyObject *self; |
| 1929 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1930 | { |
| 1931 | char *name; |
| 1932 | int ret; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1933 | PyObject *m; |
| 1934 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1935 | return NULL; |
| 1936 | ret = init_builtin(name); |
| 1937 | if (ret < 0) |
| 1938 | return NULL; |
| 1939 | if (ret == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1940 | Py_INCREF(Py_None); |
| 1941 | return Py_None; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1942 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1943 | m = PyImport_AddModule(name); |
| 1944 | Py_XINCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1945 | return m; |
| 1946 | } |
| 1947 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1948 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1949 | imp_init_frozen(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1950 | PyObject *self; |
| 1951 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1952 | { |
| 1953 | char *name; |
| 1954 | int ret; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1955 | PyObject *m; |
| 1956 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1957 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1958 | ret = PyImport_ImportFrozenModule(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1959 | if (ret < 0) |
| 1960 | return NULL; |
| 1961 | if (ret == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1962 | Py_INCREF(Py_None); |
| 1963 | return Py_None; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1964 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1965 | m = PyImport_AddModule(name); |
| 1966 | Py_XINCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1967 | return m; |
| 1968 | } |
| 1969 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1970 | static PyObject * |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1971 | imp_get_frozen_object(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1972 | PyObject *self; |
| 1973 | PyObject *args; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1974 | { |
| 1975 | char *name; |
Jack Jansen | 95ffa23 | 1995-10-03 14:38:41 +0000 | [diff] [blame] | 1976 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1977 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1978 | return NULL; |
| 1979 | return get_frozen_object(name); |
| 1980 | } |
| 1981 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1982 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1983 | imp_is_builtin(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1984 | PyObject *self; |
| 1985 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1986 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1987 | char *name; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1988 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1989 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1990 | return PyInt_FromLong(is_builtin(name)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1993 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1994 | imp_is_frozen(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1995 | PyObject *self; |
| 1996 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1997 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1998 | char *name; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1999 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2000 | return NULL; |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 2001 | return PyInt_FromLong(find_frozen(name) != NULL); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | static FILE * |
| 2005 | get_file(pathname, fob, mode) |
| 2006 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2007 | PyObject *fob; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2008 | char *mode; |
| 2009 | { |
| 2010 | FILE *fp; |
| 2011 | if (fob == NULL) { |
| 2012 | fp = fopen(pathname, mode); |
| 2013 | if (fp == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2014 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2015 | } |
| 2016 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2017 | fp = PyFile_AsFile(fob); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2018 | if (fp == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2019 | PyErr_SetString(PyExc_ValueError, |
| 2020 | "bad/closed file object"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2021 | } |
| 2022 | return fp; |
| 2023 | } |
| 2024 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2025 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2026 | imp_load_compiled(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2027 | PyObject *self; |
| 2028 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2029 | { |
| 2030 | char *name; |
| 2031 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2032 | PyObject *fob = NULL; |
| 2033 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2034 | FILE *fp; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2035 | if (!PyArg_ParseTuple(args, "ss|O!", &name, &pathname, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2036 | &PyFile_Type, &fob)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2037 | return NULL; |
| 2038 | fp = get_file(pathname, fob, "rb"); |
| 2039 | if (fp == NULL) |
| 2040 | return NULL; |
| 2041 | m = load_compiled_module(name, pathname, fp); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2042 | if (fob == NULL) |
| 2043 | fclose(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2044 | return m; |
| 2045 | } |
| 2046 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2047 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2048 | imp_load_dynamic(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2049 | PyObject *self; |
| 2050 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2051 | { |
| 2052 | char *name; |
| 2053 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2054 | PyObject *fob = NULL; |
| 2055 | PyObject *m; |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 2056 | FILE *fp = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2057 | if (!PyArg_ParseTuple(args, "ss|O!", &name, &pathname, |
| 2058 | &PyFile_Type, &fob)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2059 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2060 | if (fob) { |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 2061 | fp = get_file(pathname, fob, "r"); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2062 | if (fp == NULL) |
| 2063 | return NULL; |
| 2064 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2065 | m = _PyImport_LoadDynamicModule(name, pathname, fp); |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 2066 | return m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2069 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2070 | imp_load_source(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2071 | PyObject *self; |
| 2072 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2073 | { |
| 2074 | char *name; |
| 2075 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2076 | PyObject *fob = NULL; |
| 2077 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2078 | FILE *fp; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2079 | if (!PyArg_ParseTuple(args, "ss|O!", &name, &pathname, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2080 | &PyFile_Type, &fob)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2081 | return NULL; |
| 2082 | fp = get_file(pathname, fob, "r"); |
| 2083 | if (fp == NULL) |
| 2084 | return NULL; |
| 2085 | m = load_source_module(name, pathname, fp); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2086 | if (fob == NULL) |
| 2087 | fclose(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2088 | return m; |
| 2089 | } |
| 2090 | |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 2091 | #ifdef macintosh |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2092 | static PyObject * |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 2093 | imp_load_resource(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2094 | PyObject *self; |
| 2095 | PyObject *args; |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 2096 | { |
| 2097 | char *name; |
| 2098 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2099 | PyObject *m; |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 2100 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2101 | if (!PyArg_ParseTuple(args, "ss", &name, &pathname)) |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 2102 | return NULL; |
| 2103 | m = PyMac_LoadResourceModule(name, pathname); |
| 2104 | return m; |
| 2105 | } |
| 2106 | #endif /* macintosh */ |
| 2107 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2108 | static PyObject * |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2109 | imp_load_module(self, args) |
| 2110 | PyObject *self; |
| 2111 | PyObject *args; |
| 2112 | { |
| 2113 | char *name; |
| 2114 | PyObject *fob; |
| 2115 | char *pathname; |
| 2116 | char *suffix; /* Unused */ |
| 2117 | char *mode; |
| 2118 | int type; |
| 2119 | FILE *fp; |
| 2120 | |
| 2121 | if (!PyArg_ParseTuple(args, "sOs(ssi)", |
| 2122 | &name, &fob, &pathname, |
| 2123 | &suffix, &mode, &type)) |
| 2124 | return NULL; |
| 2125 | if (*mode && (*mode != 'r' || strchr(mode, '+') != NULL)) { |
| 2126 | PyErr_Format(PyExc_ValueError, |
| 2127 | "invalid file open mode %.200s", mode); |
| 2128 | return NULL; |
| 2129 | } |
| 2130 | if (fob == Py_None) |
| 2131 | fp = NULL; |
| 2132 | else { |
| 2133 | if (!PyFile_Check(fob)) { |
| 2134 | PyErr_SetString(PyExc_ValueError, |
| 2135 | "load_module arg#2 should be a file or None"); |
| 2136 | return NULL; |
| 2137 | } |
| 2138 | fp = get_file(pathname, fob, mode); |
| 2139 | if (fp == NULL) |
| 2140 | return NULL; |
| 2141 | } |
| 2142 | return load_module(name, fp, pathname, type); |
| 2143 | } |
| 2144 | |
| 2145 | static PyObject * |
| 2146 | imp_load_package(self, args) |
| 2147 | PyObject *self; |
| 2148 | PyObject *args; |
| 2149 | { |
| 2150 | char *name; |
| 2151 | char *pathname; |
| 2152 | if (!PyArg_ParseTuple(args, "ss", &name, &pathname)) |
| 2153 | return NULL; |
| 2154 | return load_package(name, pathname); |
| 2155 | } |
| 2156 | |
| 2157 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2158 | imp_new_module(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2159 | PyObject *self; |
| 2160 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2161 | { |
| 2162 | char *name; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2163 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2164 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2165 | return PyModule_New(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2168 | /* Doc strings */ |
| 2169 | |
| 2170 | static char doc_imp[] = "\ |
| 2171 | This module provides the components needed to build your own\n\ |
| 2172 | __import__ function. Undocumented functions are obsolete.\n\ |
| 2173 | "; |
| 2174 | |
| 2175 | static char doc_find_module[] = "\ |
| 2176 | find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\ |
| 2177 | Search for a module. If path is omitted or None, search for a\n\ |
| 2178 | built-in, frozen or special module and continue search in sys.path.\n\ |
| 2179 | The module name cannot contain '.'; to search for a submodule of a\n\ |
| 2180 | package, pass the submodule name and the package's __path__.\ |
| 2181 | "; |
| 2182 | |
| 2183 | static char doc_load_module[] = "\ |
| 2184 | load_module(name, file, filename, (suffix, mode, type)) -> module\n\ |
| 2185 | Load a module, given information returned by find_module().\n\ |
| 2186 | The module name must include the full package name, if any.\ |
| 2187 | "; |
| 2188 | |
| 2189 | static char doc_get_magic[] = "\ |
| 2190 | get_magic() -> string\n\ |
| 2191 | Return the magic number for .pyc or .pyo files.\ |
| 2192 | "; |
| 2193 | |
| 2194 | static char doc_get_suffixes[] = "\ |
| 2195 | get_suffixes() -> [(suffix, mode, type), ...]\n\ |
| 2196 | Return a list of (suffix, mode, type) tuples describing the files\n\ |
| 2197 | that find_module() looks for.\ |
| 2198 | "; |
| 2199 | |
| 2200 | static char doc_new_module[] = "\ |
| 2201 | new_module(name) -> module\n\ |
| 2202 | Create a new module. Do not enter it in sys.modules.\n\ |
| 2203 | The module name must include the full package name, if any.\ |
| 2204 | "; |
| 2205 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2206 | static PyMethodDef imp_methods[] = { |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2207 | {"find_module", imp_find_module, 1, doc_find_module}, |
| 2208 | {"get_magic", imp_get_magic, 1, doc_get_magic}, |
| 2209 | {"get_suffixes", imp_get_suffixes, 1, doc_get_suffixes}, |
| 2210 | {"load_module", imp_load_module, 1, doc_load_module}, |
| 2211 | {"new_module", imp_new_module, 1, doc_new_module}, |
| 2212 | /* The rest are obsolete */ |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2213 | {"get_frozen_object", imp_get_frozen_object, 1}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2214 | {"init_builtin", imp_init_builtin, 1}, |
| 2215 | {"init_frozen", imp_init_frozen, 1}, |
| 2216 | {"is_builtin", imp_is_builtin, 1}, |
| 2217 | {"is_frozen", imp_is_frozen, 1}, |
| 2218 | {"load_compiled", imp_load_compiled, 1}, |
| 2219 | {"load_dynamic", imp_load_dynamic, 1}, |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2220 | {"load_package", imp_load_package, 1}, |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 2221 | #ifdef macintosh |
| 2222 | {"load_resource", imp_load_resource, 1}, |
| 2223 | #endif |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2224 | {"load_source", imp_load_source, 1}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2225 | {NULL, NULL} /* sentinel */ |
| 2226 | }; |
| 2227 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2228 | int |
| 2229 | setint(d, name, value) |
| 2230 | PyObject *d; |
| 2231 | char *name; |
| 2232 | int value; |
| 2233 | { |
| 2234 | PyObject *v; |
| 2235 | int err; |
| 2236 | |
| 2237 | v = PyInt_FromLong((long)value); |
| 2238 | err = PyDict_SetItemString(d, name, v); |
| 2239 | Py_XDECREF(v); |
| 2240 | return err; |
| 2241 | } |
| 2242 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2243 | void |
| 2244 | initimp() |
| 2245 | { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2246 | PyObject *m, *d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2247 | |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2248 | m = Py_InitModule4("imp", imp_methods, doc_imp, |
| 2249 | NULL, PYTHON_API_VERSION); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2250 | d = PyModule_GetDict(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2251 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2252 | if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure; |
| 2253 | if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure; |
| 2254 | if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure; |
| 2255 | if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure; |
| 2256 | if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure; |
| 2257 | if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure; |
| 2258 | if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure; |
| 2259 | if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2260 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2261 | failure: |
| 2262 | ; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2263 | } |