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 | |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 122 | #include "pythread.h" |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 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) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 223 | PySys_WriteStderr("# clear __builtin__._\n"); |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 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) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 233 | PySys_WriteStderr("# clear sys.%s\n", *p); |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 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) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 238 | PySys_WriteStderr("# restore sys.%s\n", *p); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 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) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 250 | PySys_WriteStderr("# cleanup __main__\n"); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 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 | 566373e | 1998-10-01 15:24:50 +0000 | [diff] [blame] | 277 | if (PyString_Check(key) && PyModule_Check(value)) { |
| 278 | name = PyString_AS_STRING(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) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 284 | PySys_WriteStderr( |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 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)) { |
Guido van Rossum | 566373e | 1998-10-01 15:24:50 +0000 | [diff] [blame] | 296 | if (PyString_Check(key) && PyModule_Check(value)) { |
| 297 | name = PyString_AS_STRING(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) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 303 | PySys_WriteStderr("# 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) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 313 | PySys_WriteStderr("# 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) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 320 | PySys_WriteStderr("# 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) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 402 | PySys_WriteStderr("import %s # previously loaded (%s)\n", |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 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 | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 545 | PySys_WriteStderr("# %s has bad magic\n", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 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 | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 552 | PySys_WriteStderr("# %s has bad mtime\n", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 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 | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 557 | PySys_WriteStderr("# %s matches %s\n", cpathname, pathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 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 | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 608 | PySys_WriteStderr("import %s # precompiled from %s\n", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 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 | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 652 | PySys_WriteStderr( |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 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 | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 662 | PySys_WriteStderr("# can't write %s\n", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 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 | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 674 | PySys_WriteStderr("# wrote %s\n", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 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 | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 707 | PySys_WriteStderr("import %s # precompiled from %s\n", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 708 | name, cpathname); |
Guido van Rossum | afd3dae | 1998-08-25 18:44:34 +0000 | [diff] [blame] | 709 | pathname = cpathname; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 710 | } |
| 711 | else { |
| 712 | co = parse_source_module(pathname, fp); |
| 713 | if (co == NULL) |
| 714 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 715 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 716 | PySys_WriteStderr("import %s # from %s\n", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 717 | name, pathname); |
| 718 | write_compiled_module(co, cpathname, mtime); |
| 719 | } |
Guido van Rossum | afd3dae | 1998-08-25 18:44:34 +0000 | [diff] [blame] | 720 | m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 721 | Py_DECREF(co); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 722 | |
| 723 | return m; |
| 724 | } |
| 725 | |
| 726 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 727 | /* Forward */ |
| 728 | static PyObject *load_module Py_PROTO((char *, FILE *, char *, int)); |
| 729 | static struct filedescr *find_module Py_PROTO((char *, PyObject *, |
| 730 | char *, int, FILE **)); |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 731 | static struct _frozen *find_frozen Py_PROTO((char *name)); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 732 | |
| 733 | /* Load a package and return its module object WITH INCREMENTED |
| 734 | REFERENCE COUNT */ |
| 735 | |
| 736 | static PyObject * |
| 737 | load_package(name, pathname) |
| 738 | char *name; |
| 739 | char *pathname; |
| 740 | { |
| 741 | PyObject *m, *d, *file, *path; |
| 742 | int err; |
| 743 | char buf[MAXPATHLEN+1]; |
| 744 | FILE *fp = NULL; |
| 745 | struct filedescr *fdp; |
| 746 | |
| 747 | m = PyImport_AddModule(name); |
| 748 | if (m == NULL) |
| 749 | return NULL; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 750 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 751 | PySys_WriteStderr("import %s # directory %s\n", |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 752 | name, pathname); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 753 | d = PyModule_GetDict(m); |
| 754 | file = PyString_FromString(pathname); |
| 755 | if (file == NULL) |
| 756 | return NULL; |
| 757 | path = Py_BuildValue("[O]", file); |
| 758 | if (path == NULL) { |
| 759 | Py_DECREF(file); |
| 760 | return NULL; |
| 761 | } |
| 762 | err = PyDict_SetItemString(d, "__file__", file); |
| 763 | if (err == 0) |
| 764 | err = PyDict_SetItemString(d, "__path__", path); |
| 765 | if (err != 0) { |
| 766 | m = NULL; |
| 767 | goto cleanup; |
| 768 | } |
| 769 | buf[0] = '\0'; |
| 770 | fdp = find_module("__init__", path, buf, sizeof(buf), &fp); |
| 771 | if (fdp == NULL) { |
| 772 | if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 773 | PyErr_Clear(); |
| 774 | } |
| 775 | else |
| 776 | m = NULL; |
| 777 | goto cleanup; |
| 778 | } |
| 779 | m = load_module(name, fp, buf, fdp->type); |
| 780 | if (fp != NULL) |
| 781 | fclose(fp); |
| 782 | cleanup: |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 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 * |
Guido van Rossum | 0506a43 | 1998-08-11 15:07:39 +0000 | [diff] [blame] | 823 | find_module(realname, path, buf, buflen, p_fp) |
| 824 | char *realname; |
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 | 0506a43 | 1998-08-11 15:07:39 +0000 | [diff] [blame] | 839 | char name[MAXPATHLEN+1]; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 840 | |
Guido van Rossum | 0506a43 | 1998-08-11 15:07:39 +0000 | [diff] [blame] | 841 | strcpy(name, realname); |
| 842 | |
| 843 | if (path != NULL && PyString_Check(path)) { |
| 844 | /* Submodule of "frozen" package: |
| 845 | Set name to the fullname, path to NULL |
| 846 | and continue as "usual" */ |
| 847 | if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) { |
| 848 | PyErr_SetString(PyExc_ImportError, |
| 849 | "full frozen module name too long"); |
| 850 | return NULL; |
| 851 | } |
| 852 | strcpy(buf, PyString_AsString(path)); |
| 853 | strcat(buf, "."); |
| 854 | strcat(buf, name); |
| 855 | strcpy(name, buf); |
| 856 | path = NULL; |
| 857 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 858 | if (path == NULL) { |
| 859 | if (is_builtin(name)) { |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 860 | strcpy(buf, name); |
| 861 | return &fd_builtin; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 862 | } |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 863 | if ((f = find_frozen(name)) != NULL) { |
| 864 | strcpy(buf, name); |
| 865 | return &fd_frozen; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 866 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 867 | |
Guido van Rossum | ac27910 | 1996-08-22 23:10:58 +0000 | [diff] [blame] | 868 | #ifdef MS_COREDLL |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 869 | fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen); |
| 870 | if (fp != NULL) { |
| 871 | *p_fp = fp; |
| 872 | return fdp; |
| 873 | } |
Guido van Rossum | a5a3db7 | 1996-04-09 02:39:59 +0000 | [diff] [blame] | 874 | #endif |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 875 | path = PySys_GetObject("path"); |
| 876 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 877 | if (path == NULL || !PyList_Check(path)) { |
| 878 | PyErr_SetString(PyExc_ImportError, |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 879 | "sys.path must be a list of directory names"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 880 | return NULL; |
| 881 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 882 | npath = PyList_Size(path); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 883 | namelen = strlen(name); |
| 884 | for (i = 0; i < npath; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 885 | PyObject *v = PyList_GetItem(path, i); |
| 886 | if (!PyString_Check(v)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 887 | continue; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 888 | len = PyString_Size(v); |
Guido van Rossum | ef3d02e | 1997-07-21 14:54:36 +0000 | [diff] [blame] | 889 | if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 890 | continue; /* Too long */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 891 | strcpy(buf, PyString_AsString(v)); |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 892 | if ((int)strlen(buf) != len) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 893 | continue; /* v contains '\0' */ |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 894 | #ifdef macintosh |
Guido van Rossum | 741689d | 1997-08-12 14:53:39 +0000 | [diff] [blame] | 895 | #ifdef INTERN_STRINGS |
| 896 | /* |
| 897 | ** Speedup: each sys.path item is interned, and |
| 898 | ** FindResourceModule remembers which items refer to |
| 899 | ** folders (so we don't have to bother trying to look |
| 900 | ** into them for resources). |
| 901 | */ |
| 902 | PyString_InternInPlace(&PyList_GET_ITEM(path, i)); |
| 903 | v = PyList_GET_ITEM(path, i); |
| 904 | #endif |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 905 | if (PyMac_FindResourceModule((PyStringObject *)v, name, buf)) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 906 | static struct filedescr resfiledescr = |
| 907 | {"", "", PY_RESOURCE}; |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 908 | |
| 909 | return &resfiledescr; |
| 910 | } |
Guido van Rossum | 0f84a34 | 1998-08-06 13:36:01 +0000 | [diff] [blame] | 911 | if (PyMac_FindCodeResourceModule((PyStringObject *)v, name, buf)) { |
| 912 | static struct filedescr resfiledescr = |
| 913 | {"", "", PY_CODERESOURCE}; |
| 914 | |
| 915 | return &resfiledescr; |
| 916 | } |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 917 | #endif |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 918 | if (len > 0 && buf[len-1] != SEP |
| 919 | #ifdef ALTSEP |
| 920 | && buf[len-1] != ALTSEP |
| 921 | #endif |
| 922 | ) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 923 | buf[len++] = SEP; |
Guido van Rossum | 40f470f | 1996-05-23 22:51:04 +0000 | [diff] [blame] | 924 | #ifdef IMPORT_8x3_NAMES |
Guido van Rossum | e8c27bb | 1997-12-17 18:57:16 +0000 | [diff] [blame] | 925 | /* see if we are searching in directory dos-8x3 */ |
| 926 | if (len > 7 && !strncmp(buf + len - 8, "dos-8x3", 7)){ |
Guido van Rossum | 40f470f | 1996-05-23 22:51:04 +0000 | [diff] [blame] | 927 | int j; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 928 | char ch; /* limit name to 8 lower-case characters */ |
Guido van Rossum | 40f470f | 1996-05-23 22:51:04 +0000 | [diff] [blame] | 929 | for (j = 0; (ch = name[j]) && j < 8; j++) |
| 930 | if (isupper(ch)) |
| 931 | buf[len++] = tolower(ch); |
| 932 | else |
| 933 | buf[len++] = ch; |
| 934 | } |
Guido van Rossum | e8c27bb | 1997-12-17 18:57:16 +0000 | [diff] [blame] | 935 | else /* Not in dos-8x3, use the full name */ |
Guido van Rossum | 6f489d9 | 1996-06-28 20:15:15 +0000 | [diff] [blame] | 936 | #endif |
Guido van Rossum | 0e41c8c | 1996-06-20 14:18:34 +0000 | [diff] [blame] | 937 | { |
Guido van Rossum | 40f470f | 1996-05-23 22:51:04 +0000 | [diff] [blame] | 938 | strcpy(buf+len, name); |
| 939 | len += namelen; |
| 940 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 941 | #ifdef HAVE_STAT |
| 942 | if (stat(buf, &statbuf) == 0) { |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 943 | if (S_ISDIR(statbuf.st_mode)) { |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 944 | if (find_init_module(buf)) { |
| 945 | #ifdef CHECK_IMPORT_CASE |
| 946 | if (!check_case(buf, len, namelen, |
| 947 | name)) |
| 948 | return NULL; |
| 949 | #endif |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 950 | return &fd_package; |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 951 | } |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 952 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 953 | } |
| 954 | #else |
| 955 | /* XXX How are you going to test for directories? */ |
| 956 | #endif |
Guido van Rossum | 9a61dc9 | 1997-10-08 15:25:08 +0000 | [diff] [blame] | 957 | #ifdef macintosh |
| 958 | fdp = PyMac_FindModuleExtension(buf, &len, name); |
| 959 | if (fdp) |
| 960 | fp = fopen(buf, fdp->mode); |
| 961 | #else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 962 | for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 963 | strcpy(buf+len, fdp->suffix); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 964 | if (Py_VerboseFlag > 1) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 965 | PySys_WriteStderr("# trying %s\n", buf); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 966 | fp = fopen(buf, fdp->mode); |
| 967 | if (fp != NULL) |
| 968 | break; |
| 969 | } |
Guido van Rossum | 741689d | 1997-08-12 14:53:39 +0000 | [diff] [blame] | 970 | #endif /* !macintosh */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 971 | if (fp != NULL) |
| 972 | break; |
| 973 | } |
| 974 | if (fp == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 975 | PyErr_Format(PyExc_ImportError, |
| 976 | "No module named %.200s", name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 977 | return NULL; |
| 978 | } |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 979 | #ifdef CHECK_IMPORT_CASE |
| 980 | if (!check_case(buf, len, namelen, name)) { |
| 981 | fclose(fp); |
| 982 | return NULL; |
| 983 | } |
| 984 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 985 | |
| 986 | *p_fp = fp; |
| 987 | return fdp; |
| 988 | } |
| 989 | |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 990 | #ifdef CHECK_IMPORT_CASE |
| 991 | |
| 992 | #ifdef MS_WIN32 |
| 993 | #include <windows.h> |
Guido van Rossum | 323bf5e | 1998-06-24 03:54:06 +0000 | [diff] [blame] | 994 | #include <ctype.h> |
| 995 | |
| 996 | static int |
| 997 | allcaps8x3(s) |
| 998 | char *s; |
| 999 | { |
| 1000 | /* Return 1 if s is an 8.3 filename in ALLCAPS */ |
| 1001 | char c; |
| 1002 | char *dot = strchr(s, '.'); |
| 1003 | char *end = strchr(s, '\0'); |
| 1004 | if (dot != NULL) { |
| 1005 | if (dot-s > 8) |
| 1006 | return 1; /* More than 8 before '.' */ |
| 1007 | if (end-dot > 4) |
| 1008 | return 1; /* More than 3 after '.' */ |
| 1009 | end = strchr(dot+1, '.'); |
| 1010 | if (end != NULL) |
| 1011 | return 1; /* More than one dot */ |
| 1012 | } |
| 1013 | else if (end-s > 8) |
| 1014 | return 1; /* More than 8 and no dot */ |
| 1015 | while ((c = *s++)) { |
| 1016 | if (islower(c)) |
| 1017 | return 0; |
| 1018 | } |
| 1019 | return 1; |
| 1020 | } |
| 1021 | |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1022 | static int |
| 1023 | check_case(char *buf, int len, int namelen, char *name) |
| 1024 | { |
| 1025 | WIN32_FIND_DATA data; |
| 1026 | HANDLE h; |
| 1027 | if (getenv("PYTHONCASEOK") != NULL) |
| 1028 | return 1; |
| 1029 | h = FindFirstFile(buf, &data); |
| 1030 | if (h == INVALID_HANDLE_VALUE) { |
| 1031 | PyErr_Format(PyExc_NameError, |
| 1032 | "Can't find file for module %.100s\n(filename %.300s)", |
| 1033 | name, buf); |
| 1034 | return 0; |
| 1035 | } |
| 1036 | FindClose(h); |
Guido van Rossum | 323bf5e | 1998-06-24 03:54:06 +0000 | [diff] [blame] | 1037 | if (allcaps8x3(data.cFileName)) { |
| 1038 | /* Skip the test if the filename is ALL.CAPS. This can |
| 1039 | happen in certain circumstances beyond our control, |
| 1040 | e.g. when software is installed under NT on a FAT |
| 1041 | filesystem and then the same FAT filesystem is used |
| 1042 | under Windows 95. */ |
| 1043 | return 1; |
| 1044 | } |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1045 | if (strncmp(data.cFileName, name, namelen) != 0) { |
| 1046 | strcpy(buf+len-namelen, data.cFileName); |
| 1047 | PyErr_Format(PyExc_NameError, |
| 1048 | "Case mismatch for module name %.100s\n(filename %.300s)", |
| 1049 | name, buf); |
| 1050 | return 0; |
| 1051 | } |
| 1052 | return 1; |
| 1053 | } |
| 1054 | #endif /* MS_WIN32 */ |
| 1055 | |
| 1056 | #ifdef macintosh |
| 1057 | #include <TextUtils.h> |
Guido van Rossum | a0f0a33 | 1998-09-14 13:40:53 +0000 | [diff] [blame] | 1058 | #ifdef USE_GUSI |
| 1059 | #include "TFileSpec.h" /* for Path2FSSpec() */ |
| 1060 | #endif |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1061 | static int |
| 1062 | check_case(char *buf, int len, int namelen, char *name) |
| 1063 | { |
| 1064 | FSSpec fss; |
| 1065 | OSErr err; |
Guido van Rossum | a0f0a33 | 1998-09-14 13:40:53 +0000 | [diff] [blame] | 1066 | #ifndef USE_GUSI |
| 1067 | err = FSMakeFSSpec(0, 0, Pstring(buf), &fss); |
| 1068 | #else |
| 1069 | /* GUSI's Path2FSSpec() resolves all possible aliases nicely on |
| 1070 | the way, which is fine for all directories, but here we need |
| 1071 | the original name of the alias file (say, Dlg.ppc.slb, not |
| 1072 | toolboxmodules.ppc.slb). */ |
| 1073 | char *colon; |
| 1074 | err = Path2FSSpec(buf, &fss); |
| 1075 | if (err == noErr) { |
| 1076 | colon = strrchr(buf, ':'); /* find filename */ |
| 1077 | if (colon != NULL) |
| 1078 | err = FSMakeFSSpec(fss.vRefNum, fss.parID, |
| 1079 | Pstring(colon+1), &fss); |
| 1080 | else |
| 1081 | err = FSMakeFSSpec(fss.vRefNum, fss.parID, |
| 1082 | fss.name, &fss); |
| 1083 | } |
| 1084 | #endif |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1085 | if (err) { |
| 1086 | PyErr_Format(PyExc_NameError, |
Guido van Rossum | a0f0a33 | 1998-09-14 13:40:53 +0000 | [diff] [blame] | 1087 | "Can't find file for module %.100s\n(filename %.300s)", |
| 1088 | name, buf); |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1089 | return 0; |
| 1090 | } |
| 1091 | p2cstr(fss.name); |
| 1092 | if ( strncmp(name, (char *)fss.name, namelen) != 0 ) { |
| 1093 | PyErr_Format(PyExc_NameError, |
Guido van Rossum | a0f0a33 | 1998-09-14 13:40:53 +0000 | [diff] [blame] | 1094 | "Case mismatch for module name %.100s\n(filename %.300s)", |
| 1095 | name, fss.name); |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1096 | return 0; |
| 1097 | } |
| 1098 | return 1; |
| 1099 | } |
| 1100 | #endif /* macintosh */ |
| 1101 | |
Guido van Rossum | 4d1b3b9 | 1998-02-13 23:27:59 +0000 | [diff] [blame] | 1102 | #ifdef DJGPP |
Guido van Rossum | aeca79b | 1998-02-18 16:21:00 +0000 | [diff] [blame] | 1103 | #include <dir.h> |
| 1104 | |
Guido van Rossum | 4d1b3b9 | 1998-02-13 23:27:59 +0000 | [diff] [blame] | 1105 | static int |
| 1106 | check_case(char *buf, int len, int namelen, char *name) |
| 1107 | { |
| 1108 | struct ffblk ffblk; |
| 1109 | int done; |
| 1110 | |
| 1111 | if (getenv("PYTHONCASEOK") != NULL) |
| 1112 | return 1; |
Guido van Rossum | aeca79b | 1998-02-18 16:21:00 +0000 | [diff] [blame] | 1113 | 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] | 1114 | if (done) { |
| 1115 | PyErr_Format(PyExc_NameError, |
| 1116 | "Can't find file for module %.100s\n(filename %.300s)", |
| 1117 | name, buf); |
| 1118 | return 0; |
| 1119 | } |
| 1120 | |
| 1121 | if (strncmp(ffblk.ff_name, name, namelen) != 0) { |
| 1122 | strcpy(buf+len-namelen, ffblk.ff_name); |
| 1123 | PyErr_Format(PyExc_NameError, |
| 1124 | "Case mismatch for module name %.100s\n(filename %.300s)", |
| 1125 | name, buf); |
| 1126 | return 0; |
| 1127 | } |
| 1128 | return 1; |
| 1129 | } |
| 1130 | #endif |
| 1131 | |
Guido van Rossum | 8766a77 | 1998-04-10 23:44:32 +0000 | [diff] [blame] | 1132 | #endif /* CHECK_IMPORT_CASE */ |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1133 | |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1134 | #ifdef HAVE_STAT |
| 1135 | /* Helper to look for __init__.py or __init__.py[co] in potential package */ |
| 1136 | static int |
| 1137 | find_init_module(buf) |
| 1138 | char *buf; |
| 1139 | { |
| 1140 | int save_len = strlen(buf); |
| 1141 | int i = save_len; |
| 1142 | struct stat statbuf; |
| 1143 | |
| 1144 | if (save_len + 13 >= MAXPATHLEN) |
| 1145 | return 0; |
| 1146 | buf[i++] = SEP; |
| 1147 | strcpy(buf+i, "__init__.py"); |
| 1148 | if (stat(buf, &statbuf) == 0) { |
| 1149 | buf[save_len] = '\0'; |
| 1150 | return 1; |
| 1151 | } |
| 1152 | i += strlen(buf+i); |
| 1153 | if (Py_OptimizeFlag) |
| 1154 | strcpy(buf+i, "o"); |
| 1155 | else |
| 1156 | strcpy(buf+i, "c"); |
| 1157 | if (stat(buf, &statbuf) == 0) { |
| 1158 | buf[save_len] = '\0'; |
| 1159 | return 1; |
| 1160 | } |
| 1161 | buf[save_len] = '\0'; |
| 1162 | return 0; |
| 1163 | } |
| 1164 | #endif /* HAVE_STAT */ |
| 1165 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1166 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1167 | static int init_builtin Py_PROTO((char *)); /* Forward */ |
| 1168 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1169 | /* Load an external module using the default search path and return |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1170 | its module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1171 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1172 | static PyObject * |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1173 | load_module(name, fp, buf, type) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1174 | char *name; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1175 | FILE *fp; |
| 1176 | char *buf; |
| 1177 | int type; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1178 | { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1179 | PyObject *modules; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1180 | PyObject *m; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1181 | int err; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1182 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1183 | /* First check that there's an open file (if we need one) */ |
| 1184 | switch (type) { |
| 1185 | case PY_SOURCE: |
| 1186 | case PY_COMPILED: |
| 1187 | if (fp == NULL) { |
| 1188 | PyErr_Format(PyExc_ValueError, |
| 1189 | "file object required for import (type code %d)", |
| 1190 | type); |
| 1191 | return NULL; |
| 1192 | } |
| 1193 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1194 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1195 | switch (type) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1196 | |
| 1197 | case PY_SOURCE: |
| 1198 | m = load_source_module(name, buf, fp); |
| 1199 | break; |
| 1200 | |
| 1201 | case PY_COMPILED: |
| 1202 | m = load_compiled_module(name, buf, fp); |
| 1203 | break; |
| 1204 | |
| 1205 | case C_EXTENSION: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1206 | m = _PyImport_LoadDynamicModule(name, buf, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1207 | break; |
| 1208 | |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 1209 | #ifdef macintosh |
| 1210 | case PY_RESOURCE: |
| 1211 | m = PyMac_LoadResourceModule(name, buf); |
| 1212 | break; |
Guido van Rossum | 0f84a34 | 1998-08-06 13:36:01 +0000 | [diff] [blame] | 1213 | case PY_CODERESOURCE: |
| 1214 | m = PyMac_LoadCodeResourceModule(name, buf); |
| 1215 | break; |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 1216 | #endif |
| 1217 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1218 | case PKG_DIRECTORY: |
| 1219 | m = load_package(name, buf); |
| 1220 | break; |
| 1221 | |
| 1222 | case C_BUILTIN: |
| 1223 | case PY_FROZEN: |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1224 | if (buf != NULL && buf[0] != '\0') |
| 1225 | name = buf; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1226 | if (type == C_BUILTIN) |
| 1227 | err = init_builtin(name); |
| 1228 | else |
| 1229 | err = PyImport_ImportFrozenModule(name); |
| 1230 | if (err < 0) |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 1231 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1232 | if (err == 0) { |
| 1233 | PyErr_Format(PyExc_ImportError, |
| 1234 | "Purported %s module %.200s not found", |
| 1235 | type == C_BUILTIN ? |
| 1236 | "builtin" : "frozen", |
| 1237 | name); |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 1238 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1239 | } |
| 1240 | modules = PyImport_GetModuleDict(); |
| 1241 | m = PyDict_GetItemString(modules, name); |
| 1242 | if (m == NULL) { |
| 1243 | PyErr_Format( |
| 1244 | PyExc_ImportError, |
| 1245 | "%s module %.200s not properly initialized", |
| 1246 | type == C_BUILTIN ? |
| 1247 | "builtin" : "frozen", |
| 1248 | name); |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 1249 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1250 | } |
| 1251 | Py_INCREF(m); |
| 1252 | break; |
| 1253 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1254 | default: |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1255 | PyErr_Format(PyExc_ImportError, |
| 1256 | "Don't know how to import %.200s (type code %d)", |
| 1257 | name, type); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1258 | m = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1259 | |
| 1260 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1261 | |
| 1262 | return m; |
| 1263 | } |
| 1264 | |
| 1265 | |
| 1266 | /* Initialize a built-in module. |
| 1267 | Return 1 for succes, 0 if the module is not found, and -1 with |
| 1268 | an exception set if the initialization failed. */ |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1269 | |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1270 | static int |
| 1271 | init_builtin(name) |
| 1272 | char *name; |
| 1273 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1274 | struct _inittab *p; |
| 1275 | PyObject *mod; |
| 1276 | |
| 1277 | if ((mod = _PyImport_FindExtension(name, name)) != NULL) |
| 1278 | return 1; |
| 1279 | |
Guido van Rossum | 771c6c8 | 1997-10-31 18:37:24 +0000 | [diff] [blame] | 1280 | for (p = PyImport_Inittab; p->name != NULL; p++) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1281 | if (strcmp(name, p->name) == 0) { |
| 1282 | if (p->initfunc == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1283 | PyErr_Format(PyExc_ImportError, |
| 1284 | "Cannot re-init internal module %.200s", |
| 1285 | name); |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1286 | return -1; |
| 1287 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1288 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 1289 | PySys_WriteStderr("import %s # builtin\n", name); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1290 | (*p->initfunc)(); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1291 | if (PyErr_Occurred()) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1292 | return -1; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1293 | if (_PyImport_FixupExtension(name, name) == NULL) |
| 1294 | return -1; |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1295 | return 1; |
| 1296 | } |
| 1297 | } |
| 1298 | return 0; |
| 1299 | } |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1300 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1301 | |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1302 | /* Frozen modules */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1303 | |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1304 | static struct _frozen * |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1305 | find_frozen(name) |
| 1306 | char *name; |
| 1307 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1308 | struct _frozen *p; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1309 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1310 | for (p = PyImport_FrozenModules; ; p++) { |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1311 | if (p->name == NULL) |
| 1312 | return NULL; |
| 1313 | if (strcmp(p->name, name) == 0) |
| 1314 | break; |
| 1315 | } |
| 1316 | return p; |
| 1317 | } |
| 1318 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1319 | static PyObject * |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1320 | get_frozen_object(name) |
| 1321 | char *name; |
| 1322 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1323 | struct _frozen *p = find_frozen(name); |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1324 | int size; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1325 | |
| 1326 | if (p == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1327 | PyErr_Format(PyExc_ImportError, |
| 1328 | "No such frozen object named %.200s", |
| 1329 | name); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1330 | return NULL; |
| 1331 | } |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1332 | size = p->size; |
| 1333 | if (size < 0) |
| 1334 | size = -size; |
| 1335 | return PyMarshal_ReadObjectFromString((char *)p->code, size); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | /* Initialize a frozen module. |
| 1339 | Return 1 for succes, 0 if the module is not found, and -1 with |
| 1340 | an exception set if the initialization failed. |
| 1341 | This function is also used from frozenmain.c */ |
Guido van Rossum | 0b34490 | 1995-02-07 15:35:27 +0000 | [diff] [blame] | 1342 | |
| 1343 | int |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1344 | PyImport_ImportFrozenModule(name) |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1345 | char *name; |
| 1346 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1347 | struct _frozen *p = find_frozen(name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1348 | PyObject *co; |
| 1349 | PyObject *m; |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1350 | int ispackage; |
| 1351 | int size; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1352 | |
| 1353 | if (p == NULL) |
| 1354 | return 0; |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1355 | size = p->size; |
| 1356 | ispackage = (size < 0); |
| 1357 | if (ispackage) |
| 1358 | size = -size; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1359 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 1360 | PySys_WriteStderr("import %s # frozen%s\n", |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1361 | name, ispackage ? " package" : ""); |
| 1362 | co = PyMarshal_ReadObjectFromString((char *)p->code, size); |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1363 | if (co == NULL) |
| 1364 | return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1365 | if (!PyCode_Check(co)) { |
| 1366 | Py_DECREF(co); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1367 | PyErr_Format(PyExc_TypeError, |
| 1368 | "frozen object %.200s is not a code object", |
| 1369 | name); |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1370 | return -1; |
| 1371 | } |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1372 | if (ispackage) { |
| 1373 | /* Set __path__ to the package name */ |
| 1374 | PyObject *d, *s; |
| 1375 | int err; |
| 1376 | m = PyImport_AddModule(name); |
| 1377 | if (m == NULL) |
| 1378 | return -1; |
| 1379 | d = PyModule_GetDict(m); |
| 1380 | s = PyString_InternFromString(name); |
| 1381 | if (s == NULL) |
| 1382 | return -1; |
| 1383 | err = PyDict_SetItemString(d, "__path__", s); |
| 1384 | Py_DECREF(s); |
| 1385 | if (err != 0) |
| 1386 | return err; |
| 1387 | } |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 1388 | m = PyImport_ExecCodeModuleEx(name, co, "<frozen>"); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1389 | Py_DECREF(co); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1390 | if (m == NULL) |
| 1391 | return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1392 | Py_DECREF(m); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1393 | return 1; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1394 | } |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1395 | |
| 1396 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1397 | /* Import a module, either built-in, frozen, or external, and return |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1398 | its module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1399 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1400 | PyObject * |
| 1401 | PyImport_ImportModule(name) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1402 | char *name; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1403 | { |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1404 | static PyObject *fromlist = NULL; |
| 1405 | if (fromlist == NULL && strchr(name, '.') != NULL) { |
| 1406 | fromlist = Py_BuildValue("[s]", "*"); |
| 1407 | if (fromlist == NULL) |
| 1408 | return NULL; |
| 1409 | } |
| 1410 | return PyImport_ImportModuleEx(name, NULL, NULL, fromlist); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1413 | /* Forward declarations for helper routines */ |
| 1414 | static PyObject *get_parent Py_PROTO((PyObject *globals, |
| 1415 | char *buf, int *p_buflen)); |
| 1416 | static PyObject *load_next Py_PROTO((PyObject *mod, PyObject *altmod, |
| 1417 | char **p_name, char *buf, int *p_buflen)); |
Guido van Rossum | f5f5fdb | 1997-09-06 20:29:52 +0000 | [diff] [blame] | 1418 | static int mark_miss Py_PROTO((char *name)); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1419 | static int ensure_fromlist Py_PROTO((PyObject *mod, PyObject *fromlist, |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1420 | char *buf, int buflen, int recursive)); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1421 | static PyObject * import_submodule Py_PROTO((PyObject *mod, |
| 1422 | char *name, char *fullname)); |
| 1423 | |
| 1424 | /* The Magnum Opus of dotted-name import :-) */ |
| 1425 | |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 1426 | static PyObject * |
| 1427 | import_module_ex(name, globals, locals, fromlist) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1428 | char *name; |
| 1429 | PyObject *globals; |
| 1430 | PyObject *locals; |
| 1431 | PyObject *fromlist; |
| 1432 | { |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1433 | char buf[MAXPATHLEN+1]; |
| 1434 | int buflen = 0; |
| 1435 | PyObject *parent, *head, *next, *tail; |
| 1436 | |
| 1437 | parent = get_parent(globals, buf, &buflen); |
| 1438 | if (parent == NULL) |
| 1439 | return NULL; |
| 1440 | |
| 1441 | head = load_next(parent, Py_None, &name, buf, &buflen); |
| 1442 | if (head == NULL) |
| 1443 | return NULL; |
| 1444 | |
| 1445 | tail = head; |
| 1446 | Py_INCREF(tail); |
| 1447 | while (name) { |
| 1448 | next = load_next(tail, tail, &name, buf, &buflen); |
| 1449 | Py_DECREF(tail); |
| 1450 | if (next == NULL) { |
| 1451 | Py_DECREF(head); |
| 1452 | return NULL; |
| 1453 | } |
| 1454 | tail = next; |
| 1455 | } |
| 1456 | |
| 1457 | if (fromlist != NULL) { |
| 1458 | if (fromlist == Py_None || !PyObject_IsTrue(fromlist)) |
| 1459 | fromlist = NULL; |
| 1460 | } |
| 1461 | |
| 1462 | if (fromlist == NULL) { |
| 1463 | Py_DECREF(tail); |
| 1464 | return head; |
| 1465 | } |
| 1466 | |
| 1467 | Py_DECREF(head); |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1468 | if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) { |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1469 | Py_DECREF(tail); |
| 1470 | return NULL; |
| 1471 | } |
| 1472 | |
| 1473 | return tail; |
| 1474 | } |
| 1475 | |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 1476 | PyObject * |
| 1477 | PyImport_ImportModuleEx(name, globals, locals, fromlist) |
| 1478 | char *name; |
| 1479 | PyObject *globals; |
| 1480 | PyObject *locals; |
| 1481 | PyObject *fromlist; |
| 1482 | { |
| 1483 | PyObject *result; |
| 1484 | lock_import(); |
Guido van Rossum | d65911b | 1998-03-03 22:33:27 +0000 | [diff] [blame] | 1485 | result = import_module_ex(name, globals, locals, fromlist); |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 1486 | unlock_import(); |
| 1487 | return result; |
| 1488 | } |
| 1489 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1490 | static PyObject * |
| 1491 | get_parent(globals, buf, p_buflen) |
| 1492 | PyObject *globals; |
| 1493 | char *buf; |
| 1494 | int *p_buflen; |
| 1495 | { |
| 1496 | static PyObject *namestr = NULL; |
| 1497 | static PyObject *pathstr = NULL; |
| 1498 | PyObject *modname, *modpath, *modules, *parent; |
| 1499 | |
| 1500 | if (globals == NULL || !PyDict_Check(globals)) |
| 1501 | return Py_None; |
| 1502 | |
| 1503 | if (namestr == NULL) { |
| 1504 | namestr = PyString_InternFromString("__name__"); |
| 1505 | if (namestr == NULL) |
| 1506 | return NULL; |
| 1507 | } |
| 1508 | if (pathstr == NULL) { |
| 1509 | pathstr = PyString_InternFromString("__path__"); |
| 1510 | if (pathstr == NULL) |
| 1511 | return NULL; |
| 1512 | } |
| 1513 | |
| 1514 | *buf = '\0'; |
| 1515 | *p_buflen = 0; |
| 1516 | modname = PyDict_GetItem(globals, namestr); |
| 1517 | if (modname == NULL || !PyString_Check(modname)) |
| 1518 | return Py_None; |
| 1519 | |
| 1520 | modpath = PyDict_GetItem(globals, pathstr); |
| 1521 | if (modpath != NULL) { |
| 1522 | int len = PyString_GET_SIZE(modname); |
| 1523 | if (len > MAXPATHLEN) { |
| 1524 | PyErr_SetString(PyExc_ValueError, |
| 1525 | "Module name too long"); |
| 1526 | return NULL; |
| 1527 | } |
| 1528 | strcpy(buf, PyString_AS_STRING(modname)); |
| 1529 | *p_buflen = len; |
| 1530 | } |
| 1531 | else { |
| 1532 | char *start = PyString_AS_STRING(modname); |
| 1533 | char *lastdot = strrchr(start, '.'); |
| 1534 | int len; |
| 1535 | if (lastdot == NULL) |
| 1536 | return Py_None; |
| 1537 | len = lastdot - start; |
| 1538 | if (len >= MAXPATHLEN) { |
| 1539 | PyErr_SetString(PyExc_ValueError, |
| 1540 | "Module name too long"); |
| 1541 | return NULL; |
| 1542 | } |
| 1543 | strncpy(buf, start, len); |
| 1544 | buf[len] = '\0'; |
| 1545 | *p_buflen = len; |
| 1546 | } |
| 1547 | |
| 1548 | modules = PyImport_GetModuleDict(); |
| 1549 | parent = PyDict_GetItemString(modules, buf); |
| 1550 | if (parent == NULL) |
| 1551 | parent = Py_None; |
| 1552 | return parent; |
| 1553 | /* We expect, but can't guarantee, if parent != None, that: |
| 1554 | - parent.__name__ == buf |
| 1555 | - parent.__dict__ is globals |
| 1556 | If this is violated... Who cares? */ |
| 1557 | } |
| 1558 | |
| 1559 | static PyObject * |
| 1560 | load_next(mod, altmod, p_name, buf, p_buflen) |
| 1561 | PyObject *mod; |
| 1562 | PyObject *altmod; /* Either None or same as mod */ |
| 1563 | char **p_name; |
| 1564 | char *buf; |
| 1565 | int *p_buflen; |
| 1566 | { |
| 1567 | char *name = *p_name; |
| 1568 | char *dot = strchr(name, '.'); |
| 1569 | int len; |
| 1570 | char *p; |
| 1571 | PyObject *result; |
| 1572 | |
| 1573 | if (dot == NULL) { |
| 1574 | *p_name = NULL; |
| 1575 | len = strlen(name); |
| 1576 | } |
| 1577 | else { |
| 1578 | *p_name = dot+1; |
| 1579 | len = dot-name; |
| 1580 | } |
Guido van Rossum | 111c20b | 1998-04-11 17:38:22 +0000 | [diff] [blame] | 1581 | if (len == 0) { |
| 1582 | PyErr_SetString(PyExc_ValueError, |
| 1583 | "Empty module name"); |
| 1584 | return NULL; |
| 1585 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1586 | |
| 1587 | p = buf + *p_buflen; |
| 1588 | if (p != buf) |
| 1589 | *p++ = '.'; |
| 1590 | if (p+len-buf >= MAXPATHLEN) { |
| 1591 | PyErr_SetString(PyExc_ValueError, |
| 1592 | "Module name too long"); |
| 1593 | return NULL; |
| 1594 | } |
| 1595 | strncpy(p, name, len); |
| 1596 | p[len] = '\0'; |
| 1597 | *p_buflen = p+len-buf; |
| 1598 | |
| 1599 | result = import_submodule(mod, p, buf); |
| 1600 | if (result == Py_None && altmod != mod) { |
| 1601 | Py_DECREF(result); |
Guido van Rossum | f5f5fdb | 1997-09-06 20:29:52 +0000 | [diff] [blame] | 1602 | /* Here, altmod must be None and mod must not be None */ |
Guido van Rossum | 0c81945 | 1997-09-07 06:16:57 +0000 | [diff] [blame] | 1603 | result = import_submodule(altmod, p, p); |
Guido van Rossum | f5f5fdb | 1997-09-06 20:29:52 +0000 | [diff] [blame] | 1604 | if (result != NULL && result != Py_None) { |
| 1605 | if (mark_miss(buf) != 0) { |
| 1606 | Py_DECREF(result); |
| 1607 | return NULL; |
| 1608 | } |
| 1609 | strncpy(buf, name, len); |
| 1610 | buf[len] = '\0'; |
| 1611 | *p_buflen = len; |
| 1612 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1613 | } |
| 1614 | if (result == NULL) |
| 1615 | return NULL; |
| 1616 | |
| 1617 | if (result == Py_None) { |
| 1618 | Py_DECREF(result); |
| 1619 | PyErr_Format(PyExc_ImportError, |
| 1620 | "No module named %.200s", name); |
| 1621 | return NULL; |
| 1622 | } |
| 1623 | |
| 1624 | return result; |
| 1625 | } |
| 1626 | |
| 1627 | static int |
Guido van Rossum | f5f5fdb | 1997-09-06 20:29:52 +0000 | [diff] [blame] | 1628 | mark_miss(name) |
| 1629 | char *name; |
| 1630 | { |
| 1631 | PyObject *modules = PyImport_GetModuleDict(); |
| 1632 | return PyDict_SetItemString(modules, name, Py_None); |
| 1633 | } |
| 1634 | |
| 1635 | static int |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1636 | ensure_fromlist(mod, fromlist, buf, buflen, recursive) |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1637 | PyObject *mod; |
| 1638 | PyObject *fromlist; |
| 1639 | char *buf; |
| 1640 | int buflen; |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1641 | int recursive; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1642 | { |
| 1643 | int i; |
| 1644 | |
| 1645 | if (!PyObject_HasAttrString(mod, "__path__")) |
| 1646 | return 1; |
| 1647 | |
| 1648 | for (i = 0; ; i++) { |
| 1649 | PyObject *item = PySequence_GetItem(fromlist, i); |
| 1650 | int hasit; |
| 1651 | if (item == NULL) { |
| 1652 | if (PyErr_ExceptionMatches(PyExc_IndexError)) { |
| 1653 | PyErr_Clear(); |
| 1654 | return 1; |
| 1655 | } |
| 1656 | return 0; |
| 1657 | } |
| 1658 | if (!PyString_Check(item)) { |
| 1659 | PyErr_SetString(PyExc_TypeError, |
| 1660 | "Item in ``from list'' not a string"); |
| 1661 | Py_DECREF(item); |
| 1662 | return 0; |
| 1663 | } |
| 1664 | if (PyString_AS_STRING(item)[0] == '*') { |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1665 | PyObject *all; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1666 | Py_DECREF(item); |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1667 | /* See if the package defines __all__ */ |
| 1668 | if (recursive) |
| 1669 | continue; /* Avoid endless recursion */ |
| 1670 | all = PyObject_GetAttrString(mod, "__all__"); |
| 1671 | if (all == NULL) |
| 1672 | PyErr_Clear(); |
| 1673 | else { |
| 1674 | if (!ensure_fromlist(mod, all, buf, buflen, 1)) |
| 1675 | return 0; |
| 1676 | Py_DECREF(all); |
| 1677 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1678 | continue; |
| 1679 | } |
| 1680 | hasit = PyObject_HasAttr(mod, item); |
| 1681 | if (!hasit) { |
| 1682 | char *subname = PyString_AS_STRING(item); |
| 1683 | PyObject *submod; |
| 1684 | char *p; |
| 1685 | if (buflen + strlen(subname) >= MAXPATHLEN) { |
| 1686 | PyErr_SetString(PyExc_ValueError, |
| 1687 | "Module name too long"); |
| 1688 | Py_DECREF(item); |
| 1689 | return 0; |
| 1690 | } |
| 1691 | p = buf + buflen; |
| 1692 | *p++ = '.'; |
| 1693 | strcpy(p, subname); |
| 1694 | submod = import_submodule(mod, subname, buf); |
| 1695 | Py_XDECREF(submod); |
| 1696 | if (submod == NULL) { |
| 1697 | Py_DECREF(item); |
| 1698 | return 0; |
| 1699 | } |
| 1700 | } |
| 1701 | Py_DECREF(item); |
| 1702 | } |
| 1703 | |
Guido van Rossum | a7f2e81 | 1997-10-03 15:33:32 +0000 | [diff] [blame] | 1704 | /* NOTREACHED */ |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | static PyObject * |
| 1708 | import_submodule(mod, subname, fullname) |
| 1709 | PyObject *mod; /* May be None */ |
| 1710 | char *subname; |
| 1711 | char *fullname; |
| 1712 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1713 | PyObject *modules = PyImport_GetModuleDict(); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1714 | PyObject *m; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1715 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1716 | /* Require: |
| 1717 | if mod == None: subname == fullname |
| 1718 | else: mod.__name__ + "." + subname == fullname |
| 1719 | */ |
| 1720 | |
| 1721 | if ((m = PyDict_GetItemString(modules, fullname)) != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1722 | Py_INCREF(m); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1723 | } |
| 1724 | else { |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1725 | PyObject *path; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1726 | char buf[MAXPATHLEN+1]; |
| 1727 | struct filedescr *fdp; |
| 1728 | FILE *fp = NULL; |
| 1729 | |
Guido van Rossum | 9c0afe5 | 1998-05-19 15:09:05 +0000 | [diff] [blame] | 1730 | if (mod == Py_None) |
| 1731 | path = NULL; |
| 1732 | else { |
| 1733 | path = PyObject_GetAttrString(mod, "__path__"); |
| 1734 | if (path == NULL) { |
| 1735 | PyErr_Clear(); |
| 1736 | Py_INCREF(Py_None); |
| 1737 | return Py_None; |
| 1738 | } |
| 1739 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1740 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1741 | buf[0] = '\0'; |
Guido van Rossum | b68cd42 | 1998-07-01 17:36:26 +0000 | [diff] [blame] | 1742 | fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp); |
| 1743 | Py_XDECREF(path); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1744 | if (fdp == NULL) { |
| 1745 | if (!PyErr_ExceptionMatches(PyExc_ImportError)) |
| 1746 | return NULL; |
| 1747 | PyErr_Clear(); |
| 1748 | Py_INCREF(Py_None); |
| 1749 | return Py_None; |
| 1750 | } |
| 1751 | m = load_module(fullname, fp, buf, fdp->type); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1752 | if (fp) |
| 1753 | fclose(fp); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1754 | if (m != NULL && mod != Py_None) { |
| 1755 | if (PyObject_SetAttrString(mod, subname, m) < 0) { |
| 1756 | Py_DECREF(m); |
| 1757 | m = NULL; |
| 1758 | } |
| 1759 | } |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1760 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1761 | |
| 1762 | return m; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1765 | |
| 1766 | /* Re-import a module of any kind and return its module object, WITH |
| 1767 | INCREMENTED REFERENCE COUNT */ |
| 1768 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1769 | PyObject * |
| 1770 | PyImport_ReloadModule(m) |
| 1771 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1772 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1773 | PyObject *modules = PyImport_GetModuleDict(); |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 1774 | PyObject *path = NULL; |
| 1775 | char *name, *subname; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1776 | char buf[MAXPATHLEN+1]; |
| 1777 | struct filedescr *fdp; |
| 1778 | FILE *fp = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1779 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1780 | if (m == NULL || !PyModule_Check(m)) { |
| 1781 | PyErr_SetString(PyExc_TypeError, |
| 1782 | "reload() argument must be module"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1783 | return NULL; |
| 1784 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1785 | name = PyModule_GetName(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1786 | if (name == NULL) |
| 1787 | return NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1788 | if (m != PyDict_GetItemString(modules, name)) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1789 | PyErr_Format(PyExc_ImportError, |
| 1790 | "reload(): module %.200s not in sys.modules", |
| 1791 | name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1792 | return NULL; |
| 1793 | } |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 1794 | subname = strrchr(name, '.'); |
| 1795 | if (subname == NULL) |
| 1796 | subname = name; |
| 1797 | else { |
| 1798 | PyObject *parentname, *parent; |
| 1799 | parentname = PyString_FromStringAndSize(name, (subname-name)); |
| 1800 | if (parentname == NULL) |
| 1801 | return NULL; |
| 1802 | parent = PyDict_GetItem(modules, parentname); |
| 1803 | if (parent == NULL) { |
| 1804 | PyErr_Format(PyExc_ImportError, |
| 1805 | "reload(): parent %.200s not in sys.modules", |
| 1806 | name); |
| 1807 | return NULL; |
| 1808 | } |
| 1809 | subname++; |
| 1810 | path = PyObject_GetAttrString(parent, "__path__"); |
| 1811 | if (path == NULL) |
| 1812 | PyErr_Clear(); |
| 1813 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1814 | buf[0] = '\0'; |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 1815 | fdp = find_module(subname, path, buf, MAXPATHLEN+1, &fp); |
| 1816 | Py_XDECREF(path); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1817 | if (fdp == NULL) |
| 1818 | return NULL; |
| 1819 | m = load_module(name, fp, buf, fdp->type); |
| 1820 | if (fp) |
| 1821 | fclose(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1822 | return m; |
| 1823 | } |
| 1824 | |
| 1825 | |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 1826 | /* Higher-level import emulator which emulates the "import" statement |
| 1827 | more accurately -- it invokes the __import__() function from the |
| 1828 | builtins of the current globals. This means that the import is |
| 1829 | done using whatever import hooks are installed in the current |
| 1830 | environment, e.g. by "ni" or "rexec". */ |
| 1831 | |
| 1832 | PyObject * |
| 1833 | PyImport_Import(module_name) |
| 1834 | PyObject *module_name; |
| 1835 | { |
| 1836 | static PyObject *silly_list = NULL; |
| 1837 | static PyObject *builtins_str = NULL; |
| 1838 | static PyObject *import_str = NULL; |
| 1839 | static PyObject *standard_builtins = NULL; |
| 1840 | PyObject *globals = NULL; |
| 1841 | PyObject *import = NULL; |
| 1842 | PyObject *builtins = NULL; |
| 1843 | PyObject *r = NULL; |
| 1844 | |
| 1845 | /* Initialize constant string objects */ |
| 1846 | if (silly_list == NULL) { |
| 1847 | import_str = PyString_InternFromString("__import__"); |
| 1848 | if (import_str == NULL) |
| 1849 | return NULL; |
| 1850 | builtins_str = PyString_InternFromString("__builtins__"); |
| 1851 | if (builtins_str == NULL) |
| 1852 | return NULL; |
| 1853 | silly_list = Py_BuildValue("[s]", "__doc__"); |
| 1854 | if (silly_list == NULL) |
| 1855 | return NULL; |
| 1856 | } |
| 1857 | |
| 1858 | /* Get the builtins from current globals */ |
| 1859 | globals = PyEval_GetGlobals(); |
| 1860 | if(globals != NULL) { |
| 1861 | builtins = PyObject_GetItem(globals, builtins_str); |
| 1862 | if (builtins == NULL) |
| 1863 | goto err; |
| 1864 | } |
| 1865 | else { |
| 1866 | /* No globals -- use standard builtins, and fake globals */ |
| 1867 | PyErr_Clear(); |
| 1868 | |
| 1869 | if (standard_builtins == NULL) { |
| 1870 | standard_builtins = |
| 1871 | PyImport_ImportModule("__builtin__"); |
| 1872 | if (standard_builtins == NULL) |
| 1873 | return NULL; |
| 1874 | } |
| 1875 | |
| 1876 | builtins = standard_builtins; |
| 1877 | Py_INCREF(builtins); |
| 1878 | globals = Py_BuildValue("{OO}", builtins_str, builtins); |
| 1879 | if (globals == NULL) |
| 1880 | goto err; |
| 1881 | } |
| 1882 | |
| 1883 | /* Get the __import__ function from the builtins */ |
| 1884 | if (PyDict_Check(builtins)) |
| 1885 | import=PyObject_GetItem(builtins, import_str); |
| 1886 | else |
| 1887 | import=PyObject_GetAttr(builtins, import_str); |
| 1888 | if (import == NULL) |
| 1889 | goto err; |
| 1890 | |
| 1891 | /* Call the _import__ function with the proper argument list */ |
| 1892 | r = PyObject_CallFunction(import, "OOOO", |
| 1893 | module_name, globals, globals, silly_list); |
| 1894 | |
| 1895 | err: |
| 1896 | Py_XDECREF(globals); |
| 1897 | Py_XDECREF(builtins); |
| 1898 | Py_XDECREF(import); |
| 1899 | |
| 1900 | return r; |
| 1901 | } |
| 1902 | |
| 1903 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1904 | /* Module 'imp' provides Python access to the primitives used for |
| 1905 | importing modules. |
| 1906 | */ |
| 1907 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1908 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1909 | imp_get_magic(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1910 | PyObject *self; |
| 1911 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1912 | { |
| 1913 | char buf[4]; |
| 1914 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1915 | if (!PyArg_ParseTuple(args, "")) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1916 | return NULL; |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 1917 | buf[0] = (char) ((MAGIC >> 0) & 0xff); |
| 1918 | buf[1] = (char) ((MAGIC >> 8) & 0xff); |
| 1919 | buf[2] = (char) ((MAGIC >> 16) & 0xff); |
| 1920 | buf[3] = (char) ((MAGIC >> 24) & 0xff); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1921 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1922 | return PyString_FromStringAndSize(buf, 4); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1925 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1926 | imp_get_suffixes(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1927 | PyObject *self; |
| 1928 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1929 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1930 | PyObject *list; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1931 | struct filedescr *fdp; |
| 1932 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1933 | if (!PyArg_ParseTuple(args, "")) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1934 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1935 | list = PyList_New(0); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1936 | if (list == NULL) |
| 1937 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1938 | for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { |
| 1939 | PyObject *item = Py_BuildValue("ssi", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1940 | fdp->suffix, fdp->mode, fdp->type); |
| 1941 | if (item == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1942 | Py_DECREF(list); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1943 | return NULL; |
| 1944 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1945 | if (PyList_Append(list, item) < 0) { |
| 1946 | Py_DECREF(list); |
| 1947 | Py_DECREF(item); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1948 | return NULL; |
| 1949 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1950 | Py_DECREF(item); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1951 | } |
| 1952 | return list; |
| 1953 | } |
| 1954 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1955 | static PyObject * |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1956 | call_find_module(name, path) |
| 1957 | char *name; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1958 | PyObject *path; /* list or None or NULL */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1959 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1960 | extern int fclose Py_PROTO((FILE *)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1961 | PyObject *fob, *ret; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1962 | struct filedescr *fdp; |
| 1963 | char pathname[MAXPATHLEN+1]; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1964 | FILE *fp = NULL; |
| 1965 | |
| 1966 | pathname[0] = '\0'; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1967 | if (path == Py_None) |
| 1968 | path = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1969 | fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp); |
| 1970 | if (fdp == NULL) |
| 1971 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1972 | if (fp != NULL) { |
| 1973 | fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose); |
| 1974 | if (fob == NULL) { |
| 1975 | fclose(fp); |
| 1976 | return NULL; |
| 1977 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1978 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1979 | else { |
| 1980 | fob = Py_None; |
| 1981 | Py_INCREF(fob); |
| 1982 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1983 | ret = Py_BuildValue("Os(ssi)", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1984 | fob, pathname, fdp->suffix, fdp->mode, fdp->type); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1985 | Py_DECREF(fob); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1986 | return ret; |
| 1987 | } |
| 1988 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1989 | static PyObject * |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1990 | imp_find_module(self, args) |
| 1991 | PyObject *self; |
| 1992 | PyObject *args; |
| 1993 | { |
| 1994 | char *name; |
| 1995 | PyObject *path = NULL; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1996 | if (!PyArg_ParseTuple(args, "s|O", &name, &path)) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1997 | return NULL; |
| 1998 | return call_find_module(name, path); |
| 1999 | } |
| 2000 | |
| 2001 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2002 | imp_init_builtin(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2003 | PyObject *self; |
| 2004 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2005 | { |
| 2006 | char *name; |
| 2007 | int ret; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2008 | PyObject *m; |
| 2009 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2010 | return NULL; |
| 2011 | ret = init_builtin(name); |
| 2012 | if (ret < 0) |
| 2013 | return NULL; |
| 2014 | if (ret == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2015 | Py_INCREF(Py_None); |
| 2016 | return Py_None; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2017 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2018 | m = PyImport_AddModule(name); |
| 2019 | Py_XINCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2020 | return m; |
| 2021 | } |
| 2022 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2023 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2024 | imp_init_frozen(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2025 | PyObject *self; |
| 2026 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2027 | { |
| 2028 | char *name; |
| 2029 | int ret; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2030 | PyObject *m; |
| 2031 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2032 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2033 | ret = PyImport_ImportFrozenModule(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2034 | if (ret < 0) |
| 2035 | return NULL; |
| 2036 | if (ret == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2037 | Py_INCREF(Py_None); |
| 2038 | return Py_None; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2039 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2040 | m = PyImport_AddModule(name); |
| 2041 | Py_XINCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2042 | return m; |
| 2043 | } |
| 2044 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2045 | static PyObject * |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2046 | imp_get_frozen_object(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2047 | PyObject *self; |
| 2048 | PyObject *args; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2049 | { |
| 2050 | char *name; |
Jack Jansen | 95ffa23 | 1995-10-03 14:38:41 +0000 | [diff] [blame] | 2051 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2052 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2053 | return NULL; |
| 2054 | return get_frozen_object(name); |
| 2055 | } |
| 2056 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2057 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2058 | imp_is_builtin(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2059 | PyObject *self; |
| 2060 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2061 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2062 | char *name; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2063 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2064 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2065 | return PyInt_FromLong(is_builtin(name)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2068 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2069 | imp_is_frozen(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2070 | PyObject *self; |
| 2071 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2072 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2073 | char *name; |
Guido van Rossum | 323bf5e | 1998-06-24 03:54:06 +0000 | [diff] [blame] | 2074 | struct _frozen *p; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2075 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2076 | return NULL; |
Guido van Rossum | 323bf5e | 1998-06-24 03:54:06 +0000 | [diff] [blame] | 2077 | p = find_frozen(name); |
| 2078 | return PyInt_FromLong((long) (p == NULL ? 0 : p->size)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | static FILE * |
| 2082 | get_file(pathname, fob, mode) |
| 2083 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2084 | PyObject *fob; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2085 | char *mode; |
| 2086 | { |
| 2087 | FILE *fp; |
| 2088 | if (fob == NULL) { |
| 2089 | fp = fopen(pathname, mode); |
| 2090 | if (fp == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2091 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2092 | } |
| 2093 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2094 | fp = PyFile_AsFile(fob); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2095 | if (fp == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2096 | PyErr_SetString(PyExc_ValueError, |
| 2097 | "bad/closed file object"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2098 | } |
| 2099 | return fp; |
| 2100 | } |
| 2101 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2102 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2103 | imp_load_compiled(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2104 | PyObject *self; |
| 2105 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2106 | { |
| 2107 | char *name; |
| 2108 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2109 | PyObject *fob = NULL; |
| 2110 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2111 | FILE *fp; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2112 | if (!PyArg_ParseTuple(args, "ss|O!", &name, &pathname, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2113 | &PyFile_Type, &fob)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2114 | return NULL; |
| 2115 | fp = get_file(pathname, fob, "rb"); |
| 2116 | if (fp == NULL) |
| 2117 | return NULL; |
| 2118 | m = load_compiled_module(name, pathname, fp); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2119 | if (fob == NULL) |
| 2120 | fclose(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2121 | return m; |
| 2122 | } |
| 2123 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2124 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2125 | imp_load_dynamic(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2126 | PyObject *self; |
| 2127 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2128 | { |
| 2129 | char *name; |
| 2130 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2131 | PyObject *fob = NULL; |
| 2132 | PyObject *m; |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 2133 | FILE *fp = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2134 | if (!PyArg_ParseTuple(args, "ss|O!", &name, &pathname, |
| 2135 | &PyFile_Type, &fob)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2136 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2137 | if (fob) { |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 2138 | fp = get_file(pathname, fob, "r"); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2139 | if (fp == NULL) |
| 2140 | return NULL; |
| 2141 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2142 | m = _PyImport_LoadDynamicModule(name, pathname, fp); |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 2143 | return m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2146 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2147 | imp_load_source(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2148 | PyObject *self; |
| 2149 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2150 | { |
| 2151 | char *name; |
| 2152 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2153 | PyObject *fob = NULL; |
| 2154 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2155 | FILE *fp; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2156 | if (!PyArg_ParseTuple(args, "ss|O!", &name, &pathname, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2157 | &PyFile_Type, &fob)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2158 | return NULL; |
| 2159 | fp = get_file(pathname, fob, "r"); |
| 2160 | if (fp == NULL) |
| 2161 | return NULL; |
| 2162 | m = load_source_module(name, pathname, fp); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2163 | if (fob == NULL) |
| 2164 | fclose(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2165 | return m; |
| 2166 | } |
| 2167 | |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 2168 | #ifdef macintosh |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2169 | static PyObject * |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 2170 | imp_load_resource(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2171 | PyObject *self; |
| 2172 | PyObject *args; |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 2173 | { |
| 2174 | char *name; |
| 2175 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2176 | PyObject *m; |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 2177 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2178 | if (!PyArg_ParseTuple(args, "ss", &name, &pathname)) |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 2179 | return NULL; |
| 2180 | m = PyMac_LoadResourceModule(name, pathname); |
| 2181 | return m; |
| 2182 | } |
| 2183 | #endif /* macintosh */ |
| 2184 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2185 | static PyObject * |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2186 | imp_load_module(self, args) |
| 2187 | PyObject *self; |
| 2188 | PyObject *args; |
| 2189 | { |
| 2190 | char *name; |
| 2191 | PyObject *fob; |
| 2192 | char *pathname; |
| 2193 | char *suffix; /* Unused */ |
| 2194 | char *mode; |
| 2195 | int type; |
| 2196 | FILE *fp; |
| 2197 | |
| 2198 | if (!PyArg_ParseTuple(args, "sOs(ssi)", |
| 2199 | &name, &fob, &pathname, |
| 2200 | &suffix, &mode, &type)) |
| 2201 | return NULL; |
| 2202 | if (*mode && (*mode != 'r' || strchr(mode, '+') != NULL)) { |
| 2203 | PyErr_Format(PyExc_ValueError, |
| 2204 | "invalid file open mode %.200s", mode); |
| 2205 | return NULL; |
| 2206 | } |
| 2207 | if (fob == Py_None) |
| 2208 | fp = NULL; |
| 2209 | else { |
| 2210 | if (!PyFile_Check(fob)) { |
| 2211 | PyErr_SetString(PyExc_ValueError, |
| 2212 | "load_module arg#2 should be a file or None"); |
| 2213 | return NULL; |
| 2214 | } |
| 2215 | fp = get_file(pathname, fob, mode); |
| 2216 | if (fp == NULL) |
| 2217 | return NULL; |
| 2218 | } |
| 2219 | return load_module(name, fp, pathname, type); |
| 2220 | } |
| 2221 | |
| 2222 | static PyObject * |
| 2223 | imp_load_package(self, args) |
| 2224 | PyObject *self; |
| 2225 | PyObject *args; |
| 2226 | { |
| 2227 | char *name; |
| 2228 | char *pathname; |
| 2229 | if (!PyArg_ParseTuple(args, "ss", &name, &pathname)) |
| 2230 | return NULL; |
| 2231 | return load_package(name, pathname); |
| 2232 | } |
| 2233 | |
| 2234 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2235 | imp_new_module(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2236 | PyObject *self; |
| 2237 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2238 | { |
| 2239 | char *name; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2240 | if (!PyArg_ParseTuple(args, "s", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2241 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2242 | return PyModule_New(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2245 | /* Doc strings */ |
| 2246 | |
| 2247 | static char doc_imp[] = "\ |
| 2248 | This module provides the components needed to build your own\n\ |
| 2249 | __import__ function. Undocumented functions are obsolete.\n\ |
| 2250 | "; |
| 2251 | |
| 2252 | static char doc_find_module[] = "\ |
| 2253 | find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\ |
| 2254 | Search for a module. If path is omitted or None, search for a\n\ |
| 2255 | built-in, frozen or special module and continue search in sys.path.\n\ |
| 2256 | The module name cannot contain '.'; to search for a submodule of a\n\ |
| 2257 | package, pass the submodule name and the package's __path__.\ |
| 2258 | "; |
| 2259 | |
| 2260 | static char doc_load_module[] = "\ |
| 2261 | load_module(name, file, filename, (suffix, mode, type)) -> module\n\ |
| 2262 | Load a module, given information returned by find_module().\n\ |
| 2263 | The module name must include the full package name, if any.\ |
| 2264 | "; |
| 2265 | |
| 2266 | static char doc_get_magic[] = "\ |
| 2267 | get_magic() -> string\n\ |
| 2268 | Return the magic number for .pyc or .pyo files.\ |
| 2269 | "; |
| 2270 | |
| 2271 | static char doc_get_suffixes[] = "\ |
| 2272 | get_suffixes() -> [(suffix, mode, type), ...]\n\ |
| 2273 | Return a list of (suffix, mode, type) tuples describing the files\n\ |
| 2274 | that find_module() looks for.\ |
| 2275 | "; |
| 2276 | |
| 2277 | static char doc_new_module[] = "\ |
| 2278 | new_module(name) -> module\n\ |
| 2279 | Create a new module. Do not enter it in sys.modules.\n\ |
| 2280 | The module name must include the full package name, if any.\ |
| 2281 | "; |
| 2282 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2283 | static PyMethodDef imp_methods[] = { |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2284 | {"find_module", imp_find_module, 1, doc_find_module}, |
| 2285 | {"get_magic", imp_get_magic, 1, doc_get_magic}, |
| 2286 | {"get_suffixes", imp_get_suffixes, 1, doc_get_suffixes}, |
| 2287 | {"load_module", imp_load_module, 1, doc_load_module}, |
| 2288 | {"new_module", imp_new_module, 1, doc_new_module}, |
| 2289 | /* The rest are obsolete */ |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2290 | {"get_frozen_object", imp_get_frozen_object, 1}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2291 | {"init_builtin", imp_init_builtin, 1}, |
| 2292 | {"init_frozen", imp_init_frozen, 1}, |
| 2293 | {"is_builtin", imp_is_builtin, 1}, |
| 2294 | {"is_frozen", imp_is_frozen, 1}, |
| 2295 | {"load_compiled", imp_load_compiled, 1}, |
| 2296 | {"load_dynamic", imp_load_dynamic, 1}, |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2297 | {"load_package", imp_load_package, 1}, |
Jack Jansen | 9c96a92 | 1995-02-15 22:57:06 +0000 | [diff] [blame] | 2298 | #ifdef macintosh |
| 2299 | {"load_resource", imp_load_resource, 1}, |
| 2300 | #endif |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2301 | {"load_source", imp_load_source, 1}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2302 | {NULL, NULL} /* sentinel */ |
| 2303 | }; |
| 2304 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 2305 | static int |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2306 | setint(d, name, value) |
| 2307 | PyObject *d; |
| 2308 | char *name; |
| 2309 | int value; |
| 2310 | { |
| 2311 | PyObject *v; |
| 2312 | int err; |
| 2313 | |
| 2314 | v = PyInt_FromLong((long)value); |
| 2315 | err = PyDict_SetItemString(d, name, v); |
| 2316 | Py_XDECREF(v); |
| 2317 | return err; |
| 2318 | } |
| 2319 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2320 | void |
| 2321 | initimp() |
| 2322 | { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2323 | PyObject *m, *d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2324 | |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2325 | m = Py_InitModule4("imp", imp_methods, doc_imp, |
| 2326 | NULL, PYTHON_API_VERSION); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2327 | d = PyModule_GetDict(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2328 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2329 | if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure; |
| 2330 | if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure; |
| 2331 | if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure; |
| 2332 | if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure; |
| 2333 | if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure; |
| 2334 | if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure; |
| 2335 | if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure; |
| 2336 | if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure; |
Guido van Rossum | 0f84a34 | 1998-08-06 13:36:01 +0000 | [diff] [blame] | 2337 | if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2338 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2339 | failure: |
| 2340 | ; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2341 | } |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2342 | |
| 2343 | |
| 2344 | /* API for embedding applications that want to add their own entries to the |
| 2345 | table of built-in modules. This should normally be called *before* |
| 2346 | Py_Initialize(). When the malloc() or realloc() call fails, -1 is returned |
| 2347 | and the existing table is unchanged. |
| 2348 | |
| 2349 | After a similar function by Just van Rossum. */ |
| 2350 | |
| 2351 | int |
| 2352 | PyImport_ExtendInittab(newtab) |
| 2353 | struct _inittab *newtab; |
| 2354 | { |
| 2355 | static struct _inittab *our_copy = NULL; |
| 2356 | struct _inittab *p; |
| 2357 | int i, n; |
| 2358 | |
| 2359 | /* Count the number of entries in both tables */ |
| 2360 | for (n = 0; newtab[n].name != NULL; n++) |
| 2361 | ; |
| 2362 | if (n == 0) |
| 2363 | return 0; /* Nothing to do */ |
| 2364 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) |
| 2365 | ; |
| 2366 | |
| 2367 | /* Allocate new memory for the combined table */ |
| 2368 | if (our_copy == NULL) |
| 2369 | p = malloc((i+n+1) * sizeof(struct _inittab)); |
| 2370 | else |
| 2371 | p = realloc(our_copy, (i+n+1) * sizeof(struct _inittab)); |
| 2372 | if (p == NULL) |
| 2373 | return -1; |
| 2374 | |
| 2375 | /* Copy the tables into the new memory */ |
| 2376 | if (our_copy != PyImport_Inittab) |
| 2377 | memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab)); |
| 2378 | PyImport_Inittab = our_copy = p; |
| 2379 | memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab)); |
| 2380 | |
| 2381 | return 0; |
| 2382 | } |
| 2383 | |
| 2384 | /* Shorthand to add a single entry given a name and a function */ |
| 2385 | |
| 2386 | int |
| 2387 | PyImport_AppendInittab(name, initfunc) |
| 2388 | char *name; |
| 2389 | void (*initfunc)(); |
| 2390 | { |
| 2391 | struct _inittab newtab[2]; |
| 2392 | |
| 2393 | memset(newtab, '\0', sizeof newtab); |
| 2394 | |
| 2395 | newtab[0].name = name; |
| 2396 | newtab[0].initfunc = initfunc; |
| 2397 | |
| 2398 | return PyImport_ExtendInittab(newtab); |
| 2399 | } |