Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Module definition and import implementation */ |
| 3 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6 | #include "node.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7 | #include "token.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8 | #include "errcode.h" |
Guido van Rossum | c405b7b | 1991-06-04 19:39:42 +0000 | [diff] [blame] | 9 | #include "marshal.h" |
| 10 | #include "compile.h" |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 11 | #include "eval.h" |
Guido van Rossum | d8bac6d | 1992-02-26 15:19:13 +0000 | [diff] [blame] | 12 | #include "osdefs.h" |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 13 | #include "importdl.h" |
Guido van Rossum | c405b7b | 1991-06-04 19:39:42 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 55a8338 | 2000-09-20 20:31:38 +0000 | [diff] [blame] | 15 | #ifdef HAVE_FCNTL_H |
| 16 | #include <fcntl.h> |
| 17 | #endif |
| 18 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 19 | extern time_t PyOS_GetLastModificationTime(char *, FILE *); |
| 20 | /* In getmtime.c */ |
Guido van Rossum | 21d335e | 1993-10-15 13:01:11 +0000 | [diff] [blame] | 21 | |
Jeremy Hylton | d4ceb31 | 2004-04-01 02:45:22 +0000 | [diff] [blame] | 22 | /* Magic word to reject .pyc files generated by other Python versions. |
| 23 | It should change for each incompatible change to the bytecode. |
| 24 | |
| 25 | The value of CR and LF is incorporated so if you ever read or write |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 26 | a .pyc file in text mode the magic number will be wrong; also, the |
Tim Peters | 36515e2 | 2001-11-18 04:06:29 +0000 | [diff] [blame] | 27 | Apple MPW compiler swaps their values, botching string constants. |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 28 | |
Jeremy Hylton | d4ceb31 | 2004-04-01 02:45:22 +0000 | [diff] [blame] | 29 | Apparently, there was a distinction made between even and odd |
| 30 | bytecodes that is related to Unicode. The details aren't clear, |
| 31 | but the magic number has been odd for a long time. |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 32 | |
Jeremy Hylton | d4ceb31 | 2004-04-01 02:45:22 +0000 | [diff] [blame] | 33 | There were a variety of old schemes for setting the magic number. |
| 34 | The current working scheme is to increment the previous value by |
| 35 | 10. |
Guido van Rossum | f689492 | 2002-08-31 15:16:14 +0000 | [diff] [blame] | 36 | |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 37 | Known values: |
| 38 | Python 1.5: 20121 |
| 39 | Python 1.5.1: 20121 |
| 40 | Python 1.5.2: 20121 |
| 41 | Python 2.0: 50823 |
| 42 | Python 2.0.1: 50823 |
| 43 | Python 2.1: 60202 |
| 44 | Python 2.1.1: 60202 |
| 45 | Python 2.1.2: 60202 |
| 46 | Python 2.2: 60717 |
Neal Norwitz | 7fdcb41 | 2002-06-14 01:07:39 +0000 | [diff] [blame] | 47 | Python 2.3a0: 62011 |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 48 | Python 2.3a0: 62021 |
Guido van Rossum | f689492 | 2002-08-31 15:16:14 +0000 | [diff] [blame] | 49 | Python 2.3a0: 62011 (!) |
Jeremy Hylton | d4ceb31 | 2004-04-01 02:45:22 +0000 | [diff] [blame] | 50 | Python 2.4a0: 62031 |
Tim Peters | 36515e2 | 2001-11-18 04:06:29 +0000 | [diff] [blame] | 51 | */ |
Jeremy Hylton | d4ceb31 | 2004-04-01 02:45:22 +0000 | [diff] [blame] | 52 | #define MAGIC (62031 | ((long)'\r'<<16) | ((long)'\n'<<24)) |
Guido van Rossum | 3ddee71 | 1991-12-16 13:06:34 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 54 | /* Magic word as global; note that _PyImport_Init() can change the |
Jeremy Hylton | 9262b8a | 2000-06-30 04:59:17 +0000 | [diff] [blame] | 55 | value of this global to accommodate for alterations of how the |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 56 | compiler works which are enabled by command line switches. */ |
| 57 | static long pyc_magic = MAGIC; |
| 58 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 59 | /* See _PyImport_FixupExtension() below */ |
| 60 | static PyObject *extensions = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 771c6c8 | 1997-10-31 18:37:24 +0000 | [diff] [blame] | 62 | /* This table is defined in config.c: */ |
| 63 | extern struct _inittab _PyImport_Inittab[]; |
| 64 | |
| 65 | struct _inittab *PyImport_Inittab = _PyImport_Inittab; |
Guido van Rossum | 66f1fa8 | 1991-04-03 19:03:52 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 67 | /* these tables define the module suffixes that Python recognizes */ |
| 68 | struct filedescr * _PyImport_Filetab = NULL; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 69 | |
| 70 | #ifdef RISCOS |
| 71 | static const struct filedescr _PyImport_StandardFiletab[] = { |
Jack Jansen | c88da1f | 2002-05-28 10:58:19 +0000 | [diff] [blame] | 72 | {"/py", "U", PY_SOURCE}, |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 73 | {"/pyc", "rb", PY_COMPILED}, |
| 74 | {0, 0} |
| 75 | }; |
| 76 | #else |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 77 | static const struct filedescr _PyImport_StandardFiletab[] = { |
Jack Jansen | c88da1f | 2002-05-28 10:58:19 +0000 | [diff] [blame] | 78 | {".py", "U", PY_SOURCE}, |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 79 | #ifdef MS_WINDOWS |
Jack Jansen | c88da1f | 2002-05-28 10:58:19 +0000 | [diff] [blame] | 80 | {".pyw", "U", PY_SOURCE}, |
Tim Peters | 36515e2 | 2001-11-18 04:06:29 +0000 | [diff] [blame] | 81 | #endif |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 82 | {".pyc", "rb", PY_COMPILED}, |
| 83 | {0, 0} |
| 84 | }; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 85 | #endif |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 87 | /* Initialize things */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 88 | |
| 89 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 90 | _PyImport_Init(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 91 | { |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 92 | const struct filedescr *scan; |
| 93 | struct filedescr *filetab; |
| 94 | int countD = 0; |
| 95 | int countS = 0; |
| 96 | |
| 97 | /* prepare _PyImport_Filetab: copy entries from |
| 98 | _PyImport_DynLoadFiletab and _PyImport_StandardFiletab. |
| 99 | */ |
| 100 | for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan) |
| 101 | ++countD; |
| 102 | for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan) |
| 103 | ++countS; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 104 | filetab = PyMem_NEW(struct filedescr, countD + countS + 1); |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 105 | memcpy(filetab, _PyImport_DynLoadFiletab, |
| 106 | countD * sizeof(struct filedescr)); |
| 107 | memcpy(filetab + countD, _PyImport_StandardFiletab, |
| 108 | countS * sizeof(struct filedescr)); |
| 109 | filetab[countD + countS].suffix = NULL; |
| 110 | |
| 111 | _PyImport_Filetab = filetab; |
| 112 | |
Guido van Rossum | 0824f63 | 1997-03-11 18:37:35 +0000 | [diff] [blame] | 113 | if (Py_OptimizeFlag) { |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 114 | /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */ |
| 115 | for (; filetab->suffix != NULL; filetab++) { |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 116 | #ifndef RISCOS |
Guido van Rossum | ed1170e | 1999-12-20 21:23:41 +0000 | [diff] [blame] | 117 | if (strcmp(filetab->suffix, ".pyc") == 0) |
| 118 | filetab->suffix = ".pyo"; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 119 | #else |
| 120 | if (strcmp(filetab->suffix, "/pyc") == 0) |
| 121 | filetab->suffix = "/pyo"; |
| 122 | #endif |
Guido van Rossum | 0824f63 | 1997-03-11 18:37:35 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 125 | |
| 126 | if (Py_UnicodeFlag) { |
| 127 | /* Fix the pyc_magic so that byte compiled code created |
| 128 | using the all-Unicode method doesn't interfere with |
| 129 | code created in normal operation mode. */ |
| 130 | pyc_magic = MAGIC + 1; |
| 131 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 134 | void |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 135 | _PyImportHooks_Init(void) |
| 136 | { |
| 137 | PyObject *v, *path_hooks = NULL, *zimpimport; |
| 138 | int err = 0; |
| 139 | |
| 140 | /* adding sys.path_hooks and sys.path_importer_cache, setting up |
| 141 | zipimport */ |
| 142 | |
| 143 | if (Py_VerboseFlag) |
| 144 | PySys_WriteStderr("# installing zipimport hook\n"); |
| 145 | |
| 146 | v = PyList_New(0); |
| 147 | if (v == NULL) |
| 148 | goto error; |
| 149 | err = PySys_SetObject("meta_path", v); |
| 150 | Py_DECREF(v); |
| 151 | if (err) |
| 152 | goto error; |
| 153 | v = PyDict_New(); |
| 154 | if (v == NULL) |
| 155 | goto error; |
| 156 | err = PySys_SetObject("path_importer_cache", v); |
| 157 | Py_DECREF(v); |
| 158 | if (err) |
| 159 | goto error; |
| 160 | path_hooks = PyList_New(0); |
| 161 | if (path_hooks == NULL) |
| 162 | goto error; |
| 163 | err = PySys_SetObject("path_hooks", path_hooks); |
| 164 | if (err) { |
| 165 | error: |
| 166 | PyErr_Print(); |
| 167 | Py_FatalError("initializing sys.meta_path, sys.path_hooks or " |
| 168 | "path_importer_cache failed"); |
| 169 | } |
| 170 | zimpimport = PyImport_ImportModule("zipimport"); |
| 171 | if (zimpimport == NULL) { |
| 172 | PyErr_Clear(); /* No zip import module -- okay */ |
| 173 | if (Py_VerboseFlag) |
| 174 | PySys_WriteStderr("# can't import zipimport\n"); |
| 175 | } |
| 176 | else { |
| 177 | PyObject *zipimporter = PyObject_GetAttrString(zimpimport, |
| 178 | "zipimporter"); |
| 179 | Py_DECREF(zimpimport); |
| 180 | if (zipimporter == NULL) { |
| 181 | PyErr_Clear(); /* No zipimporter object -- okay */ |
| 182 | if (Py_VerboseFlag) |
| 183 | PySys_WriteStderr( |
Fred Drake | 1e5fc55 | 2003-07-11 15:01:02 +0000 | [diff] [blame] | 184 | "# can't import zipimport.zipimporter\n"); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 185 | } |
| 186 | else { |
| 187 | /* sys.path_hooks.append(zipimporter) */ |
| 188 | err = PyList_Append(path_hooks, zipimporter); |
| 189 | Py_DECREF(zipimporter); |
| 190 | if (err) |
| 191 | goto error; |
| 192 | if (Py_VerboseFlag) |
| 193 | PySys_WriteStderr( |
| 194 | "# installed zipimport hook\n"); |
| 195 | } |
| 196 | } |
| 197 | Py_DECREF(path_hooks); |
| 198 | } |
| 199 | |
| 200 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 201 | _PyImport_Fini(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 202 | { |
| 203 | Py_XDECREF(extensions); |
| 204 | extensions = NULL; |
Barry Warsaw | 8429448 | 2000-10-03 16:02:05 +0000 | [diff] [blame] | 205 | PyMem_DEL(_PyImport_Filetab); |
| 206 | _PyImport_Filetab = NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 210 | /* Locking primitives to prevent parallel imports of the same module |
| 211 | in different threads to return with a partially loaded module. |
| 212 | These calls are serialized by the global interpreter lock. */ |
| 213 | |
| 214 | #ifdef WITH_THREAD |
| 215 | |
Guido van Rossum | 49b5606 | 1998-10-01 20:42:43 +0000 | [diff] [blame] | 216 | #include "pythread.h" |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 217 | |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 218 | static PyThread_type_lock import_lock = 0; |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 219 | static long import_lock_thread = -1; |
| 220 | static int import_lock_level = 0; |
| 221 | |
| 222 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 223 | lock_import(void) |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 224 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 225 | long me = PyThread_get_thread_ident(); |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 226 | if (me == -1) |
| 227 | return; /* Too bad */ |
| 228 | if (import_lock == NULL) |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 229 | import_lock = PyThread_allocate_lock(); |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 230 | if (import_lock_thread == me) { |
| 231 | import_lock_level++; |
| 232 | return; |
| 233 | } |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 234 | if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0)) |
| 235 | { |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 236 | PyThreadState *tstate = PyEval_SaveThread(); |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 237 | PyThread_acquire_lock(import_lock, 1); |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 238 | PyEval_RestoreThread(tstate); |
| 239 | } |
| 240 | import_lock_thread = me; |
| 241 | import_lock_level = 1; |
| 242 | } |
| 243 | |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 244 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 245 | unlock_import(void) |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 246 | { |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 247 | long me = PyThread_get_thread_ident(); |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 248 | if (me == -1) |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 249 | return 0; /* Too bad */ |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 250 | if (import_lock_thread != me) |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 251 | return -1; |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 252 | import_lock_level--; |
| 253 | if (import_lock_level == 0) { |
| 254 | import_lock_thread = -1; |
Guido van Rossum | 65d5b57 | 1998-12-21 19:32:43 +0000 | [diff] [blame] | 255 | PyThread_release_lock(import_lock); |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 256 | } |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 257 | return 1; |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | #else |
| 261 | |
| 262 | #define lock_import() |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 263 | #define unlock_import() 0 |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 264 | |
| 265 | #endif |
| 266 | |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 267 | static PyObject * |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 268 | imp_lock_held(PyObject *self, PyObject *noargs) |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 269 | { |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 270 | #ifdef WITH_THREAD |
Guido van Rossum | b8bff3f | 2002-04-07 06:34:38 +0000 | [diff] [blame] | 271 | return PyBool_FromLong(import_lock_thread != -1); |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 272 | #else |
Guido van Rossum | b8bff3f | 2002-04-07 06:34:38 +0000 | [diff] [blame] | 273 | return PyBool_FromLong(0); |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 274 | #endif |
| 275 | } |
| 276 | |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 277 | static PyObject * |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 278 | imp_acquire_lock(PyObject *self, PyObject *noargs) |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 279 | { |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 280 | #ifdef WITH_THREAD |
| 281 | lock_import(); |
| 282 | #endif |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 283 | Py_INCREF(Py_None); |
| 284 | return Py_None; |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static PyObject * |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 288 | imp_release_lock(PyObject *self, PyObject *noargs) |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 289 | { |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 290 | #ifdef WITH_THREAD |
| 291 | if (unlock_import() < 0) { |
| 292 | PyErr_SetString(PyExc_RuntimeError, |
| 293 | "not holding the import lock"); |
| 294 | return NULL; |
| 295 | } |
| 296 | #endif |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 297 | Py_INCREF(Py_None); |
| 298 | return Py_None; |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 301 | /* Helper for sys */ |
| 302 | |
| 303 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 304 | PyImport_GetModuleDict(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 305 | { |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 306 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 307 | if (interp->modules == NULL) |
| 308 | Py_FatalError("PyImport_GetModuleDict: no module dictionary!"); |
| 309 | return interp->modules; |
| 310 | } |
| 311 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 312 | |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 313 | /* List of names to clear in sys */ |
| 314 | static char* sys_deletes[] = { |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 315 | "path", "argv", "ps1", "ps2", "exitfunc", |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 316 | "exc_type", "exc_value", "exc_traceback", |
| 317 | "last_type", "last_value", "last_traceback", |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 318 | "path_hooks", "path_importer_cache", "meta_path", |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 319 | NULL |
| 320 | }; |
| 321 | |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 322 | static char* sys_files[] = { |
| 323 | "stdin", "__stdin__", |
| 324 | "stdout", "__stdout__", |
| 325 | "stderr", "__stderr__", |
| 326 | NULL |
| 327 | }; |
| 328 | |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 329 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 330 | /* Un-initialize things, as good as we can */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 331 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 332 | void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 333 | PyImport_Cleanup(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 334 | { |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 335 | int pos, ndone; |
| 336 | char *name; |
| 337 | PyObject *key, *value, *dict; |
Nicholas Bastin | e5662ae | 2004-03-24 22:22:12 +0000 | [diff] [blame] | 338 | PyInterpreterState *interp = PyThreadState_GET()->interp; |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 339 | PyObject *modules = interp->modules; |
| 340 | |
| 341 | if (modules == NULL) |
| 342 | return; /* Already done */ |
| 343 | |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 344 | /* Delete some special variables first. These are common |
| 345 | places where user values hide and people complain when their |
| 346 | destructors fail. Since the modules containing them are |
| 347 | deleted *last* of all, they would come too late in the normal |
| 348 | destruction order. Sigh. */ |
| 349 | |
| 350 | value = PyDict_GetItemString(modules, "__builtin__"); |
| 351 | if (value != NULL && PyModule_Check(value)) { |
| 352 | dict = PyModule_GetDict(value); |
| 353 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 354 | PySys_WriteStderr("# clear __builtin__._\n"); |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 355 | PyDict_SetItemString(dict, "_", Py_None); |
| 356 | } |
| 357 | value = PyDict_GetItemString(modules, "sys"); |
| 358 | if (value != NULL && PyModule_Check(value)) { |
| 359 | char **p; |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 360 | PyObject *v; |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 361 | dict = PyModule_GetDict(value); |
| 362 | for (p = sys_deletes; *p != NULL; p++) { |
| 363 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 364 | PySys_WriteStderr("# clear sys.%s\n", *p); |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 365 | PyDict_SetItemString(dict, *p, Py_None); |
| 366 | } |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 367 | for (p = sys_files; *p != NULL; p+=2) { |
| 368 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 369 | PySys_WriteStderr("# restore sys.%s\n", *p); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 370 | v = PyDict_GetItemString(dict, *(p+1)); |
| 371 | if (v == NULL) |
| 372 | v = Py_None; |
| 373 | PyDict_SetItemString(dict, *p, v); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /* First, delete __main__ */ |
| 378 | value = PyDict_GetItemString(modules, "__main__"); |
| 379 | if (value != NULL && PyModule_Check(value)) { |
| 380 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 381 | PySys_WriteStderr("# cleanup __main__\n"); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 382 | _PyModule_Clear(value); |
| 383 | PyDict_SetItemString(modules, "__main__", Py_None); |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 386 | /* The special treatment of __builtin__ here is because even |
| 387 | when it's not referenced as a module, its dictionary is |
| 388 | referenced by almost every module's __builtins__. Since |
| 389 | deleting a module clears its dictionary (even if there are |
| 390 | references left to it), we need to delete the __builtin__ |
| 391 | module last. Likewise, we don't delete sys until the very |
| 392 | end because it is implicitly referenced (e.g. by print). |
| 393 | |
| 394 | Also note that we 'delete' modules by replacing their entry |
| 395 | in the modules dict with None, rather than really deleting |
| 396 | them; this avoids a rehash of the modules dictionary and |
| 397 | also marks them as "non existent" so they won't be |
| 398 | re-imported. */ |
| 399 | |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 400 | /* Next, repeatedly delete modules with a reference count of |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 401 | one (skipping __builtin__ and sys) and delete them */ |
| 402 | do { |
| 403 | ndone = 0; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 404 | pos = 0; |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 405 | while (PyDict_Next(modules, &pos, &key, &value)) { |
| 406 | if (value->ob_refcnt != 1) |
| 407 | continue; |
Guido van Rossum | 566373e | 1998-10-01 15:24:50 +0000 | [diff] [blame] | 408 | if (PyString_Check(key) && PyModule_Check(value)) { |
| 409 | name = PyString_AS_STRING(key); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 410 | if (strcmp(name, "__builtin__") == 0) |
| 411 | continue; |
| 412 | if (strcmp(name, "sys") == 0) |
| 413 | continue; |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 414 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 415 | PySys_WriteStderr( |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 416 | "# cleanup[1] %s\n", name); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 417 | _PyModule_Clear(value); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 418 | PyDict_SetItem(modules, key, Py_None); |
| 419 | ndone++; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 422 | } while (ndone > 0); |
| 423 | |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 424 | /* Next, delete all modules (still skipping __builtin__ and sys) */ |
| 425 | pos = 0; |
| 426 | while (PyDict_Next(modules, &pos, &key, &value)) { |
Guido van Rossum | 566373e | 1998-10-01 15:24:50 +0000 | [diff] [blame] | 427 | if (PyString_Check(key) && PyModule_Check(value)) { |
| 428 | name = PyString_AS_STRING(key); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 429 | if (strcmp(name, "__builtin__") == 0) |
| 430 | continue; |
| 431 | if (strcmp(name, "sys") == 0) |
| 432 | continue; |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 433 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 434 | PySys_WriteStderr("# cleanup[2] %s\n", name); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 435 | _PyModule_Clear(value); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 436 | PyDict_SetItem(modules, key, Py_None); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /* Next, delete sys and __builtin__ (in that order) */ |
| 441 | value = PyDict_GetItemString(modules, "sys"); |
| 442 | if (value != NULL && PyModule_Check(value)) { |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 443 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 444 | PySys_WriteStderr("# cleanup sys\n"); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 445 | _PyModule_Clear(value); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 446 | PyDict_SetItemString(modules, "sys", Py_None); |
| 447 | } |
| 448 | value = PyDict_GetItemString(modules, "__builtin__"); |
| 449 | if (value != NULL && PyModule_Check(value)) { |
Guido van Rossum | a0fec2b | 1998-02-06 17:16:02 +0000 | [diff] [blame] | 450 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 451 | PySys_WriteStderr("# cleanup __builtin__\n"); |
Guido van Rossum | 05f9dce | 1998-02-19 20:58:44 +0000 | [diff] [blame] | 452 | _PyModule_Clear(value); |
Guido van Rossum | 758eec0 | 1998-01-19 21:58:26 +0000 | [diff] [blame] | 453 | PyDict_SetItemString(modules, "__builtin__", Py_None); |
| 454 | } |
| 455 | |
| 456 | /* Finally, clear and delete the modules directory */ |
| 457 | PyDict_Clear(modules); |
| 458 | interp->modules = NULL; |
| 459 | Py_DECREF(modules); |
Guido van Rossum | 8d15b5d | 1990-10-26 14:58:58 +0000 | [diff] [blame] | 460 | } |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 461 | |
| 462 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 463 | /* Helper for pythonrun.c -- return magic number */ |
| 464 | |
| 465 | long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 466 | PyImport_GetMagicNumber(void) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 467 | { |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 468 | return pyc_magic; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 472 | /* Magic for extension modules (built-in as well as dynamically |
| 473 | loaded). To prevent initializing an extension module more than |
| 474 | once, we keep a static dictionary 'extensions' keyed by module name |
| 475 | (for built-in modules) or by filename (for dynamically loaded |
Barry Warsaw | 9288338 | 2001-08-13 23:05:44 +0000 | [diff] [blame] | 476 | modules), containing these modules. A copy of the module's |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 477 | dictionary is stored by calling _PyImport_FixupExtension() |
| 478 | immediately after the module initialization function succeeds. A |
| 479 | copy can be retrieved from there by calling |
| 480 | _PyImport_FindExtension(). */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 481 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 482 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 483 | _PyImport_FixupExtension(char *name, char *filename) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 484 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 485 | PyObject *modules, *mod, *dict, *copy; |
| 486 | if (extensions == NULL) { |
| 487 | extensions = PyDict_New(); |
| 488 | if (extensions == NULL) |
| 489 | return NULL; |
| 490 | } |
| 491 | modules = PyImport_GetModuleDict(); |
| 492 | mod = PyDict_GetItemString(modules, name); |
| 493 | if (mod == NULL || !PyModule_Check(mod)) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 494 | PyErr_Format(PyExc_SystemError, |
| 495 | "_PyImport_FixupExtension: module %.200s not loaded", name); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 496 | return NULL; |
| 497 | } |
| 498 | dict = PyModule_GetDict(mod); |
| 499 | if (dict == NULL) |
| 500 | return NULL; |
Fred Drake | 9cd0efc | 2001-10-25 21:38:59 +0000 | [diff] [blame] | 501 | copy = PyDict_Copy(dict); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 502 | if (copy == NULL) |
| 503 | return NULL; |
| 504 | PyDict_SetItemString(extensions, filename, copy); |
| 505 | Py_DECREF(copy); |
| 506 | return copy; |
| 507 | } |
| 508 | |
| 509 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 510 | _PyImport_FindExtension(char *name, char *filename) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 511 | { |
Fred Drake | 9cd0efc | 2001-10-25 21:38:59 +0000 | [diff] [blame] | 512 | PyObject *dict, *mod, *mdict; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 513 | if (extensions == NULL) |
| 514 | return NULL; |
| 515 | dict = PyDict_GetItemString(extensions, filename); |
| 516 | if (dict == NULL) |
| 517 | return NULL; |
| 518 | mod = PyImport_AddModule(name); |
| 519 | if (mod == NULL) |
| 520 | return NULL; |
| 521 | mdict = PyModule_GetDict(mod); |
| 522 | if (mdict == NULL) |
| 523 | return NULL; |
Fred Drake | 9cd0efc | 2001-10-25 21:38:59 +0000 | [diff] [blame] | 524 | if (PyDict_Update(mdict, dict)) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 525 | return NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 526 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 527 | PySys_WriteStderr("import %s # previously loaded (%s)\n", |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 528 | name, filename); |
| 529 | return mod; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | |
| 533 | /* Get the module object corresponding to a module name. |
| 534 | First check the modules dictionary if there's one there, |
Walter Dörwald | f0dfc7a | 2003-10-20 14:01:56 +0000 | [diff] [blame] | 535 | if not, create a new one and insert it in the modules dictionary. |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 536 | Because the former action is most common, THIS DOES NOT RETURN A |
| 537 | 'NEW' REFERENCE! */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 538 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 539 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 540 | PyImport_AddModule(char *name) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 541 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 542 | PyObject *modules = PyImport_GetModuleDict(); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 543 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 544 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 545 | if ((m = PyDict_GetItemString(modules, name)) != NULL && |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 546 | PyModule_Check(m)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 547 | return m; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 548 | m = PyModule_New(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 549 | if (m == NULL) |
| 550 | return NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 551 | if (PyDict_SetItemString(modules, name, m) != 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 552 | Py_DECREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 553 | return NULL; |
| 554 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 555 | Py_DECREF(m); /* Yes, it still exists, in modules! */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 556 | |
| 557 | return m; |
| 558 | } |
| 559 | |
| 560 | |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 561 | /* Execute a code object in a module and return the module object |
| 562 | WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 563 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 564 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 565 | PyImport_ExecCodeModule(char *name, PyObject *co) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 566 | { |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 567 | return PyImport_ExecCodeModuleEx(name, co, (char *)NULL); |
| 568 | } |
| 569 | |
| 570 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 571 | PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname) |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 572 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 573 | PyObject *modules = PyImport_GetModuleDict(); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 574 | PyObject *m, *d, *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 575 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 576 | m = PyImport_AddModule(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 577 | if (m == NULL) |
| 578 | return NULL; |
Jeremy Hylton | ecd9129 | 2004-01-02 23:25:32 +0000 | [diff] [blame] | 579 | /* If the module is being reloaded, we get the old module back |
| 580 | and re-use its dict to exec the new code. */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 581 | d = PyModule_GetDict(m); |
| 582 | if (PyDict_GetItemString(d, "__builtins__") == NULL) { |
| 583 | if (PyDict_SetItemString(d, "__builtins__", |
| 584 | PyEval_GetBuiltins()) != 0) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 585 | return NULL; |
| 586 | } |
Guido van Rossum | 9c9a07c | 1996-05-16 20:43:40 +0000 | [diff] [blame] | 587 | /* Remember the filename as the __file__ attribute */ |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 588 | v = NULL; |
| 589 | if (pathname != NULL) { |
| 590 | v = PyString_FromString(pathname); |
| 591 | if (v == NULL) |
| 592 | PyErr_Clear(); |
| 593 | } |
| 594 | if (v == NULL) { |
| 595 | v = ((PyCodeObject *)co)->co_filename; |
| 596 | Py_INCREF(v); |
| 597 | } |
| 598 | if (PyDict_SetItemString(d, "__file__", v) != 0) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 599 | PyErr_Clear(); /* Not important enough to report */ |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 600 | Py_DECREF(v); |
| 601 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 602 | v = PyEval_EvalCode((PyCodeObject *)co, d, d); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 603 | if (v == NULL) |
| 604 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 605 | Py_DECREF(v); |
Guido van Rossum | b65e85c | 1997-07-10 18:00:45 +0000 | [diff] [blame] | 606 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 607 | if ((m = PyDict_GetItemString(modules, name)) == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 608 | PyErr_Format(PyExc_ImportError, |
| 609 | "Loaded module %.200s not found in sys.modules", |
| 610 | name); |
Guido van Rossum | b65e85c | 1997-07-10 18:00:45 +0000 | [diff] [blame] | 611 | return NULL; |
| 612 | } |
| 613 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 614 | Py_INCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 615 | |
| 616 | return m; |
| 617 | } |
| 618 | |
| 619 | |
| 620 | /* Given a pathname for a Python source file, fill a buffer with the |
| 621 | pathname for the corresponding compiled file. Return the pathname |
| 622 | for the compiled file, or NULL if there's no space in the buffer. |
| 623 | Doesn't set an exception. */ |
| 624 | |
| 625 | static char * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 626 | make_compiled_pathname(char *pathname, char *buf, size_t buflen) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 627 | { |
Tim Peters | c173137 | 2001-08-04 08:12:36 +0000 | [diff] [blame] | 628 | size_t len = strlen(pathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 629 | if (len+2 > buflen) |
| 630 | return NULL; |
Tim Peters | c173137 | 2001-08-04 08:12:36 +0000 | [diff] [blame] | 631 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 632 | #ifdef MS_WINDOWS |
Tim Peters | c173137 | 2001-08-04 08:12:36 +0000 | [diff] [blame] | 633 | /* Treat .pyw as if it were .py. The case of ".pyw" must match |
| 634 | that used in _PyImport_StandardFiletab. */ |
| 635 | if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0) |
| 636 | --len; /* pretend 'w' isn't there */ |
| 637 | #endif |
| 638 | memcpy(buf, pathname, len); |
| 639 | buf[len] = Py_OptimizeFlag ? 'o' : 'c'; |
| 640 | buf[len+1] = '\0'; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 641 | |
| 642 | return buf; |
| 643 | } |
| 644 | |
| 645 | |
| 646 | /* Given a pathname for a Python source file, its time of last |
| 647 | modification, and a pathname for a compiled file, check whether the |
| 648 | compiled file represents the same version of the source. If so, |
| 649 | return a FILE pointer for the compiled file, positioned just after |
| 650 | the header; if not, return NULL. |
| 651 | Doesn't set an exception. */ |
| 652 | |
| 653 | static FILE * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 654 | check_compiled_module(char *pathname, long mtime, char *cpathname) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 655 | { |
| 656 | FILE *fp; |
| 657 | long magic; |
| 658 | long pyc_mtime; |
| 659 | |
| 660 | fp = fopen(cpathname, "rb"); |
| 661 | if (fp == NULL) |
| 662 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 663 | magic = PyMarshal_ReadLongFromFile(fp); |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 664 | if (magic != pyc_magic) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 665 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 666 | PySys_WriteStderr("# %s has bad magic\n", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 667 | fclose(fp); |
| 668 | return NULL; |
| 669 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 670 | pyc_mtime = PyMarshal_ReadLongFromFile(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 671 | if (pyc_mtime != mtime) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 672 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 673 | PySys_WriteStderr("# %s has bad mtime\n", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 674 | fclose(fp); |
| 675 | return NULL; |
| 676 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 677 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 678 | PySys_WriteStderr("# %s matches %s\n", cpathname, pathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 679 | return fp; |
| 680 | } |
| 681 | |
| 682 | |
| 683 | /* Read a code object from a file and check it for validity */ |
| 684 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 685 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 686 | read_compiled_module(char *cpathname, FILE *fp) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 687 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 688 | PyObject *co; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 689 | |
Tim Peters | d9b9ac8 | 2001-01-28 00:27:39 +0000 | [diff] [blame] | 690 | co = PyMarshal_ReadLastObjectFromFile(fp); |
Armin Rigo | 01ab279 | 2004-03-26 15:09:27 +0000 | [diff] [blame] | 691 | if (co == NULL) |
| 692 | return NULL; |
| 693 | if (!PyCode_Check(co)) { |
| 694 | PyErr_Format(PyExc_ImportError, |
| 695 | "Non-code object in %.200s", cpathname); |
| 696 | Py_DECREF(co); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 697 | return NULL; |
| 698 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 699 | return (PyCodeObject *)co; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | |
| 703 | /* 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] | 704 | module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 705 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 706 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 707 | load_compiled_module(char *name, char *cpathname, FILE *fp) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 708 | { |
| 709 | long magic; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 710 | PyCodeObject *co; |
| 711 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 712 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 713 | magic = PyMarshal_ReadLongFromFile(fp); |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 714 | if (magic != pyc_magic) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 715 | PyErr_Format(PyExc_ImportError, |
| 716 | "Bad magic number in %.200s", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 717 | return NULL; |
| 718 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 719 | (void) PyMarshal_ReadLongFromFile(fp); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 720 | co = read_compiled_module(cpathname, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 721 | if (co == NULL) |
| 722 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 723 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 724 | PySys_WriteStderr("import %s # precompiled from %s\n", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 725 | name, cpathname); |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 726 | m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 727 | Py_DECREF(co); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 728 | |
| 729 | return m; |
| 730 | } |
| 731 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 732 | /* Parse a source file and return the corresponding code object */ |
| 733 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 734 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 735 | parse_source_module(char *pathname, FILE *fp) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 736 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 737 | PyCodeObject *co; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 738 | node *n; |
| 739 | |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 740 | n = PyParser_SimpleParseFile(fp, pathname, Py_file_input); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 741 | if (n == NULL) |
| 742 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 743 | co = PyNode_Compile(n, pathname); |
| 744 | PyNode_Free(n); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 745 | |
| 746 | return co; |
| 747 | } |
| 748 | |
| 749 | |
Guido van Rossum | 55a8338 | 2000-09-20 20:31:38 +0000 | [diff] [blame] | 750 | /* Helper to open a bytecode file for writing in exclusive mode */ |
| 751 | |
| 752 | static FILE * |
| 753 | open_exclusive(char *filename) |
| 754 | { |
| 755 | #if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC) |
| 756 | /* Use O_EXCL to avoid a race condition when another process tries to |
| 757 | write the same file. When that happens, our open() call fails, |
| 758 | which is just fine (since it's only a cache). |
| 759 | XXX If the file exists and is writable but the directory is not |
| 760 | writable, the file will never be written. Oh well. |
| 761 | */ |
| 762 | int fd; |
| 763 | (void) unlink(filename); |
Tim Peters | 42c83af | 2000-09-29 04:03:10 +0000 | [diff] [blame] | 764 | fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC |
| 765 | #ifdef O_BINARY |
| 766 | |O_BINARY /* necessary for Windows */ |
| 767 | #endif |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 768 | #ifdef __VMS |
| 769 | , 0666, "ctxt=bin", "shr=nil"); |
| 770 | #else |
| 771 | , 0666); |
| 772 | #endif |
Guido van Rossum | 55a8338 | 2000-09-20 20:31:38 +0000 | [diff] [blame] | 773 | if (fd < 0) |
| 774 | return NULL; |
| 775 | return fdopen(fd, "wb"); |
| 776 | #else |
| 777 | /* Best we can do -- on Windows this can't happen anyway */ |
| 778 | return fopen(filename, "wb"); |
| 779 | #endif |
| 780 | } |
| 781 | |
| 782 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 783 | /* Write a compiled module to a file, placing the time of last |
| 784 | modification of its source into the header. |
| 785 | Errors are ignored, if a write error occurs an attempt is made to |
| 786 | remove the file. */ |
| 787 | |
| 788 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 789 | write_compiled_module(PyCodeObject *co, char *cpathname, long mtime) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 790 | { |
| 791 | FILE *fp; |
| 792 | |
Guido van Rossum | 55a8338 | 2000-09-20 20:31:38 +0000 | [diff] [blame] | 793 | fp = open_exclusive(cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 794 | if (fp == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 795 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 796 | PySys_WriteStderr( |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 797 | "# can't create %s\n", cpathname); |
| 798 | return; |
| 799 | } |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 800 | PyMarshal_WriteLongToFile(pyc_magic, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 801 | /* First write a 0 for mtime */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 802 | PyMarshal_WriteLongToFile(0L, fp); |
| 803 | PyMarshal_WriteObjectToFile((PyObject *)co, fp); |
Armin Rigo | 01ab279 | 2004-03-26 15:09:27 +0000 | [diff] [blame] | 804 | if (fflush(fp) != 0 || ferror(fp)) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 805 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 806 | PySys_WriteStderr("# can't write %s\n", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 807 | /* Don't keep partial file */ |
| 808 | fclose(fp); |
| 809 | (void) unlink(cpathname); |
| 810 | return; |
| 811 | } |
| 812 | /* Now write the true mtime */ |
| 813 | fseek(fp, 4L, 0); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 814 | PyMarshal_WriteLongToFile(mtime, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 815 | fflush(fp); |
| 816 | fclose(fp); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 817 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 818 | PySys_WriteStderr("# wrote %s\n", cpathname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | |
| 822 | /* 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] | 823 | object WITH INCREMENTED REFERENCE COUNT. If there's a matching |
| 824 | byte-compiled file, use that instead. */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 825 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 826 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 827 | load_source_module(char *name, char *pathname, FILE *fp) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 828 | { |
Fred Drake | 4c82b23 | 2000-06-30 16:18:57 +0000 | [diff] [blame] | 829 | time_t mtime; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 830 | FILE *fpc; |
| 831 | char buf[MAXPATHLEN+1]; |
| 832 | char *cpathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 833 | PyCodeObject *co; |
| 834 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 835 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 836 | mtime = PyOS_GetLastModificationTime(pathname, fp); |
Fred Drake | c63d3e9 | 2001-03-01 06:33:32 +0000 | [diff] [blame] | 837 | if (mtime == (time_t)(-1)) |
Fred Drake | 4c82b23 | 2000-06-30 16:18:57 +0000 | [diff] [blame] | 838 | return NULL; |
| 839 | #if SIZEOF_TIME_T > 4 |
| 840 | /* Python's .pyc timestamp handling presumes that the timestamp fits |
| 841 | in 4 bytes. This will be fine until sometime in the year 2038, |
| 842 | when a 4-byte signed time_t will overflow. |
| 843 | */ |
| 844 | if (mtime >> 32) { |
| 845 | PyErr_SetString(PyExc_OverflowError, |
Fred Drake | c63d3e9 | 2001-03-01 06:33:32 +0000 | [diff] [blame] | 846 | "modification time overflows a 4 byte field"); |
Fred Drake | 4c82b23 | 2000-06-30 16:18:57 +0000 | [diff] [blame] | 847 | return NULL; |
| 848 | } |
| 849 | #endif |
Tim Peters | 36515e2 | 2001-11-18 04:06:29 +0000 | [diff] [blame] | 850 | cpathname = make_compiled_pathname(pathname, buf, |
Jeremy Hylton | 37832f0 | 2001-04-13 17:50:20 +0000 | [diff] [blame] | 851 | (size_t)MAXPATHLEN + 1); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 852 | if (cpathname != NULL && |
| 853 | (fpc = check_compiled_module(pathname, mtime, cpathname))) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 854 | co = read_compiled_module(cpathname, fpc); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 855 | fclose(fpc); |
| 856 | if (co == NULL) |
| 857 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 858 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 859 | PySys_WriteStderr("import %s # precompiled from %s\n", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 860 | name, cpathname); |
Guido van Rossum | afd3dae | 1998-08-25 18:44:34 +0000 | [diff] [blame] | 861 | pathname = cpathname; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 862 | } |
| 863 | else { |
| 864 | co = parse_source_module(pathname, fp); |
| 865 | if (co == NULL) |
| 866 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 867 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 868 | PySys_WriteStderr("import %s # from %s\n", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 869 | name, pathname); |
| 870 | write_compiled_module(co, cpathname, mtime); |
| 871 | } |
Guido van Rossum | afd3dae | 1998-08-25 18:44:34 +0000 | [diff] [blame] | 872 | m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 873 | Py_DECREF(co); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 874 | |
| 875 | return m; |
| 876 | } |
| 877 | |
| 878 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 879 | /* Forward */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 880 | static PyObject *load_module(char *, FILE *, char *, int, PyObject *); |
| 881 | static struct filedescr *find_module(char *, char *, PyObject *, |
| 882 | char *, size_t, FILE **, PyObject **); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 883 | static struct _frozen *find_frozen(char *name); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 884 | |
| 885 | /* Load a package and return its module object WITH INCREMENTED |
| 886 | REFERENCE COUNT */ |
| 887 | |
| 888 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 889 | load_package(char *name, char *pathname) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 890 | { |
| 891 | PyObject *m, *d, *file, *path; |
| 892 | int err; |
| 893 | char buf[MAXPATHLEN+1]; |
| 894 | FILE *fp = NULL; |
| 895 | struct filedescr *fdp; |
| 896 | |
| 897 | m = PyImport_AddModule(name); |
| 898 | if (m == NULL) |
| 899 | return NULL; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 900 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 901 | PySys_WriteStderr("import %s # directory %s\n", |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 902 | name, pathname); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 903 | d = PyModule_GetDict(m); |
| 904 | file = PyString_FromString(pathname); |
| 905 | if (file == NULL) |
| 906 | return NULL; |
| 907 | path = Py_BuildValue("[O]", file); |
| 908 | if (path == NULL) { |
| 909 | Py_DECREF(file); |
| 910 | return NULL; |
| 911 | } |
| 912 | err = PyDict_SetItemString(d, "__file__", file); |
| 913 | if (err == 0) |
| 914 | err = PyDict_SetItemString(d, "__path__", path); |
| 915 | if (err != 0) { |
| 916 | m = NULL; |
| 917 | goto cleanup; |
| 918 | } |
| 919 | buf[0] = '\0'; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 920 | fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 921 | if (fdp == NULL) { |
| 922 | if (PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 923 | PyErr_Clear(); |
| 924 | } |
| 925 | else |
| 926 | m = NULL; |
| 927 | goto cleanup; |
| 928 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 929 | m = load_module(name, fp, buf, fdp->type, NULL); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 930 | if (fp != NULL) |
| 931 | fclose(fp); |
| 932 | cleanup: |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 933 | Py_XDECREF(path); |
| 934 | Py_XDECREF(file); |
| 935 | return m; |
| 936 | } |
| 937 | |
| 938 | |
| 939 | /* Helper to test for built-in module */ |
| 940 | |
| 941 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 942 | is_builtin(char *name) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 943 | { |
| 944 | int i; |
Guido van Rossum | 771c6c8 | 1997-10-31 18:37:24 +0000 | [diff] [blame] | 945 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) { |
| 946 | if (strcmp(name, PyImport_Inittab[i].name) == 0) { |
| 947 | if (PyImport_Inittab[i].initfunc == NULL) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 948 | return -1; |
| 949 | else |
| 950 | return 1; |
| 951 | } |
| 952 | } |
| 953 | return 0; |
| 954 | } |
| 955 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 956 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 957 | /* Return an importer object for a sys.path/pkg.__path__ item 'p', |
| 958 | possibly by fetching it from the path_importer_cache dict. If it |
| 959 | wasn't yet cached, traverse path_hooks until it a hook is found |
| 960 | that can handle the path item. Return None if no hook could; |
| 961 | this tells our caller it should fall back to the builtin |
| 962 | import mechanism. Cache the result in path_importer_cache. |
| 963 | Returns a borrowed reference. */ |
| 964 | |
| 965 | static PyObject * |
| 966 | get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks, |
| 967 | PyObject *p) |
| 968 | { |
| 969 | PyObject *importer; |
| 970 | int j, nhooks; |
| 971 | |
| 972 | /* These conditions are the caller's responsibility: */ |
| 973 | assert(PyList_Check(path_hooks)); |
| 974 | assert(PyDict_Check(path_importer_cache)); |
| 975 | |
| 976 | nhooks = PyList_Size(path_hooks); |
| 977 | if (nhooks < 0) |
| 978 | return NULL; /* Shouldn't happen */ |
| 979 | |
| 980 | importer = PyDict_GetItem(path_importer_cache, p); |
| 981 | if (importer != NULL) |
| 982 | return importer; |
| 983 | |
| 984 | /* set path_importer_cache[p] to None to avoid recursion */ |
| 985 | if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0) |
| 986 | return NULL; |
| 987 | |
| 988 | for (j = 0; j < nhooks; j++) { |
| 989 | PyObject *hook = PyList_GetItem(path_hooks, j); |
| 990 | if (hook == NULL) |
| 991 | return NULL; |
| 992 | importer = PyObject_CallFunction(hook, "O", p); |
| 993 | if (importer != NULL) |
| 994 | break; |
| 995 | |
| 996 | if (!PyErr_ExceptionMatches(PyExc_ImportError)) { |
| 997 | return NULL; |
| 998 | } |
| 999 | PyErr_Clear(); |
| 1000 | } |
| 1001 | if (importer == NULL) |
| 1002 | importer = Py_None; |
| 1003 | else if (importer != Py_None) { |
| 1004 | int err = PyDict_SetItem(path_importer_cache, p, importer); |
| 1005 | Py_DECREF(importer); |
| 1006 | if (err != 0) |
| 1007 | return NULL; |
| 1008 | } |
| 1009 | return importer; |
| 1010 | } |
| 1011 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1012 | /* Search the path (default sys.path) for a module. Return the |
| 1013 | corresponding filedescr struct, and (via return arguments) the |
| 1014 | pathname and an open file. Return NULL if the module is not found. */ |
| 1015 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1016 | #ifdef MS_COREDLL |
Thomas Wouters | b4bd21c | 2000-07-22 23:38:01 +0000 | [diff] [blame] | 1017 | extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **, |
| 1018 | char *, int); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1019 | #endif |
| 1020 | |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1021 | static int case_ok(char *, int, int, char *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 1022 | static int find_init_module(char *); /* Forward */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1023 | static struct filedescr importhookdescr = {"", "", IMP_HOOK}; |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1024 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1025 | static struct filedescr * |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1026 | find_module(char *fullname, char *subname, PyObject *path, char *buf, |
| 1027 | size_t buflen, FILE **p_fp, PyObject **p_loader) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1028 | { |
Fred Drake | 4c82b23 | 2000-06-30 16:18:57 +0000 | [diff] [blame] | 1029 | int i, npath; |
| 1030 | size_t len, namelen; |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1031 | struct filedescr *fdp = NULL; |
Jack Jansen | c88da1f | 2002-05-28 10:58:19 +0000 | [diff] [blame] | 1032 | char *filemode; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1033 | FILE *fp = NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1034 | PyObject *path_hooks, *path_importer_cache; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1035 | #ifndef RISCOS |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1036 | struct stat statbuf; |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1037 | #endif |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1038 | static struct filedescr fd_frozen = {"", "", PY_FROZEN}; |
| 1039 | static struct filedescr fd_builtin = {"", "", C_BUILTIN}; |
| 1040 | static struct filedescr fd_package = {"", "", PKG_DIRECTORY}; |
Guido van Rossum | 0506a43 | 1998-08-11 15:07:39 +0000 | [diff] [blame] | 1041 | char name[MAXPATHLEN+1]; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1042 | #if defined(PYOS_OS2) |
| 1043 | size_t saved_len; |
| 1044 | size_t saved_namelen; |
| 1045 | char *saved_buf = NULL; |
| 1046 | #endif |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1047 | if (p_loader != NULL) |
| 1048 | *p_loader = NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1049 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1050 | if (strlen(subname) > MAXPATHLEN) { |
Guido van Rossum | 8f4d331 | 2001-10-18 18:54:11 +0000 | [diff] [blame] | 1051 | PyErr_SetString(PyExc_OverflowError, |
| 1052 | "module name is too long"); |
Fred Drake | 4c82b23 | 2000-06-30 16:18:57 +0000 | [diff] [blame] | 1053 | return NULL; |
| 1054 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1055 | strcpy(name, subname); |
| 1056 | |
| 1057 | /* sys.meta_path import hook */ |
| 1058 | if (p_loader != NULL) { |
| 1059 | PyObject *meta_path; |
| 1060 | |
| 1061 | meta_path = PySys_GetObject("meta_path"); |
| 1062 | if (meta_path == NULL || !PyList_Check(meta_path)) { |
| 1063 | PyErr_SetString(PyExc_ImportError, |
| 1064 | "sys.meta_path must be a list of " |
| 1065 | "import hooks"); |
| 1066 | return NULL; |
| 1067 | } |
| 1068 | Py_INCREF(meta_path); /* zap guard */ |
| 1069 | npath = PyList_Size(meta_path); |
| 1070 | for (i = 0; i < npath; i++) { |
| 1071 | PyObject *loader; |
| 1072 | PyObject *hook = PyList_GetItem(meta_path, i); |
| 1073 | loader = PyObject_CallMethod(hook, "find_module", |
| 1074 | "sO", fullname, |
| 1075 | path != NULL ? |
| 1076 | path : Py_None); |
| 1077 | if (loader == NULL) { |
| 1078 | Py_DECREF(meta_path); |
| 1079 | return NULL; /* true error */ |
| 1080 | } |
| 1081 | if (loader != Py_None) { |
| 1082 | /* a loader was found */ |
| 1083 | *p_loader = loader; |
| 1084 | Py_DECREF(meta_path); |
| 1085 | return &importhookdescr; |
| 1086 | } |
| 1087 | Py_DECREF(loader); |
| 1088 | } |
| 1089 | Py_DECREF(meta_path); |
| 1090 | } |
Guido van Rossum | 0506a43 | 1998-08-11 15:07:39 +0000 | [diff] [blame] | 1091 | |
| 1092 | if (path != NULL && PyString_Check(path)) { |
Guido van Rossum | 8f4d331 | 2001-10-18 18:54:11 +0000 | [diff] [blame] | 1093 | /* The only type of submodule allowed inside a "frozen" |
| 1094 | package are other frozen modules or packages. */ |
Guido van Rossum | 0506a43 | 1998-08-11 15:07:39 +0000 | [diff] [blame] | 1095 | if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) { |
| 1096 | PyErr_SetString(PyExc_ImportError, |
| 1097 | "full frozen module name too long"); |
| 1098 | return NULL; |
| 1099 | } |
| 1100 | strcpy(buf, PyString_AsString(path)); |
| 1101 | strcat(buf, "."); |
| 1102 | strcat(buf, name); |
| 1103 | strcpy(name, buf); |
Guido van Rossum | 8f4d331 | 2001-10-18 18:54:11 +0000 | [diff] [blame] | 1104 | if (find_frozen(name) != NULL) { |
| 1105 | strcpy(buf, name); |
| 1106 | return &fd_frozen; |
| 1107 | } |
| 1108 | PyErr_Format(PyExc_ImportError, |
| 1109 | "No frozen submodule named %.200s", name); |
| 1110 | return NULL; |
Guido van Rossum | 0506a43 | 1998-08-11 15:07:39 +0000 | [diff] [blame] | 1111 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1112 | if (path == NULL) { |
| 1113 | if (is_builtin(name)) { |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1114 | strcpy(buf, name); |
| 1115 | return &fd_builtin; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1116 | } |
Greg Ward | 201baee | 2001-10-04 14:52:06 +0000 | [diff] [blame] | 1117 | if ((find_frozen(name)) != NULL) { |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1118 | strcpy(buf, name); |
| 1119 | return &fd_frozen; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1120 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1121 | |
Guido van Rossum | ac27910 | 1996-08-22 23:10:58 +0000 | [diff] [blame] | 1122 | #ifdef MS_COREDLL |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1123 | fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen); |
| 1124 | if (fp != NULL) { |
| 1125 | *p_fp = fp; |
| 1126 | return fdp; |
| 1127 | } |
Guido van Rossum | a5a3db7 | 1996-04-09 02:39:59 +0000 | [diff] [blame] | 1128 | #endif |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1129 | path = PySys_GetObject("path"); |
| 1130 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1131 | if (path == NULL || !PyList_Check(path)) { |
| 1132 | PyErr_SetString(PyExc_ImportError, |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1133 | "sys.path must be a list of directory names"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1134 | return NULL; |
| 1135 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1136 | |
| 1137 | path_hooks = PySys_GetObject("path_hooks"); |
| 1138 | if (path_hooks == NULL || !PyList_Check(path_hooks)) { |
| 1139 | PyErr_SetString(PyExc_ImportError, |
| 1140 | "sys.path_hooks must be a list of " |
| 1141 | "import hooks"); |
| 1142 | return NULL; |
| 1143 | } |
| 1144 | path_importer_cache = PySys_GetObject("path_importer_cache"); |
| 1145 | if (path_importer_cache == NULL || |
| 1146 | !PyDict_Check(path_importer_cache)) { |
| 1147 | PyErr_SetString(PyExc_ImportError, |
| 1148 | "sys.path_importer_cache must be a dict"); |
| 1149 | return NULL; |
| 1150 | } |
| 1151 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1152 | npath = PyList_Size(path); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1153 | namelen = strlen(name); |
| 1154 | for (i = 0; i < npath; i++) { |
Walter Dörwald | 3430d70 | 2002-06-17 10:43:59 +0000 | [diff] [blame] | 1155 | PyObject *copy = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1156 | PyObject *v = PyList_GetItem(path, i); |
Walter Dörwald | 3430d70 | 2002-06-17 10:43:59 +0000 | [diff] [blame] | 1157 | #ifdef Py_USING_UNICODE |
| 1158 | if (PyUnicode_Check(v)) { |
| 1159 | copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v), |
| 1160 | PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL); |
| 1161 | if (copy == NULL) |
| 1162 | return NULL; |
| 1163 | v = copy; |
| 1164 | } |
| 1165 | else |
| 1166 | #endif |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1167 | if (!PyString_Check(v)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1168 | continue; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1169 | len = PyString_Size(v); |
Walter Dörwald | 3430d70 | 2002-06-17 10:43:59 +0000 | [diff] [blame] | 1170 | if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) { |
| 1171 | Py_XDECREF(copy); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1172 | continue; /* Too long */ |
Walter Dörwald | 3430d70 | 2002-06-17 10:43:59 +0000 | [diff] [blame] | 1173 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1174 | strcpy(buf, PyString_AsString(v)); |
Walter Dörwald | 3430d70 | 2002-06-17 10:43:59 +0000 | [diff] [blame] | 1175 | if (strlen(buf) != len) { |
| 1176 | Py_XDECREF(copy); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1177 | continue; /* v contains '\0' */ |
Walter Dörwald | 3430d70 | 2002-06-17 10:43:59 +0000 | [diff] [blame] | 1178 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1179 | |
| 1180 | /* sys.path_hooks import hook */ |
| 1181 | if (p_loader != NULL) { |
| 1182 | PyObject *importer; |
| 1183 | |
| 1184 | importer = get_path_importer(path_importer_cache, |
| 1185 | path_hooks, v); |
| 1186 | if (importer == NULL) |
| 1187 | return NULL; |
| 1188 | /* Note: importer is a borrowed reference */ |
| 1189 | if (importer != Py_None) { |
| 1190 | PyObject *loader; |
| 1191 | loader = PyObject_CallMethod(importer, |
| 1192 | "find_module", |
| 1193 | "s", fullname); |
| 1194 | if (loader == NULL) |
| 1195 | return NULL; /* error */ |
| 1196 | if (loader != Py_None) { |
| 1197 | /* a loader was found */ |
| 1198 | *p_loader = loader; |
| 1199 | return &importhookdescr; |
| 1200 | } |
| 1201 | Py_DECREF(loader); |
| 1202 | } |
| 1203 | /* no hook was successful, use builtin import */ |
| 1204 | } |
| 1205 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1206 | if (len > 0 && buf[len-1] != SEP |
| 1207 | #ifdef ALTSEP |
| 1208 | && buf[len-1] != ALTSEP |
| 1209 | #endif |
| 1210 | ) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1211 | buf[len++] = SEP; |
Guido van Rossum | 215c340 | 2000-11-13 17:26:32 +0000 | [diff] [blame] | 1212 | strcpy(buf+len, name); |
| 1213 | len += namelen; |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1214 | |
| 1215 | /* Check for package import (buf holds a directory name, |
| 1216 | and there's an __init__ module in that directory */ |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1217 | #ifdef HAVE_STAT |
Walter Dörwald | 3430d70 | 2002-06-17 10:43:59 +0000 | [diff] [blame] | 1218 | if (stat(buf, &statbuf) == 0 && /* it exists */ |
| 1219 | S_ISDIR(statbuf.st_mode) && /* it's a directory */ |
| 1220 | find_init_module(buf) && /* it has __init__.py */ |
| 1221 | case_ok(buf, len, namelen, name)) { /* and case matches */ |
| 1222 | Py_XDECREF(copy); |
Tim Peters | cab3f68 | 2001-04-29 22:21:25 +0000 | [diff] [blame] | 1223 | return &fd_package; |
Walter Dörwald | 3430d70 | 2002-06-17 10:43:59 +0000 | [diff] [blame] | 1224 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1225 | #else |
| 1226 | /* XXX How are you going to test for directories? */ |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1227 | #ifdef RISCOS |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 1228 | if (isdir(buf) && |
| 1229 | find_init_module(buf) && |
Walter Dörwald | 3430d70 | 2002-06-17 10:43:59 +0000 | [diff] [blame] | 1230 | case_ok(buf, len, namelen, name)) { |
| 1231 | Py_XDECREF(copy); |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 1232 | return &fd_package; |
Walter Dörwald | 3430d70 | 2002-06-17 10:43:59 +0000 | [diff] [blame] | 1233 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1234 | #endif |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1235 | #endif |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1236 | #if defined(PYOS_OS2) |
| 1237 | /* take a snapshot of the module spec for restoration |
| 1238 | * after the 8 character DLL hackery |
| 1239 | */ |
| 1240 | saved_buf = strdup(buf); |
| 1241 | saved_len = len; |
| 1242 | saved_namelen = namelen; |
| 1243 | #endif /* PYOS_OS2 */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1244 | for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1245 | #if defined(PYOS_OS2) |
Jeremy Hylton | 4ae6fae | 2002-05-30 17:15:25 +0000 | [diff] [blame] | 1246 | /* OS/2 limits DLLs to 8 character names (w/o |
| 1247 | extension) |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1248 | * so if the name is longer than that and its a |
| 1249 | * dynamically loaded module we're going to try, |
| 1250 | * truncate the name before trying |
| 1251 | */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1252 | if (strlen(subname) > 8) { |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1253 | /* is this an attempt to load a C extension? */ |
Jeremy Hylton | 4ae6fae | 2002-05-30 17:15:25 +0000 | [diff] [blame] | 1254 | const struct filedescr *scan; |
| 1255 | scan = _PyImport_DynLoadFiletab; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1256 | while (scan->suffix != NULL) { |
Jeremy Hylton | 4ae6fae | 2002-05-30 17:15:25 +0000 | [diff] [blame] | 1257 | if (!strcmp(scan->suffix, fdp->suffix)) |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1258 | break; |
| 1259 | else |
| 1260 | scan++; |
| 1261 | } |
| 1262 | if (scan->suffix != NULL) { |
| 1263 | /* yes, so truncate the name */ |
| 1264 | namelen = 8; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1265 | len -= strlen(subname) - namelen; |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1266 | buf[len] = '\0'; |
| 1267 | } |
| 1268 | } |
| 1269 | #endif /* PYOS_OS2 */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1270 | strcpy(buf+len, fdp->suffix); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1271 | if (Py_VerboseFlag > 1) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 1272 | PySys_WriteStderr("# trying %s\n", buf); |
Jack Jansen | c88da1f | 2002-05-28 10:58:19 +0000 | [diff] [blame] | 1273 | filemode = fdp->mode; |
Jeremy Hylton | 4ae6fae | 2002-05-30 17:15:25 +0000 | [diff] [blame] | 1274 | if (filemode[0] == 'U') |
| 1275 | filemode = "r" PY_STDIOTEXTMODE; |
Jack Jansen | c88da1f | 2002-05-28 10:58:19 +0000 | [diff] [blame] | 1276 | fp = fopen(buf, filemode); |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1277 | if (fp != NULL) { |
| 1278 | if (case_ok(buf, len, namelen, name)) |
| 1279 | break; |
| 1280 | else { /* continue search */ |
| 1281 | fclose(fp); |
| 1282 | fp = NULL; |
Barry Warsaw | 914a0b1 | 2001-02-02 19:12:16 +0000 | [diff] [blame] | 1283 | } |
Barry Warsaw | 914a0b1 | 2001-02-02 19:12:16 +0000 | [diff] [blame] | 1284 | } |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1285 | #if defined(PYOS_OS2) |
| 1286 | /* restore the saved snapshot */ |
| 1287 | strcpy(buf, saved_buf); |
| 1288 | len = saved_len; |
| 1289 | namelen = saved_namelen; |
| 1290 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1291 | } |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1292 | #if defined(PYOS_OS2) |
| 1293 | /* don't need/want the module name snapshot anymore */ |
| 1294 | if (saved_buf) |
| 1295 | { |
| 1296 | free(saved_buf); |
| 1297 | saved_buf = NULL; |
| 1298 | } |
| 1299 | #endif |
Walter Dörwald | 3430d70 | 2002-06-17 10:43:59 +0000 | [diff] [blame] | 1300 | Py_XDECREF(copy); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1301 | if (fp != NULL) |
| 1302 | break; |
| 1303 | } |
| 1304 | if (fp == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1305 | PyErr_Format(PyExc_ImportError, |
| 1306 | "No module named %.200s", name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1307 | return NULL; |
| 1308 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1309 | *p_fp = fp; |
| 1310 | return fdp; |
| 1311 | } |
| 1312 | |
Tim Peters | d1e87a8 | 2001-03-01 18:12:00 +0000 | [diff] [blame] | 1313 | /* case_ok(char* buf, int len, int namelen, char* name) |
| 1314 | * The arguments here are tricky, best shown by example: |
| 1315 | * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0 |
| 1316 | * ^ ^ ^ ^ |
| 1317 | * |--------------------- buf ---------------------| |
| 1318 | * |------------------- len ------------------| |
| 1319 | * |------ name -------| |
| 1320 | * |----- namelen -----| |
| 1321 | * buf is the full path, but len only counts up to (& exclusive of) the |
| 1322 | * extension. name is the module name, also exclusive of extension. |
| 1323 | * |
| 1324 | * We've already done a successful stat() or fopen() on buf, so know that |
| 1325 | * there's some match, possibly case-insensitive. |
| 1326 | * |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1327 | * case_ok() is to return 1 if there's a case-sensitive match for |
| 1328 | * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK |
| 1329 | * exists. |
Tim Peters | d1e87a8 | 2001-03-01 18:12:00 +0000 | [diff] [blame] | 1330 | * |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1331 | * case_ok() is used to implement case-sensitive import semantics even |
| 1332 | * on platforms with case-insensitive filesystems. It's trivial to implement |
| 1333 | * for case-sensitive filesystems. It's pretty much a cross-platform |
| 1334 | * nightmare for systems with case-insensitive filesystems. |
| 1335 | */ |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1336 | |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1337 | /* First we may need a pile of platform-specific header files; the sequence |
| 1338 | * of #if's here should match the sequence in the body of case_ok(). |
| 1339 | */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1340 | #if defined(MS_WINDOWS) || defined(__CYGWIN__) |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1341 | #include <windows.h> |
Guido van Rossum | 4c3f57c | 2001-01-10 20:40:46 +0000 | [diff] [blame] | 1342 | #ifdef __CYGWIN__ |
| 1343 | #include <sys/cygwin.h> |
| 1344 | #endif |
| 1345 | |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1346 | #elif defined(DJGPP) |
| 1347 | #include <dir.h> |
| 1348 | |
Tim Peters | d1e87a8 | 2001-03-01 18:12:00 +0000 | [diff] [blame] | 1349 | #elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H) |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1350 | #include <sys/types.h> |
| 1351 | #include <dirent.h> |
| 1352 | |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1353 | #elif defined(PYOS_OS2) |
| 1354 | #define INCL_DOS |
| 1355 | #define INCL_DOSERRORS |
| 1356 | #define INCL_NOPMAPI |
| 1357 | #include <os2.h> |
| 1358 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 1359 | #elif defined(RISCOS) |
| 1360 | #include "oslib/osfscontrol.h" |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1361 | #endif |
| 1362 | |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1363 | static int |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1364 | case_ok(char *buf, int len, int namelen, char *name) |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1365 | { |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1366 | /* Pick a platform-specific implementation; the sequence of #if's here should |
| 1367 | * match the sequence just above. |
| 1368 | */ |
| 1369 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1370 | /* MS_WINDOWS || __CYGWIN__ */ |
| 1371 | #if defined(MS_WINDOWS) || defined(__CYGWIN__) |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1372 | WIN32_FIND_DATA data; |
| 1373 | HANDLE h; |
Guido van Rossum | 4c3f57c | 2001-01-10 20:40:46 +0000 | [diff] [blame] | 1374 | #ifdef __CYGWIN__ |
| 1375 | char tempbuf[MAX_PATH]; |
| 1376 | #endif |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1377 | |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 1378 | if (Py_GETENV("PYTHONCASEOK") != NULL) |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1379 | return 1; |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1380 | |
Guido van Rossum | 4c3f57c | 2001-01-10 20:40:46 +0000 | [diff] [blame] | 1381 | #ifdef __CYGWIN__ |
| 1382 | cygwin32_conv_to_win32_path(buf, tempbuf); |
| 1383 | h = FindFirstFile(tempbuf, &data); |
| 1384 | #else |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1385 | h = FindFirstFile(buf, &data); |
Guido van Rossum | 4c3f57c | 2001-01-10 20:40:46 +0000 | [diff] [blame] | 1386 | #endif |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1387 | if (h == INVALID_HANDLE_VALUE) { |
| 1388 | PyErr_Format(PyExc_NameError, |
| 1389 | "Can't find file for module %.100s\n(filename %.300s)", |
| 1390 | name, buf); |
| 1391 | return 0; |
| 1392 | } |
| 1393 | FindClose(h); |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1394 | return strncmp(data.cFileName, name, namelen) == 0; |
| 1395 | |
| 1396 | /* DJGPP */ |
| 1397 | #elif defined(DJGPP) |
| 1398 | struct ffblk ffblk; |
| 1399 | int done; |
| 1400 | |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 1401 | if (Py_GETENV("PYTHONCASEOK") != NULL) |
Guido van Rossum | 323bf5e | 1998-06-24 03:54:06 +0000 | [diff] [blame] | 1402 | return 1; |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1403 | |
| 1404 | done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC); |
| 1405 | if (done) { |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1406 | PyErr_Format(PyExc_NameError, |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1407 | "Can't find file for module %.100s\n(filename %.300s)", |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1408 | name, buf); |
| 1409 | return 0; |
| 1410 | } |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1411 | return strncmp(ffblk.ff_name, name, namelen) == 0; |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1412 | |
Tim Peters | 677898a | 2001-03-02 03:28:03 +0000 | [diff] [blame] | 1413 | /* new-fangled macintosh (macosx) */ |
Tim Peters | d1e87a8 | 2001-03-01 18:12:00 +0000 | [diff] [blame] | 1414 | #elif defined(__MACH__) && defined(__APPLE__) && defined(HAVE_DIRENT_H) |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1415 | DIR *dirp; |
| 1416 | struct dirent *dp; |
Tim Peters | d1e87a8 | 2001-03-01 18:12:00 +0000 | [diff] [blame] | 1417 | char dirname[MAXPATHLEN + 1]; |
| 1418 | const int dirlen = len - namelen - 1; /* don't want trailing SEP */ |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1419 | |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 1420 | if (Py_GETENV("PYTHONCASEOK") != NULL) |
Tim Peters | dbe6ebb | 2001-03-01 08:47:29 +0000 | [diff] [blame] | 1421 | return 1; |
| 1422 | |
Tim Peters | d1e87a8 | 2001-03-01 18:12:00 +0000 | [diff] [blame] | 1423 | /* Copy the dir component into dirname; substitute "." if empty */ |
| 1424 | if (dirlen <= 0) { |
| 1425 | dirname[0] = '.'; |
| 1426 | dirname[1] = '\0'; |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1427 | } |
| 1428 | else { |
Tim Peters | d1e87a8 | 2001-03-01 18:12:00 +0000 | [diff] [blame] | 1429 | assert(dirlen <= MAXPATHLEN); |
| 1430 | memcpy(dirname, buf, dirlen); |
| 1431 | dirname[dirlen] = '\0'; |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1432 | } |
| 1433 | /* Open the directory and search the entries for an exact match. */ |
Tim Peters | d1e87a8 | 2001-03-01 18:12:00 +0000 | [diff] [blame] | 1434 | dirp = opendir(dirname); |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1435 | if (dirp) { |
Tim Peters | 677898a | 2001-03-02 03:28:03 +0000 | [diff] [blame] | 1436 | char *nameWithExt = buf + len - namelen; |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1437 | while ((dp = readdir(dirp)) != NULL) { |
Tim Peters | dbe6ebb | 2001-03-01 08:47:29 +0000 | [diff] [blame] | 1438 | const int thislen = |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1439 | #ifdef _DIRENT_HAVE_D_NAMELEN |
Tim Peters | dbe6ebb | 2001-03-01 08:47:29 +0000 | [diff] [blame] | 1440 | dp->d_namlen; |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1441 | #else |
Tim Peters | dbe6ebb | 2001-03-01 08:47:29 +0000 | [diff] [blame] | 1442 | strlen(dp->d_name); |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1443 | #endif |
Tim Peters | d1e87a8 | 2001-03-01 18:12:00 +0000 | [diff] [blame] | 1444 | if (thislen >= namelen && |
Tim Peters | 677898a | 2001-03-02 03:28:03 +0000 | [diff] [blame] | 1445 | strcmp(dp->d_name, nameWithExt) == 0) { |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1446 | (void)closedir(dirp); |
| 1447 | return 1; /* Found */ |
| 1448 | } |
| 1449 | } |
Tim Peters | dbe6ebb | 2001-03-01 08:47:29 +0000 | [diff] [blame] | 1450 | (void)closedir(dirp); |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1451 | } |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1452 | return 0 ; /* Not found */ |
Tim Peters | 430f5d4 | 2001-03-01 01:30:56 +0000 | [diff] [blame] | 1453 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 1454 | /* RISC OS */ |
| 1455 | #elif defined(RISCOS) |
| 1456 | char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */ |
| 1457 | char buf2[MAXPATHLEN+2]; |
| 1458 | char *nameWithExt = buf+len-namelen; |
| 1459 | int canonlen; |
| 1460 | os_error *e; |
| 1461 | |
| 1462 | if (Py_GETENV("PYTHONCASEOK") != NULL) |
| 1463 | return 1; |
| 1464 | |
| 1465 | /* workaround: |
| 1466 | append wildcard, otherwise case of filename wouldn't be touched */ |
| 1467 | strcpy(buf2, buf); |
| 1468 | strcat(buf2, "*"); |
| 1469 | |
| 1470 | e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen); |
| 1471 | canonlen = MAXPATHLEN+1-canonlen; |
| 1472 | if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) ) |
| 1473 | return 0; |
| 1474 | if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0) |
| 1475 | return 1; /* match */ |
| 1476 | |
| 1477 | return 0; |
| 1478 | |
Andrew MacIntyre | d940054 | 2002-02-26 11:41:34 +0000 | [diff] [blame] | 1479 | /* OS/2 */ |
| 1480 | #elif defined(PYOS_OS2) |
| 1481 | HDIR hdir = 1; |
| 1482 | ULONG srchcnt = 1; |
| 1483 | FILEFINDBUF3 ffbuf; |
| 1484 | APIRET rc; |
| 1485 | |
| 1486 | if (getenv("PYTHONCASEOK") != NULL) |
| 1487 | return 1; |
| 1488 | |
| 1489 | rc = DosFindFirst(buf, |
| 1490 | &hdir, |
| 1491 | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY, |
| 1492 | &ffbuf, sizeof(ffbuf), |
| 1493 | &srchcnt, |
| 1494 | FIL_STANDARD); |
| 1495 | if (rc != NO_ERROR) |
| 1496 | return 0; |
| 1497 | return strncmp(ffbuf.achName, name, namelen) == 0; |
| 1498 | |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1499 | /* assuming it's a case-sensitive filesystem, so there's nothing to do! */ |
| 1500 | #else |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1501 | return 1; |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1502 | |
Guido van Rossum | 4d1b3b9 | 1998-02-13 23:27:59 +0000 | [diff] [blame] | 1503 | #endif |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 1504 | } |
Guido van Rossum | 4d1b3b9 | 1998-02-13 23:27:59 +0000 | [diff] [blame] | 1505 | |
Guido van Rossum | 0980bd9 | 1998-02-13 17:18:36 +0000 | [diff] [blame] | 1506 | |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1507 | #ifdef HAVE_STAT |
| 1508 | /* Helper to look for __init__.py or __init__.py[co] in potential package */ |
| 1509 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1510 | find_init_module(char *buf) |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1511 | { |
Tim Peters | 0f9431f | 2001-07-05 03:47:53 +0000 | [diff] [blame] | 1512 | const size_t save_len = strlen(buf); |
Fred Drake | 4c82b23 | 2000-06-30 16:18:57 +0000 | [diff] [blame] | 1513 | size_t i = save_len; |
Tim Peters | 0f9431f | 2001-07-05 03:47:53 +0000 | [diff] [blame] | 1514 | char *pname; /* pointer to start of __init__ */ |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1515 | struct stat statbuf; |
| 1516 | |
Tim Peters | 0f9431f | 2001-07-05 03:47:53 +0000 | [diff] [blame] | 1517 | /* For calling case_ok(buf, len, namelen, name): |
| 1518 | * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0 |
| 1519 | * ^ ^ ^ ^ |
| 1520 | * |--------------------- buf ---------------------| |
| 1521 | * |------------------- len ------------------| |
| 1522 | * |------ name -------| |
| 1523 | * |----- namelen -----| |
| 1524 | */ |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1525 | if (save_len + 13 >= MAXPATHLEN) |
| 1526 | return 0; |
| 1527 | buf[i++] = SEP; |
Tim Peters | 0f9431f | 2001-07-05 03:47:53 +0000 | [diff] [blame] | 1528 | pname = buf + i; |
| 1529 | strcpy(pname, "__init__.py"); |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1530 | if (stat(buf, &statbuf) == 0) { |
Tim Peters | 0f9431f | 2001-07-05 03:47:53 +0000 | [diff] [blame] | 1531 | if (case_ok(buf, |
| 1532 | save_len + 9, /* len("/__init__") */ |
| 1533 | 8, /* len("__init__") */ |
| 1534 | pname)) { |
| 1535 | buf[save_len] = '\0'; |
| 1536 | return 1; |
| 1537 | } |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1538 | } |
Tim Peters | 0f9431f | 2001-07-05 03:47:53 +0000 | [diff] [blame] | 1539 | i += strlen(pname); |
| 1540 | strcpy(buf+i, Py_OptimizeFlag ? "o" : "c"); |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1541 | if (stat(buf, &statbuf) == 0) { |
Tim Peters | 0f9431f | 2001-07-05 03:47:53 +0000 | [diff] [blame] | 1542 | if (case_ok(buf, |
| 1543 | save_len + 9, /* len("/__init__") */ |
| 1544 | 8, /* len("__init__") */ |
| 1545 | pname)) { |
| 1546 | buf[save_len] = '\0'; |
| 1547 | return 1; |
| 1548 | } |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1549 | } |
| 1550 | buf[save_len] = '\0'; |
| 1551 | return 0; |
| 1552 | } |
Guido van Rossum | 48a680c | 2001-03-02 06:34:14 +0000 | [diff] [blame] | 1553 | |
| 1554 | #else |
| 1555 | |
| 1556 | #ifdef RISCOS |
| 1557 | static int |
| 1558 | find_init_module(buf) |
| 1559 | char *buf; |
| 1560 | { |
| 1561 | int save_len = strlen(buf); |
| 1562 | int i = save_len; |
| 1563 | |
| 1564 | if (save_len + 13 >= MAXPATHLEN) |
| 1565 | return 0; |
| 1566 | buf[i++] = SEP; |
| 1567 | strcpy(buf+i, "__init__/py"); |
| 1568 | if (isfile(buf)) { |
| 1569 | buf[save_len] = '\0'; |
| 1570 | return 1; |
| 1571 | } |
| 1572 | |
| 1573 | if (Py_OptimizeFlag) |
| 1574 | strcpy(buf+i, "o"); |
| 1575 | else |
| 1576 | strcpy(buf+i, "c"); |
| 1577 | if (isfile(buf)) { |
| 1578 | buf[save_len] = '\0'; |
| 1579 | return 1; |
| 1580 | } |
| 1581 | buf[save_len] = '\0'; |
| 1582 | return 0; |
| 1583 | } |
| 1584 | #endif /*RISCOS*/ |
| 1585 | |
Guido van Rossum | 197346f | 1997-10-31 18:38:52 +0000 | [diff] [blame] | 1586 | #endif /* HAVE_STAT */ |
| 1587 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1588 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 1589 | static int init_builtin(char *); /* Forward */ |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1590 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1591 | /* Load an external module using the default search path and return |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1592 | its module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1593 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1594 | static PyObject * |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1595 | load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1596 | { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1597 | PyObject *modules; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1598 | PyObject *m; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1599 | int err; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1600 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1601 | /* First check that there's an open file (if we need one) */ |
| 1602 | switch (type) { |
| 1603 | case PY_SOURCE: |
| 1604 | case PY_COMPILED: |
| 1605 | if (fp == NULL) { |
| 1606 | PyErr_Format(PyExc_ValueError, |
| 1607 | "file object required for import (type code %d)", |
| 1608 | type); |
| 1609 | return NULL; |
| 1610 | } |
| 1611 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1612 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1613 | switch (type) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1614 | |
| 1615 | case PY_SOURCE: |
| 1616 | m = load_source_module(name, buf, fp); |
| 1617 | break; |
| 1618 | |
| 1619 | case PY_COMPILED: |
| 1620 | m = load_compiled_module(name, buf, fp); |
| 1621 | break; |
| 1622 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 1623 | #ifdef HAVE_DYNAMIC_LOADING |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1624 | case C_EXTENSION: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1625 | m = _PyImport_LoadDynamicModule(name, buf, fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1626 | break; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 1627 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1628 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1629 | case PKG_DIRECTORY: |
| 1630 | m = load_package(name, buf); |
| 1631 | break; |
| 1632 | |
| 1633 | case C_BUILTIN: |
| 1634 | case PY_FROZEN: |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1635 | if (buf != NULL && buf[0] != '\0') |
| 1636 | name = buf; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1637 | if (type == C_BUILTIN) |
| 1638 | err = init_builtin(name); |
| 1639 | else |
| 1640 | err = PyImport_ImportFrozenModule(name); |
| 1641 | if (err < 0) |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 1642 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1643 | if (err == 0) { |
| 1644 | PyErr_Format(PyExc_ImportError, |
| 1645 | "Purported %s module %.200s not found", |
| 1646 | type == C_BUILTIN ? |
| 1647 | "builtin" : "frozen", |
| 1648 | name); |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 1649 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1650 | } |
| 1651 | modules = PyImport_GetModuleDict(); |
| 1652 | m = PyDict_GetItemString(modules, name); |
| 1653 | if (m == NULL) { |
| 1654 | PyErr_Format( |
| 1655 | PyExc_ImportError, |
| 1656 | "%s module %.200s not properly initialized", |
| 1657 | type == C_BUILTIN ? |
| 1658 | "builtin" : "frozen", |
| 1659 | name); |
Guido van Rossum | a86f77d | 1997-09-09 18:53:47 +0000 | [diff] [blame] | 1660 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1661 | } |
| 1662 | Py_INCREF(m); |
| 1663 | break; |
| 1664 | |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1665 | case IMP_HOOK: { |
| 1666 | if (loader == NULL) { |
| 1667 | PyErr_SetString(PyExc_ImportError, |
| 1668 | "import hook without loader"); |
| 1669 | return NULL; |
| 1670 | } |
| 1671 | m = PyObject_CallMethod(loader, "load_module", "s", name); |
| 1672 | break; |
| 1673 | } |
| 1674 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1675 | default: |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1676 | PyErr_Format(PyExc_ImportError, |
| 1677 | "Don't know how to import %.200s (type code %d)", |
| 1678 | name, type); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1679 | m = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1680 | |
| 1681 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1682 | |
| 1683 | return m; |
| 1684 | } |
| 1685 | |
| 1686 | |
| 1687 | /* Initialize a built-in module. |
| 1688 | Return 1 for succes, 0 if the module is not found, and -1 with |
| 1689 | an exception set if the initialization failed. */ |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1690 | |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1691 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1692 | init_builtin(char *name) |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1693 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1694 | struct _inittab *p; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1695 | |
Greg Ward | 201baee | 2001-10-04 14:52:06 +0000 | [diff] [blame] | 1696 | if (_PyImport_FindExtension(name, name) != NULL) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1697 | return 1; |
| 1698 | |
Guido van Rossum | 771c6c8 | 1997-10-31 18:37:24 +0000 | [diff] [blame] | 1699 | for (p = PyImport_Inittab; p->name != NULL; p++) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1700 | if (strcmp(name, p->name) == 0) { |
| 1701 | if (p->initfunc == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1702 | PyErr_Format(PyExc_ImportError, |
| 1703 | "Cannot re-init internal module %.200s", |
| 1704 | name); |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1705 | return -1; |
| 1706 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1707 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 1708 | PySys_WriteStderr("import %s # builtin\n", name); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1709 | (*p->initfunc)(); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1710 | if (PyErr_Occurred()) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1711 | return -1; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1712 | if (_PyImport_FixupExtension(name, name) == NULL) |
| 1713 | return -1; |
Guido van Rossum | 7f133ed | 1991-02-19 12:23:57 +0000 | [diff] [blame] | 1714 | return 1; |
| 1715 | } |
| 1716 | } |
| 1717 | return 0; |
| 1718 | } |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1719 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1720 | |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1721 | /* Frozen modules */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1722 | |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1723 | static struct _frozen * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1724 | find_frozen(char *name) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1725 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1726 | struct _frozen *p; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1727 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1728 | for (p = PyImport_FrozenModules; ; p++) { |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1729 | if (p->name == NULL) |
| 1730 | return NULL; |
| 1731 | if (strcmp(p->name, name) == 0) |
| 1732 | break; |
| 1733 | } |
| 1734 | return p; |
| 1735 | } |
| 1736 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1737 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1738 | get_frozen_object(char *name) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1739 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1740 | struct _frozen *p = find_frozen(name); |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1741 | int size; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1742 | |
| 1743 | if (p == NULL) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1744 | PyErr_Format(PyExc_ImportError, |
| 1745 | "No such frozen object named %.200s", |
| 1746 | name); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1747 | return NULL; |
| 1748 | } |
Guido van Rossum | 8f4d331 | 2001-10-18 18:54:11 +0000 | [diff] [blame] | 1749 | if (p->code == NULL) { |
| 1750 | PyErr_Format(PyExc_ImportError, |
| 1751 | "Excluded frozen object named %.200s", |
| 1752 | name); |
| 1753 | return NULL; |
| 1754 | } |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1755 | size = p->size; |
| 1756 | if (size < 0) |
| 1757 | size = -size; |
| 1758 | return PyMarshal_ReadObjectFromString((char *)p->code, size); |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | /* Initialize a frozen module. |
| 1762 | Return 1 for succes, 0 if the module is not found, and -1 with |
| 1763 | an exception set if the initialization failed. |
| 1764 | This function is also used from frozenmain.c */ |
Guido van Rossum | 0b34490 | 1995-02-07 15:35:27 +0000 | [diff] [blame] | 1765 | |
| 1766 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1767 | PyImport_ImportFrozenModule(char *name) |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1768 | { |
Guido van Rossum | cfd0a22 | 1996-06-17 17:06:34 +0000 | [diff] [blame] | 1769 | struct _frozen *p = find_frozen(name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1770 | PyObject *co; |
| 1771 | PyObject *m; |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1772 | int ispackage; |
| 1773 | int size; |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 1774 | |
| 1775 | if (p == NULL) |
| 1776 | return 0; |
Guido van Rossum | 8f4d331 | 2001-10-18 18:54:11 +0000 | [diff] [blame] | 1777 | if (p->code == NULL) { |
| 1778 | PyErr_Format(PyExc_ImportError, |
| 1779 | "Excluded frozen object named %.200s", |
| 1780 | name); |
| 1781 | return -1; |
| 1782 | } |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1783 | size = p->size; |
| 1784 | ispackage = (size < 0); |
| 1785 | if (ispackage) |
| 1786 | size = -size; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1787 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 1788 | PySys_WriteStderr("import %s # frozen%s\n", |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1789 | name, ispackage ? " package" : ""); |
| 1790 | co = PyMarshal_ReadObjectFromString((char *)p->code, size); |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1791 | if (co == NULL) |
| 1792 | return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1793 | if (!PyCode_Check(co)) { |
| 1794 | Py_DECREF(co); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1795 | PyErr_Format(PyExc_TypeError, |
| 1796 | "frozen object %.200s is not a code object", |
| 1797 | name); |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1798 | return -1; |
| 1799 | } |
Guido van Rossum | a5568d3 | 1998-03-05 03:45:08 +0000 | [diff] [blame] | 1800 | if (ispackage) { |
| 1801 | /* Set __path__ to the package name */ |
| 1802 | PyObject *d, *s; |
| 1803 | int err; |
| 1804 | m = PyImport_AddModule(name); |
| 1805 | if (m == NULL) |
| 1806 | return -1; |
| 1807 | d = PyModule_GetDict(m); |
| 1808 | s = PyString_InternFromString(name); |
| 1809 | if (s == NULL) |
| 1810 | return -1; |
| 1811 | err = PyDict_SetItemString(d, "__path__", s); |
| 1812 | Py_DECREF(s); |
| 1813 | if (err != 0) |
| 1814 | return err; |
| 1815 | } |
Guido van Rossum | e32bf6e | 1998-02-11 05:53:02 +0000 | [diff] [blame] | 1816 | m = PyImport_ExecCodeModuleEx(name, co, "<frozen>"); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1817 | Py_DECREF(co); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1818 | if (m == NULL) |
| 1819 | return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1820 | Py_DECREF(m); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1821 | return 1; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1822 | } |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1823 | |
| 1824 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1825 | /* Import a module, either built-in, frozen, or external, and return |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 1826 | its module object WITH INCREMENTED REFERENCE COUNT */ |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1827 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1828 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1829 | PyImport_ImportModule(char *name) |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 1830 | { |
Marc-André Lemburg | 3c61c35 | 2001-02-09 19:40:15 +0000 | [diff] [blame] | 1831 | PyObject *pname; |
| 1832 | PyObject *result; |
| 1833 | |
| 1834 | pname = PyString_FromString(name); |
Neal Norwitz | a11e4c1 | 2003-03-23 14:31:01 +0000 | [diff] [blame] | 1835 | if (pname == NULL) |
| 1836 | return NULL; |
Marc-André Lemburg | 3c61c35 | 2001-02-09 19:40:15 +0000 | [diff] [blame] | 1837 | result = PyImport_Import(pname); |
| 1838 | Py_DECREF(pname); |
| 1839 | return result; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1842 | /* Forward declarations for helper routines */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 1843 | static PyObject *get_parent(PyObject *globals, char *buf, int *p_buflen); |
| 1844 | static PyObject *load_next(PyObject *mod, PyObject *altmod, |
| 1845 | char **p_name, char *buf, int *p_buflen); |
| 1846 | static int mark_miss(char *name); |
| 1847 | static int ensure_fromlist(PyObject *mod, PyObject *fromlist, |
| 1848 | char *buf, int buflen, int recursive); |
| 1849 | static PyObject * import_submodule(PyObject *mod, char *name, char *fullname); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1850 | |
| 1851 | /* The Magnum Opus of dotted-name import :-) */ |
| 1852 | |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 1853 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1854 | import_module_ex(char *name, PyObject *globals, PyObject *locals, |
| 1855 | PyObject *fromlist) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 1856 | { |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1857 | char buf[MAXPATHLEN+1]; |
| 1858 | int buflen = 0; |
| 1859 | PyObject *parent, *head, *next, *tail; |
| 1860 | |
| 1861 | parent = get_parent(globals, buf, &buflen); |
| 1862 | if (parent == NULL) |
| 1863 | return NULL; |
| 1864 | |
| 1865 | head = load_next(parent, Py_None, &name, buf, &buflen); |
| 1866 | if (head == NULL) |
| 1867 | return NULL; |
| 1868 | |
| 1869 | tail = head; |
| 1870 | Py_INCREF(tail); |
| 1871 | while (name) { |
| 1872 | next = load_next(tail, tail, &name, buf, &buflen); |
| 1873 | Py_DECREF(tail); |
| 1874 | if (next == NULL) { |
| 1875 | Py_DECREF(head); |
| 1876 | return NULL; |
| 1877 | } |
| 1878 | tail = next; |
| 1879 | } |
| 1880 | |
| 1881 | if (fromlist != NULL) { |
| 1882 | if (fromlist == Py_None || !PyObject_IsTrue(fromlist)) |
| 1883 | fromlist = NULL; |
| 1884 | } |
| 1885 | |
| 1886 | if (fromlist == NULL) { |
| 1887 | Py_DECREF(tail); |
| 1888 | return head; |
| 1889 | } |
| 1890 | |
| 1891 | Py_DECREF(head); |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 1892 | if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) { |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1893 | Py_DECREF(tail); |
| 1894 | return NULL; |
| 1895 | } |
| 1896 | |
| 1897 | return tail; |
| 1898 | } |
| 1899 | |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 1900 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1901 | PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals, |
| 1902 | PyObject *fromlist) |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 1903 | { |
| 1904 | PyObject *result; |
| 1905 | lock_import(); |
Guido van Rossum | d65911b | 1998-03-03 22:33:27 +0000 | [diff] [blame] | 1906 | result = import_module_ex(name, globals, locals, fromlist); |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 1907 | if (unlock_import() < 0) { |
| 1908 | Py_XDECREF(result); |
| 1909 | PyErr_SetString(PyExc_RuntimeError, |
| 1910 | "not holding the import lock"); |
| 1911 | return NULL; |
| 1912 | } |
Guido van Rossum | 75acc9c | 1998-03-03 22:26:50 +0000 | [diff] [blame] | 1913 | return result; |
| 1914 | } |
| 1915 | |
Fred Drake | 8759090 | 2004-05-28 20:21:36 +0000 | [diff] [blame] | 1916 | /* Return the package that an import is being performed in. If globals comes |
| 1917 | from the module foo.bar.bat (not itself a package), this returns the |
| 1918 | sys.modules entry for foo.bar. If globals is from a package's __init__.py, |
| 1919 | the package's entry in sys.modules is returned. |
| 1920 | |
| 1921 | The *name* of the returned package is returned in buf, with the length of |
| 1922 | the name in *p_buflen. |
| 1923 | |
| 1924 | If globals doesn't come from a package or a module in a package, or a |
| 1925 | corresponding entry is not found in sys.modules, Py_None is returned. |
| 1926 | */ |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1927 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1928 | get_parent(PyObject *globals, char *buf, int *p_buflen) |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1929 | { |
| 1930 | static PyObject *namestr = NULL; |
| 1931 | static PyObject *pathstr = NULL; |
| 1932 | PyObject *modname, *modpath, *modules, *parent; |
| 1933 | |
| 1934 | if (globals == NULL || !PyDict_Check(globals)) |
| 1935 | return Py_None; |
| 1936 | |
| 1937 | if (namestr == NULL) { |
| 1938 | namestr = PyString_InternFromString("__name__"); |
| 1939 | if (namestr == NULL) |
| 1940 | return NULL; |
| 1941 | } |
| 1942 | if (pathstr == NULL) { |
| 1943 | pathstr = PyString_InternFromString("__path__"); |
| 1944 | if (pathstr == NULL) |
| 1945 | return NULL; |
| 1946 | } |
| 1947 | |
| 1948 | *buf = '\0'; |
| 1949 | *p_buflen = 0; |
| 1950 | modname = PyDict_GetItem(globals, namestr); |
| 1951 | if (modname == NULL || !PyString_Check(modname)) |
| 1952 | return Py_None; |
| 1953 | |
| 1954 | modpath = PyDict_GetItem(globals, pathstr); |
| 1955 | if (modpath != NULL) { |
| 1956 | int len = PyString_GET_SIZE(modname); |
| 1957 | if (len > MAXPATHLEN) { |
| 1958 | PyErr_SetString(PyExc_ValueError, |
| 1959 | "Module name too long"); |
| 1960 | return NULL; |
| 1961 | } |
| 1962 | strcpy(buf, PyString_AS_STRING(modname)); |
| 1963 | *p_buflen = len; |
| 1964 | } |
| 1965 | else { |
| 1966 | char *start = PyString_AS_STRING(modname); |
| 1967 | char *lastdot = strrchr(start, '.'); |
Fred Drake | 4c82b23 | 2000-06-30 16:18:57 +0000 | [diff] [blame] | 1968 | size_t len; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1969 | if (lastdot == NULL) |
| 1970 | return Py_None; |
| 1971 | len = lastdot - start; |
| 1972 | if (len >= MAXPATHLEN) { |
| 1973 | PyErr_SetString(PyExc_ValueError, |
| 1974 | "Module name too long"); |
| 1975 | return NULL; |
| 1976 | } |
| 1977 | strncpy(buf, start, len); |
| 1978 | buf[len] = '\0'; |
| 1979 | *p_buflen = len; |
| 1980 | } |
| 1981 | |
| 1982 | modules = PyImport_GetModuleDict(); |
| 1983 | parent = PyDict_GetItemString(modules, buf); |
| 1984 | if (parent == NULL) |
| 1985 | parent = Py_None; |
| 1986 | return parent; |
| 1987 | /* We expect, but can't guarantee, if parent != None, that: |
| 1988 | - parent.__name__ == buf |
| 1989 | - parent.__dict__ is globals |
| 1990 | If this is violated... Who cares? */ |
| 1991 | } |
| 1992 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1993 | /* altmod is either None or same as mod */ |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1994 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1995 | load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf, |
| 1996 | int *p_buflen) |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 1997 | { |
| 1998 | char *name = *p_name; |
| 1999 | char *dot = strchr(name, '.'); |
Fred Drake | 4c82b23 | 2000-06-30 16:18:57 +0000 | [diff] [blame] | 2000 | size_t len; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2001 | char *p; |
| 2002 | PyObject *result; |
| 2003 | |
| 2004 | if (dot == NULL) { |
| 2005 | *p_name = NULL; |
| 2006 | len = strlen(name); |
| 2007 | } |
| 2008 | else { |
| 2009 | *p_name = dot+1; |
| 2010 | len = dot-name; |
| 2011 | } |
Guido van Rossum | 111c20b | 1998-04-11 17:38:22 +0000 | [diff] [blame] | 2012 | if (len == 0) { |
| 2013 | PyErr_SetString(PyExc_ValueError, |
| 2014 | "Empty module name"); |
| 2015 | return NULL; |
| 2016 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2017 | |
| 2018 | p = buf + *p_buflen; |
| 2019 | if (p != buf) |
| 2020 | *p++ = '.'; |
| 2021 | if (p+len-buf >= MAXPATHLEN) { |
| 2022 | PyErr_SetString(PyExc_ValueError, |
| 2023 | "Module name too long"); |
| 2024 | return NULL; |
| 2025 | } |
| 2026 | strncpy(p, name, len); |
| 2027 | p[len] = '\0'; |
| 2028 | *p_buflen = p+len-buf; |
| 2029 | |
| 2030 | result = import_submodule(mod, p, buf); |
| 2031 | if (result == Py_None && altmod != mod) { |
| 2032 | Py_DECREF(result); |
Guido van Rossum | f5f5fdb | 1997-09-06 20:29:52 +0000 | [diff] [blame] | 2033 | /* Here, altmod must be None and mod must not be None */ |
Guido van Rossum | 0c81945 | 1997-09-07 06:16:57 +0000 | [diff] [blame] | 2034 | result = import_submodule(altmod, p, p); |
Guido van Rossum | f5f5fdb | 1997-09-06 20:29:52 +0000 | [diff] [blame] | 2035 | if (result != NULL && result != Py_None) { |
| 2036 | if (mark_miss(buf) != 0) { |
| 2037 | Py_DECREF(result); |
| 2038 | return NULL; |
| 2039 | } |
| 2040 | strncpy(buf, name, len); |
| 2041 | buf[len] = '\0'; |
| 2042 | *p_buflen = len; |
| 2043 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2044 | } |
| 2045 | if (result == NULL) |
| 2046 | return NULL; |
| 2047 | |
| 2048 | if (result == Py_None) { |
| 2049 | Py_DECREF(result); |
| 2050 | PyErr_Format(PyExc_ImportError, |
| 2051 | "No module named %.200s", name); |
| 2052 | return NULL; |
| 2053 | } |
| 2054 | |
| 2055 | return result; |
| 2056 | } |
| 2057 | |
| 2058 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2059 | mark_miss(char *name) |
Guido van Rossum | f5f5fdb | 1997-09-06 20:29:52 +0000 | [diff] [blame] | 2060 | { |
| 2061 | PyObject *modules = PyImport_GetModuleDict(); |
| 2062 | return PyDict_SetItemString(modules, name, Py_None); |
| 2063 | } |
| 2064 | |
| 2065 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2066 | ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, int buflen, |
| 2067 | int recursive) |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2068 | { |
| 2069 | int i; |
| 2070 | |
| 2071 | if (!PyObject_HasAttrString(mod, "__path__")) |
| 2072 | return 1; |
| 2073 | |
| 2074 | for (i = 0; ; i++) { |
| 2075 | PyObject *item = PySequence_GetItem(fromlist, i); |
| 2076 | int hasit; |
| 2077 | if (item == NULL) { |
| 2078 | if (PyErr_ExceptionMatches(PyExc_IndexError)) { |
| 2079 | PyErr_Clear(); |
| 2080 | return 1; |
| 2081 | } |
| 2082 | return 0; |
| 2083 | } |
| 2084 | if (!PyString_Check(item)) { |
| 2085 | PyErr_SetString(PyExc_TypeError, |
| 2086 | "Item in ``from list'' not a string"); |
| 2087 | Py_DECREF(item); |
| 2088 | return 0; |
| 2089 | } |
| 2090 | if (PyString_AS_STRING(item)[0] == '*') { |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 2091 | PyObject *all; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2092 | Py_DECREF(item); |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 2093 | /* See if the package defines __all__ */ |
| 2094 | if (recursive) |
| 2095 | continue; /* Avoid endless recursion */ |
| 2096 | all = PyObject_GetAttrString(mod, "__all__"); |
| 2097 | if (all == NULL) |
| 2098 | PyErr_Clear(); |
| 2099 | else { |
Martin v. Löwis | 83969ee | 2004-03-23 16:28:13 +0000 | [diff] [blame] | 2100 | int ret = ensure_fromlist(mod, all, buf, buflen, 1); |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 2101 | Py_DECREF(all); |
Martin v. Löwis | 83969ee | 2004-03-23 16:28:13 +0000 | [diff] [blame] | 2102 | if (!ret) |
| 2103 | return 0; |
Guido van Rossum | 9905ef9 | 1997-09-08 16:07:11 +0000 | [diff] [blame] | 2104 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2105 | continue; |
| 2106 | } |
| 2107 | hasit = PyObject_HasAttr(mod, item); |
| 2108 | if (!hasit) { |
| 2109 | char *subname = PyString_AS_STRING(item); |
| 2110 | PyObject *submod; |
| 2111 | char *p; |
| 2112 | if (buflen + strlen(subname) >= MAXPATHLEN) { |
| 2113 | PyErr_SetString(PyExc_ValueError, |
| 2114 | "Module name too long"); |
| 2115 | Py_DECREF(item); |
| 2116 | return 0; |
| 2117 | } |
| 2118 | p = buf + buflen; |
| 2119 | *p++ = '.'; |
| 2120 | strcpy(p, subname); |
| 2121 | submod = import_submodule(mod, subname, buf); |
| 2122 | Py_XDECREF(submod); |
| 2123 | if (submod == NULL) { |
| 2124 | Py_DECREF(item); |
| 2125 | return 0; |
| 2126 | } |
| 2127 | } |
| 2128 | Py_DECREF(item); |
| 2129 | } |
| 2130 | |
Guido van Rossum | a7f2e81 | 1997-10-03 15:33:32 +0000 | [diff] [blame] | 2131 | /* NOTREACHED */ |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
Neil Schemenauer | 00b0966 | 2003-06-16 21:03:07 +0000 | [diff] [blame] | 2134 | static int |
| 2135 | add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname, |
| 2136 | PyObject *modules) |
| 2137 | { |
| 2138 | if (mod == Py_None) |
| 2139 | return 1; |
| 2140 | /* Irrespective of the success of this load, make a |
| 2141 | reference to it in the parent package module. A copy gets |
| 2142 | saved in the modules dictionary under the full name, so get a |
| 2143 | reference from there, if need be. (The exception is when the |
| 2144 | load failed with a SyntaxError -- then there's no trace in |
| 2145 | sys.modules. In that case, of course, do nothing extra.) */ |
| 2146 | if (submod == NULL) { |
| 2147 | submod = PyDict_GetItemString(modules, fullname); |
| 2148 | if (submod == NULL) |
| 2149 | return 1; |
| 2150 | } |
| 2151 | if (PyModule_Check(mod)) { |
| 2152 | /* We can't use setattr here since it can give a |
| 2153 | * spurious warning if the submodule name shadows a |
| 2154 | * builtin name */ |
| 2155 | PyObject *dict = PyModule_GetDict(mod); |
| 2156 | if (!dict) |
| 2157 | return 0; |
| 2158 | if (PyDict_SetItemString(dict, subname, submod) < 0) |
| 2159 | return 0; |
| 2160 | } |
| 2161 | else { |
| 2162 | if (PyObject_SetAttrString(mod, subname, submod) < 0) |
| 2163 | return 0; |
| 2164 | } |
| 2165 | return 1; |
| 2166 | } |
| 2167 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2168 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2169 | import_submodule(PyObject *mod, char *subname, char *fullname) |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2170 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2171 | PyObject *modules = PyImport_GetModuleDict(); |
Neil Schemenauer | 00b0966 | 2003-06-16 21:03:07 +0000 | [diff] [blame] | 2172 | PyObject *m = NULL; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 2173 | |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2174 | /* Require: |
| 2175 | if mod == None: subname == fullname |
| 2176 | else: mod.__name__ + "." + subname == fullname |
| 2177 | */ |
| 2178 | |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 2179 | if ((m = PyDict_GetItemString(modules, fullname)) != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2180 | Py_INCREF(m); |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 2181 | } |
| 2182 | else { |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 2183 | PyObject *path, *loader = NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2184 | char buf[MAXPATHLEN+1]; |
| 2185 | struct filedescr *fdp; |
| 2186 | FILE *fp = NULL; |
| 2187 | |
Guido van Rossum | 9c0afe5 | 1998-05-19 15:09:05 +0000 | [diff] [blame] | 2188 | if (mod == Py_None) |
| 2189 | path = NULL; |
| 2190 | else { |
| 2191 | path = PyObject_GetAttrString(mod, "__path__"); |
| 2192 | if (path == NULL) { |
| 2193 | PyErr_Clear(); |
| 2194 | Py_INCREF(Py_None); |
| 2195 | return Py_None; |
| 2196 | } |
| 2197 | } |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2198 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2199 | buf[0] = '\0'; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 2200 | fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1, |
| 2201 | &fp, &loader); |
Guido van Rossum | b68cd42 | 1998-07-01 17:36:26 +0000 | [diff] [blame] | 2202 | Py_XDECREF(path); |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2203 | if (fdp == NULL) { |
| 2204 | if (!PyErr_ExceptionMatches(PyExc_ImportError)) |
| 2205 | return NULL; |
| 2206 | PyErr_Clear(); |
| 2207 | Py_INCREF(Py_None); |
| 2208 | return Py_None; |
| 2209 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 2210 | m = load_module(fullname, fp, buf, fdp->type, loader); |
| 2211 | Py_XDECREF(loader); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2212 | if (fp) |
| 2213 | fclose(fp); |
Neil Schemenauer | 00b0966 | 2003-06-16 21:03:07 +0000 | [diff] [blame] | 2214 | if (!add_submodule(mod, m, fullname, subname, modules)) { |
| 2215 | Py_XDECREF(m); |
| 2216 | m = NULL; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2217 | } |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 2218 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2219 | |
| 2220 | return m; |
Guido van Rossum | 74e6a11 | 1994-08-29 12:54:38 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2223 | |
| 2224 | /* Re-import a module of any kind and return its module object, WITH |
| 2225 | INCREMENTED REFERENCE COUNT */ |
| 2226 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2227 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2228 | PyImport_ReloadModule(PyObject *m) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2229 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2230 | PyObject *modules = PyImport_GetModuleDict(); |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 2231 | PyObject *path = NULL; |
| 2232 | char *name, *subname; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2233 | char buf[MAXPATHLEN+1]; |
| 2234 | struct filedescr *fdp; |
| 2235 | FILE *fp = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2236 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2237 | if (m == NULL || !PyModule_Check(m)) { |
| 2238 | PyErr_SetString(PyExc_TypeError, |
| 2239 | "reload() argument must be module"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2240 | return NULL; |
| 2241 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2242 | name = PyModule_GetName(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2243 | if (name == NULL) |
| 2244 | return NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2245 | if (m != PyDict_GetItemString(modules, name)) { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2246 | PyErr_Format(PyExc_ImportError, |
| 2247 | "reload(): module %.200s not in sys.modules", |
| 2248 | name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2249 | return NULL; |
| 2250 | } |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 2251 | subname = strrchr(name, '.'); |
| 2252 | if (subname == NULL) |
| 2253 | subname = name; |
| 2254 | else { |
| 2255 | PyObject *parentname, *parent; |
| 2256 | parentname = PyString_FromStringAndSize(name, (subname-name)); |
| 2257 | if (parentname == NULL) |
| 2258 | return NULL; |
| 2259 | parent = PyDict_GetItem(modules, parentname); |
Barry Warsaw | 3879333 | 1999-01-27 17:54:20 +0000 | [diff] [blame] | 2260 | Py_DECREF(parentname); |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 2261 | if (parent == NULL) { |
| 2262 | PyErr_Format(PyExc_ImportError, |
| 2263 | "reload(): parent %.200s not in sys.modules", |
| 2264 | name); |
| 2265 | return NULL; |
| 2266 | } |
| 2267 | subname++; |
| 2268 | path = PyObject_GetAttrString(parent, "__path__"); |
| 2269 | if (path == NULL) |
| 2270 | PyErr_Clear(); |
| 2271 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2272 | buf[0] = '\0'; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 2273 | fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, NULL); |
Guido van Rossum | 222ef56 | 1997-09-06 19:41:09 +0000 | [diff] [blame] | 2274 | Py_XDECREF(path); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2275 | if (fdp == NULL) |
| 2276 | return NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 2277 | m = load_module(name, fp, buf, fdp->type, NULL); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2278 | if (fp) |
| 2279 | fclose(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2280 | return m; |
| 2281 | } |
| 2282 | |
| 2283 | |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2284 | /* Higher-level import emulator which emulates the "import" statement |
| 2285 | more accurately -- it invokes the __import__() function from the |
| 2286 | builtins of the current globals. This means that the import is |
| 2287 | done using whatever import hooks are installed in the current |
Guido van Rossum | 6058eb4 | 1998-12-21 19:51:00 +0000 | [diff] [blame] | 2288 | environment, e.g. by "rexec". |
| 2289 | A dummy list ["__doc__"] is passed as the 4th argument so that |
| 2290 | e.g. PyImport_Import(PyString_FromString("win32com.client.gencache")) |
| 2291 | will return <module "gencache"> instead of <module "win32com">. */ |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2292 | |
| 2293 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2294 | PyImport_Import(PyObject *module_name) |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2295 | { |
| 2296 | static PyObject *silly_list = NULL; |
| 2297 | static PyObject *builtins_str = NULL; |
| 2298 | static PyObject *import_str = NULL; |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2299 | PyObject *globals = NULL; |
| 2300 | PyObject *import = NULL; |
| 2301 | PyObject *builtins = NULL; |
| 2302 | PyObject *r = NULL; |
| 2303 | |
| 2304 | /* Initialize constant string objects */ |
| 2305 | if (silly_list == NULL) { |
| 2306 | import_str = PyString_InternFromString("__import__"); |
| 2307 | if (import_str == NULL) |
| 2308 | return NULL; |
| 2309 | builtins_str = PyString_InternFromString("__builtins__"); |
| 2310 | if (builtins_str == NULL) |
| 2311 | return NULL; |
| 2312 | silly_list = Py_BuildValue("[s]", "__doc__"); |
| 2313 | if (silly_list == NULL) |
| 2314 | return NULL; |
| 2315 | } |
| 2316 | |
| 2317 | /* Get the builtins from current globals */ |
| 2318 | globals = PyEval_GetGlobals(); |
Guido van Rossum | 85cd1d6 | 2001-02-20 21:43:24 +0000 | [diff] [blame] | 2319 | if (globals != NULL) { |
Guido van Rossum | 6646856 | 1998-10-22 15:46:50 +0000 | [diff] [blame] | 2320 | Py_INCREF(globals); |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2321 | builtins = PyObject_GetItem(globals, builtins_str); |
| 2322 | if (builtins == NULL) |
| 2323 | goto err; |
| 2324 | } |
| 2325 | else { |
| 2326 | /* No globals -- use standard builtins, and fake globals */ |
| 2327 | PyErr_Clear(); |
| 2328 | |
Guido van Rossum | 85cd1d6 | 2001-02-20 21:43:24 +0000 | [diff] [blame] | 2329 | builtins = PyImport_ImportModuleEx("__builtin__", |
| 2330 | NULL, NULL, NULL); |
| 2331 | if (builtins == NULL) |
| 2332 | return NULL; |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2333 | globals = Py_BuildValue("{OO}", builtins_str, builtins); |
| 2334 | if (globals == NULL) |
| 2335 | goto err; |
| 2336 | } |
| 2337 | |
| 2338 | /* Get the __import__ function from the builtins */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2339 | if (PyDict_Check(builtins)) { |
Fred Drake | a76ba6e | 2001-03-06 06:31:15 +0000 | [diff] [blame] | 2340 | import = PyObject_GetItem(builtins, import_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2341 | if (import == NULL) |
| 2342 | PyErr_SetObject(PyExc_KeyError, import_str); |
| 2343 | } |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2344 | else |
Fred Drake | a76ba6e | 2001-03-06 06:31:15 +0000 | [diff] [blame] | 2345 | import = PyObject_GetAttr(builtins, import_str); |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2346 | if (import == NULL) |
| 2347 | goto err; |
| 2348 | |
| 2349 | /* Call the _import__ function with the proper argument list */ |
| 2350 | r = PyObject_CallFunction(import, "OOOO", |
| 2351 | module_name, globals, globals, silly_list); |
| 2352 | |
| 2353 | err: |
| 2354 | Py_XDECREF(globals); |
| 2355 | Py_XDECREF(builtins); |
| 2356 | Py_XDECREF(import); |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 2357 | |
Guido van Rossum | d47a0a8 | 1997-08-14 20:11:26 +0000 | [diff] [blame] | 2358 | return r; |
| 2359 | } |
| 2360 | |
| 2361 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2362 | /* Module 'imp' provides Python access to the primitives used for |
| 2363 | importing modules. |
| 2364 | */ |
| 2365 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2366 | static PyObject * |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 2367 | imp_get_magic(PyObject *self, PyObject *noargs) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2368 | { |
| 2369 | char buf[4]; |
| 2370 | |
Guido van Rossum | 96774c1 | 2000-05-01 20:19:08 +0000 | [diff] [blame] | 2371 | buf[0] = (char) ((pyc_magic >> 0) & 0xff); |
| 2372 | buf[1] = (char) ((pyc_magic >> 8) & 0xff); |
| 2373 | buf[2] = (char) ((pyc_magic >> 16) & 0xff); |
| 2374 | buf[3] = (char) ((pyc_magic >> 24) & 0xff); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2375 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2376 | return PyString_FromStringAndSize(buf, 4); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2379 | static PyObject * |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 2380 | imp_get_suffixes(PyObject *self, PyObject *noargs) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2381 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2382 | PyObject *list; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2383 | struct filedescr *fdp; |
| 2384 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2385 | list = PyList_New(0); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2386 | if (list == NULL) |
| 2387 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2388 | for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { |
| 2389 | PyObject *item = Py_BuildValue("ssi", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2390 | fdp->suffix, fdp->mode, fdp->type); |
| 2391 | if (item == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2392 | Py_DECREF(list); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2393 | return NULL; |
| 2394 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2395 | if (PyList_Append(list, item) < 0) { |
| 2396 | Py_DECREF(list); |
| 2397 | Py_DECREF(item); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2398 | return NULL; |
| 2399 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2400 | Py_DECREF(item); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2401 | } |
| 2402 | return list; |
| 2403 | } |
| 2404 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2405 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2406 | call_find_module(char *name, PyObject *path) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2407 | { |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 2408 | extern int fclose(FILE *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2409 | PyObject *fob, *ret; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2410 | struct filedescr *fdp; |
| 2411 | char pathname[MAXPATHLEN+1]; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2412 | FILE *fp = NULL; |
| 2413 | |
| 2414 | pathname[0] = '\0'; |
Guido van Rossum | 17fc85f | 1997-09-06 18:52:03 +0000 | [diff] [blame] | 2415 | if (path == Py_None) |
| 2416 | path = NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 2417 | fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2418 | if (fdp == NULL) |
| 2419 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2420 | if (fp != NULL) { |
| 2421 | fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose); |
| 2422 | if (fob == NULL) { |
| 2423 | fclose(fp); |
| 2424 | return NULL; |
| 2425 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2426 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2427 | else { |
| 2428 | fob = Py_None; |
| 2429 | Py_INCREF(fob); |
Tim Peters | 50d8d37 | 2001-02-28 05:34:27 +0000 | [diff] [blame] | 2430 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2431 | ret = Py_BuildValue("Os(ssi)", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2432 | fob, pathname, fdp->suffix, fdp->mode, fdp->type); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2433 | Py_DECREF(fob); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2434 | return ret; |
| 2435 | } |
| 2436 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2437 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2438 | imp_find_module(PyObject *self, PyObject *args) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2439 | { |
| 2440 | char *name; |
| 2441 | PyObject *path = NULL; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2442 | if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path)) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2443 | return NULL; |
| 2444 | return call_find_module(name, path); |
| 2445 | } |
| 2446 | |
| 2447 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2448 | imp_init_builtin(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2449 | { |
| 2450 | char *name; |
| 2451 | int ret; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2452 | PyObject *m; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2453 | if (!PyArg_ParseTuple(args, "s:init_builtin", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2454 | return NULL; |
| 2455 | ret = init_builtin(name); |
| 2456 | if (ret < 0) |
| 2457 | return NULL; |
| 2458 | if (ret == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2459 | Py_INCREF(Py_None); |
| 2460 | return Py_None; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2461 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2462 | m = PyImport_AddModule(name); |
| 2463 | Py_XINCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2464 | return m; |
| 2465 | } |
| 2466 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2467 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2468 | imp_init_frozen(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2469 | { |
| 2470 | char *name; |
| 2471 | int ret; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2472 | PyObject *m; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2473 | if (!PyArg_ParseTuple(args, "s:init_frozen", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2474 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2475 | ret = PyImport_ImportFrozenModule(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2476 | if (ret < 0) |
| 2477 | return NULL; |
| 2478 | if (ret == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2479 | Py_INCREF(Py_None); |
| 2480 | return Py_None; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2481 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2482 | m = PyImport_AddModule(name); |
| 2483 | Py_XINCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2484 | return m; |
| 2485 | } |
| 2486 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2487 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2488 | imp_get_frozen_object(PyObject *self, PyObject *args) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2489 | { |
| 2490 | char *name; |
Jack Jansen | 95ffa23 | 1995-10-03 14:38:41 +0000 | [diff] [blame] | 2491 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2492 | if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name)) |
Guido van Rossum | 6ec1efb | 1995-08-04 04:08:57 +0000 | [diff] [blame] | 2493 | return NULL; |
| 2494 | return get_frozen_object(name); |
| 2495 | } |
| 2496 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2497 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2498 | imp_is_builtin(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2499 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2500 | char *name; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2501 | if (!PyArg_ParseTuple(args, "s:is_builtin", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2502 | return NULL; |
Guido van Rossum | 50ee94f | 2002-04-09 18:00:58 +0000 | [diff] [blame] | 2503 | return PyInt_FromLong(is_builtin(name)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2506 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2507 | imp_is_frozen(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2508 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2509 | char *name; |
Guido van Rossum | 323bf5e | 1998-06-24 03:54:06 +0000 | [diff] [blame] | 2510 | struct _frozen *p; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2511 | if (!PyArg_ParseTuple(args, "s:is_frozen", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2512 | return NULL; |
Guido van Rossum | 323bf5e | 1998-06-24 03:54:06 +0000 | [diff] [blame] | 2513 | p = find_frozen(name); |
Guido van Rossum | b8bff3f | 2002-04-07 06:34:38 +0000 | [diff] [blame] | 2514 | return PyBool_FromLong((long) (p == NULL ? 0 : p->size)); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | static FILE * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2518 | get_file(char *pathname, PyObject *fob, char *mode) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2519 | { |
| 2520 | FILE *fp; |
| 2521 | if (fob == NULL) { |
Jeremy Hylton | 4ae6fae | 2002-05-30 17:15:25 +0000 | [diff] [blame] | 2522 | if (mode[0] == 'U') |
| 2523 | mode = "r" PY_STDIOTEXTMODE; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2524 | fp = fopen(pathname, mode); |
| 2525 | if (fp == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2526 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2527 | } |
| 2528 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2529 | fp = PyFile_AsFile(fob); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2530 | if (fp == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2531 | PyErr_SetString(PyExc_ValueError, |
| 2532 | "bad/closed file object"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2533 | } |
| 2534 | return fp; |
| 2535 | } |
| 2536 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2537 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2538 | imp_load_compiled(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2539 | { |
| 2540 | char *name; |
| 2541 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2542 | PyObject *fob = NULL; |
| 2543 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2544 | FILE *fp; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2545 | if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2546 | &PyFile_Type, &fob)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2547 | return NULL; |
| 2548 | fp = get_file(pathname, fob, "rb"); |
| 2549 | if (fp == NULL) |
| 2550 | return NULL; |
| 2551 | m = load_compiled_module(name, pathname, fp); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2552 | if (fob == NULL) |
| 2553 | fclose(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2554 | return m; |
| 2555 | } |
| 2556 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 2557 | #ifdef HAVE_DYNAMIC_LOADING |
| 2558 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2559 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2560 | imp_load_dynamic(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2561 | { |
| 2562 | char *name; |
| 2563 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2564 | PyObject *fob = NULL; |
| 2565 | PyObject *m; |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 2566 | FILE *fp = NULL; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2567 | if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2568 | &PyFile_Type, &fob)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2569 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2570 | if (fob) { |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 2571 | fp = get_file(pathname, fob, "r"); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2572 | if (fp == NULL) |
| 2573 | return NULL; |
| 2574 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2575 | m = _PyImport_LoadDynamicModule(name, pathname, fp); |
Guido van Rossum | 7faeab3 | 1995-07-07 22:50:36 +0000 | [diff] [blame] | 2576 | return m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 2579 | #endif /* HAVE_DYNAMIC_LOADING */ |
| 2580 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2581 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2582 | imp_load_source(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2583 | { |
| 2584 | char *name; |
| 2585 | char *pathname; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2586 | PyObject *fob = NULL; |
| 2587 | PyObject *m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2588 | FILE *fp; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2589 | if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2590 | &PyFile_Type, &fob)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2591 | return NULL; |
| 2592 | fp = get_file(pathname, fob, "r"); |
| 2593 | if (fp == NULL) |
| 2594 | return NULL; |
| 2595 | m = load_source_module(name, pathname, fp); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2596 | if (fob == NULL) |
| 2597 | fclose(fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2598 | return m; |
| 2599 | } |
| 2600 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2601 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2602 | imp_load_module(PyObject *self, PyObject *args) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2603 | { |
| 2604 | char *name; |
| 2605 | PyObject *fob; |
| 2606 | char *pathname; |
| 2607 | char *suffix; /* Unused */ |
| 2608 | char *mode; |
| 2609 | int type; |
| 2610 | FILE *fp; |
| 2611 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2612 | if (!PyArg_ParseTuple(args, "sOs(ssi):load_module", |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2613 | &name, &fob, &pathname, |
| 2614 | &suffix, &mode, &type)) |
| 2615 | return NULL; |
Guido van Rossum | 5e2c5fa | 2002-05-30 17:33:07 +0000 | [diff] [blame] | 2616 | if (*mode) { |
| 2617 | /* Mode must start with 'r' or 'U' and must not contain '+'. |
| 2618 | Implicit in this test is the assumption that the mode |
| 2619 | may contain other modifiers like 'b' or 't'. */ |
| 2620 | |
| 2621 | if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) { |
Jeremy Hylton | 4ae6fae | 2002-05-30 17:15:25 +0000 | [diff] [blame] | 2622 | PyErr_Format(PyExc_ValueError, |
| 2623 | "invalid file open mode %.200s", mode); |
| 2624 | return NULL; |
Guido van Rossum | 5e2c5fa | 2002-05-30 17:33:07 +0000 | [diff] [blame] | 2625 | } |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2626 | } |
| 2627 | if (fob == Py_None) |
| 2628 | fp = NULL; |
| 2629 | else { |
| 2630 | if (!PyFile_Check(fob)) { |
| 2631 | PyErr_SetString(PyExc_ValueError, |
| 2632 | "load_module arg#2 should be a file or None"); |
| 2633 | return NULL; |
| 2634 | } |
| 2635 | fp = get_file(pathname, fob, mode); |
| 2636 | if (fp == NULL) |
| 2637 | return NULL; |
| 2638 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 2639 | return load_module(name, fp, pathname, type, NULL); |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
| 2642 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2643 | imp_load_package(PyObject *self, PyObject *args) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2644 | { |
| 2645 | char *name; |
| 2646 | char *pathname; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2647 | if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname)) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2648 | return NULL; |
| 2649 | return load_package(name, pathname); |
| 2650 | } |
| 2651 | |
| 2652 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2653 | imp_new_module(PyObject *self, PyObject *args) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2654 | { |
| 2655 | char *name; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 2656 | if (!PyArg_ParseTuple(args, "s:new_module", &name)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2657 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2658 | return PyModule_New(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2661 | /* Doc strings */ |
| 2662 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2663 | PyDoc_STRVAR(doc_imp, |
| 2664 | "This module provides the components needed to build your own\n\ |
| 2665 | __import__ function. Undocumented functions are obsolete."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2666 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2667 | PyDoc_STRVAR(doc_find_module, |
| 2668 | "find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\ |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2669 | Search for a module. If path is omitted or None, search for a\n\ |
| 2670 | built-in, frozen or special module and continue search in sys.path.\n\ |
| 2671 | The module name cannot contain '.'; to search for a submodule of a\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2672 | package, pass the submodule name and the package's __path__."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2673 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2674 | PyDoc_STRVAR(doc_load_module, |
| 2675 | "load_module(name, file, filename, (suffix, mode, type)) -> module\n\ |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2676 | Load a module, given information returned by find_module().\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2677 | The module name must include the full package name, if any."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2678 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2679 | PyDoc_STRVAR(doc_get_magic, |
| 2680 | "get_magic() -> string\n\ |
| 2681 | Return the magic number for .pyc or .pyo files."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2682 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2683 | PyDoc_STRVAR(doc_get_suffixes, |
| 2684 | "get_suffixes() -> [(suffix, mode, type), ...]\n\ |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2685 | Return a list of (suffix, mode, type) tuples describing the files\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2686 | that find_module() looks for."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2687 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2688 | PyDoc_STRVAR(doc_new_module, |
| 2689 | "new_module(name) -> module\n\ |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2690 | Create a new module. Do not enter it in sys.modules.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2691 | The module name must include the full package name, if any."); |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2692 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2693 | PyDoc_STRVAR(doc_lock_held, |
| 2694 | "lock_held() -> 0 or 1\n\ |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 2695 | Return 1 if the import lock is currently held.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2696 | On platforms without threads, return 0."); |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 2697 | |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 2698 | PyDoc_STRVAR(doc_acquire_lock, |
| 2699 | "acquire_lock() -> None\n\ |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 2700 | Acquires the interpreter's import lock for the current thread.\n\ |
| 2701 | This lock should be used by import hooks to ensure thread-safety\n\ |
| 2702 | when importing modules.\n\ |
Guido van Rossum | c4f4ca9 | 2003-02-12 21:46:11 +0000 | [diff] [blame] | 2703 | On platforms without threads, this function does nothing."); |
| 2704 | |
| 2705 | PyDoc_STRVAR(doc_release_lock, |
| 2706 | "release_lock() -> None\n\ |
| 2707 | Release the interpreter's import lock.\n\ |
| 2708 | On platforms without threads, this function does nothing."); |
| 2709 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2710 | static PyMethodDef imp_methods[] = { |
Neal Norwitz | 08ea61a | 2003-02-17 18:18:00 +0000 | [diff] [blame] | 2711 | {"find_module", imp_find_module, METH_VARARGS, doc_find_module}, |
| 2712 | {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic}, |
| 2713 | {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes}, |
| 2714 | {"load_module", imp_load_module, METH_VARARGS, doc_load_module}, |
| 2715 | {"new_module", imp_new_module, METH_VARARGS, doc_new_module}, |
| 2716 | {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held}, |
| 2717 | {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock}, |
| 2718 | {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock}, |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2719 | /* The rest are obsolete */ |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 2720 | {"get_frozen_object", imp_get_frozen_object, METH_VARARGS}, |
| 2721 | {"init_builtin", imp_init_builtin, METH_VARARGS}, |
| 2722 | {"init_frozen", imp_init_frozen, METH_VARARGS}, |
| 2723 | {"is_builtin", imp_is_builtin, METH_VARARGS}, |
| 2724 | {"is_frozen", imp_is_frozen, METH_VARARGS}, |
| 2725 | {"load_compiled", imp_load_compiled, METH_VARARGS}, |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 2726 | #ifdef HAVE_DYNAMIC_LOADING |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 2727 | {"load_dynamic", imp_load_dynamic, METH_VARARGS}, |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 2728 | #endif |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 2729 | {"load_package", imp_load_package, METH_VARARGS}, |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 2730 | {"load_source", imp_load_source, METH_VARARGS}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2731 | {NULL, NULL} /* sentinel */ |
| 2732 | }; |
| 2733 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 2734 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2735 | setint(PyObject *d, char *name, int value) |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2736 | { |
| 2737 | PyObject *v; |
| 2738 | int err; |
| 2739 | |
| 2740 | v = PyInt_FromLong((long)value); |
| 2741 | err = PyDict_SetItemString(d, name, v); |
| 2742 | Py_XDECREF(v); |
| 2743 | return err; |
| 2744 | } |
| 2745 | |
Jason Tishler | 6bc06ec | 2003-09-04 11:59:50 +0000 | [diff] [blame] | 2746 | PyMODINIT_FUNC |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2747 | initimp(void) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2748 | { |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2749 | PyObject *m, *d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2750 | |
Guido van Rossum | 0207e6d | 1997-09-09 22:04:42 +0000 | [diff] [blame] | 2751 | m = Py_InitModule4("imp", imp_methods, doc_imp, |
| 2752 | NULL, PYTHON_API_VERSION); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2753 | d = PyModule_GetDict(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2754 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2755 | if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure; |
| 2756 | if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure; |
| 2757 | if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure; |
| 2758 | if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure; |
| 2759 | if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure; |
| 2760 | if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure; |
| 2761 | if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure; |
| 2762 | if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure; |
Guido van Rossum | 0f84a34 | 1998-08-06 13:36:01 +0000 | [diff] [blame] | 2763 | if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 2764 | if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2765 | |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 2766 | failure: |
| 2767 | ; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2768 | } |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2769 | |
| 2770 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2771 | /* API for embedding applications that want to add their own entries |
| 2772 | to the table of built-in modules. This should normally be called |
| 2773 | *before* Py_Initialize(). When the table resize fails, -1 is |
| 2774 | returned and the existing table is unchanged. |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2775 | |
| 2776 | After a similar function by Just van Rossum. */ |
| 2777 | |
| 2778 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2779 | PyImport_ExtendInittab(struct _inittab *newtab) |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2780 | { |
| 2781 | static struct _inittab *our_copy = NULL; |
| 2782 | struct _inittab *p; |
| 2783 | int i, n; |
| 2784 | |
| 2785 | /* Count the number of entries in both tables */ |
| 2786 | for (n = 0; newtab[n].name != NULL; n++) |
| 2787 | ; |
| 2788 | if (n == 0) |
| 2789 | return 0; /* Nothing to do */ |
| 2790 | for (i = 0; PyImport_Inittab[i].name != NULL; i++) |
| 2791 | ; |
| 2792 | |
| 2793 | /* Allocate new memory for the combined table */ |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2794 | p = our_copy; |
| 2795 | PyMem_RESIZE(p, struct _inittab, i+n+1); |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2796 | if (p == NULL) |
| 2797 | return -1; |
| 2798 | |
| 2799 | /* Copy the tables into the new memory */ |
| 2800 | if (our_copy != PyImport_Inittab) |
| 2801 | memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab)); |
| 2802 | PyImport_Inittab = our_copy = p; |
| 2803 | memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab)); |
| 2804 | |
| 2805 | return 0; |
| 2806 | } |
| 2807 | |
| 2808 | /* Shorthand to add a single entry given a name and a function */ |
| 2809 | |
| 2810 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2811 | PyImport_AppendInittab(char *name, void (*initfunc)(void)) |
Guido van Rossum | 09cae1f | 1998-05-14 02:32:54 +0000 | [diff] [blame] | 2812 | { |
| 2813 | struct _inittab newtab[2]; |
| 2814 | |
| 2815 | memset(newtab, '\0', sizeof newtab); |
| 2816 | |
| 2817 | newtab[0].name = name; |
| 2818 | newtab[0].initfunc = initfunc; |
| 2819 | |
| 2820 | return PyImport_ExtendInittab(newtab); |
| 2821 | } |