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