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