1 2 /* Module definition and import implementation */ 3 4 #include "Python.h" 5 6 #include "Python-ast.h" 7 #undef Yield /* undefine macro conflicting with winbase.h */ 8 #include "errcode.h" 9 #include "marshal.h" 10 #include "code.h" 11 #include "frameobject.h" 12 #include "osdefs.h" 13 #include "importdl.h" 14 15 #ifdef HAVE_FCNTL_H 16 #include <fcntl.h> 17 #endif 18 #ifdef __cplusplus 19 extern "C" { 20 #endif 21 22 #define CACHEDIR "__pycache__" 23 24 /* See _PyImport_FixupExtensionObject() below */ 25 static PyObject *extensions = NULL; 26 27 /* This table is defined in config.c: */ 28 extern struct _inittab _PyImport_Inittab[]; 29 30 struct _inittab *PyImport_Inittab = _PyImport_Inittab; 31 32 static PyObject *initstr = NULL; 33 34 /*[clinic input] 35 module _imp 36 [clinic start generated code]*/ 37 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=9c332475d8686284]*/ 38 39 #include "clinic/import.c.h" 40 41 /* Initialize things */ 42 43 void 44 _PyImport_Init(void) 45 { 46 PyInterpreterState *interp = PyThreadState_Get()->interp; 47 initstr = PyUnicode_InternFromString("__init__"); 48 if (initstr == NULL) 49 Py_FatalError("Can't initialize import variables"); 50 interp->builtins_copy = PyDict_Copy(interp->builtins); 51 if (interp->builtins_copy == NULL) 52 Py_FatalError("Can't backup builtins dict"); 53 } 54 55 void 56 _PyImportHooks_Init(void) 57 { 58 PyObject *v, *path_hooks = NULL; 59 int err = 0; 60 61 /* adding sys.path_hooks and sys.path_importer_cache */ 62 v = PyList_New(0); 63 if (v == NULL) 64 goto error; 65 err = PySys_SetObject("meta_path", v); 66 Py_DECREF(v); 67 if (err) 68 goto error; 69 v = PyDict_New(); 70 if (v == NULL) 71 goto error; 72 err = PySys_SetObject("path_importer_cache", v); 73 Py_DECREF(v); 74 if (err) 75 goto error; 76 path_hooks = PyList_New(0); 77 if (path_hooks == NULL) 78 goto error; 79 err = PySys_SetObject("path_hooks", path_hooks); 80 if (err) { 81 error: 82 PyErr_Print(); 83 Py_FatalError("initializing sys.meta_path, sys.path_hooks, " 84 "or path_importer_cache failed"); 85 } 86 Py_DECREF(path_hooks); 87 } 88 89 void 90 _PyImportZip_Init(void) 91 { 92 PyObject *path_hooks, *zimpimport; 93 int err = 0; 94 95 path_hooks = PySys_GetObject("path_hooks"); 96 if (path_hooks == NULL) { 97 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path_hooks"); 98 goto error; 99 } 100 101 if (Py_VerboseFlag) 102 PySys_WriteStderr("# installing zipimport hook\n"); 103 104 zimpimport = PyImport_ImportModule("zipimport"); 105 if (zimpimport == NULL) { 106 PyErr_Clear(); /* No zip import module -- okay */ 107 if (Py_VerboseFlag) 108 PySys_WriteStderr("# can't import zipimport\n"); 109 } 110 else { 111 _Py_IDENTIFIER(zipimporter); 112 PyObject *zipimporter = _PyObject_GetAttrId(zimpimport, 113 &PyId_zipimporter); 114 Py_DECREF(zimpimport); 115 if (zipimporter == NULL) { 116 PyErr_Clear(); /* No zipimporter object -- okay */ 117 if (Py_VerboseFlag) 118 PySys_WriteStderr( 119 "# can't import zipimport.zipimporter\n"); 120 } 121 else { 122 /* sys.path_hooks.insert(0, zipimporter) */ 123 err = PyList_Insert(path_hooks, 0, zipimporter); 124 Py_DECREF(zipimporter); 125 if (err < 0) { 126 goto error; 127 } 128 if (Py_VerboseFlag) 129 PySys_WriteStderr( 130 "# installed zipimport hook\n"); 131 } 132 } 133 134 return; 135 136 error: 137 PyErr_Print(); 138 Py_FatalError("initializing zipimport failed"); 139 } 140 141 /* Locking primitives to prevent parallel imports of the same module 142 in different threads to return with a partially loaded module. 143 These calls are serialized by the global interpreter lock. */ 144 145 #ifdef WITH_THREAD 146 147 #include "pythread.h" 148 149 static PyThread_type_lock import_lock = 0; 150 static long import_lock_thread = -1; 151 static int import_lock_level = 0; 152 153 void 154 _PyImport_AcquireLock(void) 155 { 156 long me = PyThread_get_thread_ident(); 157 if (me == -1) 158 return; /* Too bad */ 159 if (import_lock == NULL) { 160 import_lock = PyThread_allocate_lock(); 161 if (import_lock == NULL) 162 return; /* Nothing much we can do. */ 163 } 164 if (import_lock_thread == me) { 165 import_lock_level++; 166 return; 167 } 168 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0)) 169 { 170 PyThreadState *tstate = PyEval_SaveThread(); 171 PyThread_acquire_lock(import_lock, 1); 172 PyEval_RestoreThread(tstate); 173 } 174 assert(import_lock_level == 0); 175 import_lock_thread = me; 176 import_lock_level = 1; 177 } 178 179 int 180 _PyImport_ReleaseLock(void) 181 { 182 long me = PyThread_get_thread_ident(); 183 if (me == -1 || import_lock == NULL) 184 return 0; /* Too bad */ 185 if (import_lock_thread != me) 186 return -1; 187 import_lock_level--; 188 assert(import_lock_level >= 0); 189 if (import_lock_level == 0) { 190 import_lock_thread = -1; 191 PyThread_release_lock(import_lock); 192 } 193 return 1; 194 } 195 196 /* This function is called from PyOS_AfterFork to ensure that newly 197 created child processes do not share locks with the parent. 198 We now acquire the import lock around fork() calls but on some platforms 199 (Solaris 9 and earlier? see isue7242) that still left us with problems. */ 200 201 void 202 _PyImport_ReInitLock(void) 203 { 204 if (import_lock != NULL) { 205 import_lock = PyThread_allocate_lock(); 206 if (import_lock == NULL) { 207 Py_FatalError("PyImport_ReInitLock failed to create a new lock"); 208 } 209 } 210 if (import_lock_level > 1) { 211 /* Forked as a side effect of import */ 212 long me = PyThread_get_thread_ident(); 213 /* The following could fail if the lock is already held, but forking as 214 a side-effect of an import is a) rare, b) nuts, and c) difficult to 215 do thanks to the lock only being held when doing individual module 216 locks per import. */ 217 PyThread_acquire_lock(import_lock, NOWAIT_LOCK); 218 import_lock_thread = me; 219 import_lock_level--; 220 } else { 221 import_lock_thread = -1; 222 import_lock_level = 0; 223 } 224 } 225 226 #endif 227 228 /*[clinic input] 229 _imp.lock_held 230 231 Return True if the import lock is currently held, else False. 232 233 On platforms without threads, return False. 234 [clinic start generated code]*/ 235 236 static PyObject * 237 _imp_lock_held_impl(PyObject *module) 238 /*[clinic end generated code: output=8b89384b5e1963fc input=9b088f9b217d9bdf]*/ 239 { 240 #ifdef WITH_THREAD 241 return PyBool_FromLong(import_lock_thread != -1); 242 #else 243 return PyBool_FromLong(0); 244 #endif 245 } 246 247 /*[clinic input] 248 _imp.acquire_lock 249 250 Acquires the interpreter's import lock for the current thread. 251 252 This lock should be used by import hooks to ensure thread-safety when importing 253 modules. On platforms without threads, this function does nothing. 254 [clinic start generated code]*/ 255 256 static PyObject * 257 _imp_acquire_lock_impl(PyObject *module) 258 /*[clinic end generated code: output=1aff58cb0ee1b026 input=4a2d4381866d5fdc]*/ 259 { 260 #ifdef WITH_THREAD 261 _PyImport_AcquireLock(); 262 #endif 263 Py_INCREF(Py_None); 264 return Py_None; 265 } 266 267 /*[clinic input] 268 _imp.release_lock 269 270 Release the interpreter's import lock. 271 272 On platforms without threads, this function does nothing. 273 [clinic start generated code]*/ 274 275 static PyObject * 276 _imp_release_lock_impl(PyObject *module) 277 /*[clinic end generated code: output=7faab6d0be178b0a input=934fb11516dd778b]*/ 278 { 279 #ifdef WITH_THREAD 280 if (_PyImport_ReleaseLock() < 0) { 281 PyErr_SetString(PyExc_RuntimeError, 282 "not holding the import lock"); 283 return NULL; 284 } 285 #endif 286 Py_INCREF(Py_None); 287 return Py_None; 288 } 289 290 void 291 _PyImport_Fini(void) 292 { 293 Py_CLEAR(extensions); 294 #ifdef WITH_THREAD 295 if (import_lock != NULL) { 296 PyThread_free_lock(import_lock); 297 import_lock = NULL; 298 } 299 #endif 300 } 301 302 /* Helper for sys */ 303 304 PyObject * 305 PyImport_GetModuleDict(void) 306 { 307 PyInterpreterState *interp = PyThreadState_GET()->interp; 308 if (interp->modules == NULL) 309 Py_FatalError("PyImport_GetModuleDict: no module dictionary!"); 310 return interp->modules; 311 } 312 313 314 /* List of names to clear in sys */ 315 static const char * const sys_deletes[] = { 316 "path", "argv", "ps1", "ps2", 317 "last_type", "last_value", "last_traceback", 318 "path_hooks", "path_importer_cache", "meta_path", 319 "__interactivehook__", 320 /* misc stuff */ 321 "flags", "float_info", 322 NULL 323 }; 324 325 static const char * const sys_files[] = { 326 "stdin", "__stdin__", 327 "stdout", "__stdout__", 328 "stderr", "__stderr__", 329 NULL 330 }; 331 332 /* Un-initialize things, as good as we can */ 333 334 void 335 PyImport_Cleanup(void) 336 { 337 Py_ssize_t pos; 338 PyObject *key, *value, *dict; 339 PyInterpreterState *interp = PyThreadState_GET()->interp; 340 PyObject *modules = interp->modules; 341 PyObject *weaklist = NULL; 342 const char * const *p; 343 344 if (modules == NULL) 345 return; /* Already done */ 346 347 /* Delete some special variables first. These are common 348 places where user values hide and people complain when their 349 destructors fail. Since the modules containing them are 350 deleted *last* of all, they would come too late in the normal 351 destruction order. Sigh. */ 352 353 /* XXX Perhaps these precautions are obsolete. Who knows? */ 354 355 if (Py_VerboseFlag) 356 PySys_WriteStderr("# clear builtins._\n"); 357 PyDict_SetItemString(interp->builtins, "_", Py_None); 358 359 for (p = sys_deletes; *p != NULL; p++) { 360 if (Py_VerboseFlag) 361 PySys_WriteStderr("# clear sys.%s\n", *p); 362 PyDict_SetItemString(interp->sysdict, *p, Py_None); 363 } 364 for (p = sys_files; *p != NULL; p+=2) { 365 if (Py_VerboseFlag) 366 PySys_WriteStderr("# restore sys.%s\n", *p); 367 value = PyDict_GetItemString(interp->sysdict, *(p+1)); 368 if (value == NULL) 369 value = Py_None; 370 PyDict_SetItemString(interp->sysdict, *p, value); 371 } 372 373 /* We prepare a list which will receive (name, weakref) tuples of 374 modules when they are removed from sys.modules. The name is used 375 for diagnosis messages (in verbose mode), while the weakref helps 376 detect those modules which have been held alive. */ 377 weaklist = PyList_New(0); 378 if (weaklist == NULL) 379 PyErr_Clear(); 380 381 #define STORE_MODULE_WEAKREF(name, mod) \ 382 if (weaklist != NULL) { \ 383 PyObject *wr = PyWeakref_NewRef(mod, NULL); \ 384 if (name && wr) { \ 385 PyObject *tup = PyTuple_Pack(2, name, wr); \ 386 PyList_Append(weaklist, tup); \ 387 Py_XDECREF(tup); \ 388 } \ 389 Py_XDECREF(wr); \ 390 if (PyErr_Occurred()) \ 391 PyErr_Clear(); \ 392 } 393 394 /* Remove all modules from sys.modules, hoping that garbage collection 395 can reclaim most of them. */ 396 pos = 0; 397 while (PyDict_Next(modules, &pos, &key, &value)) { 398 if (PyModule_Check(value)) { 399 if (Py_VerboseFlag && PyUnicode_Check(key)) 400 PySys_FormatStderr("# cleanup[2] removing %U\n", key); 401 STORE_MODULE_WEAKREF(key, value); 402 PyDict_SetItem(modules, key, Py_None); 403 } 404 } 405 406 /* Clear the modules dict. */ 407 PyDict_Clear(modules); 408 /* Restore the original builtins dict, to ensure that any 409 user data gets cleared. */ 410 dict = PyDict_Copy(interp->builtins); 411 if (dict == NULL) 412 PyErr_Clear(); 413 PyDict_Clear(interp->builtins); 414 if (PyDict_Update(interp->builtins, interp->builtins_copy)) 415 PyErr_Clear(); 416 Py_XDECREF(dict); 417 /* Clear module dict copies stored in the interpreter state */ 418 _PyState_ClearModules(); 419 /* Collect references */ 420 _PyGC_CollectNoFail(); 421 /* Dump GC stats before it's too late, since it uses the warnings 422 machinery. */ 423 _PyGC_DumpShutdownStats(); 424 425 /* Now, if there are any modules left alive, clear their globals to 426 minimize potential leaks. All C extension modules actually end 427 up here, since they are kept alive in the interpreter state. 428 429 The special treatment of "builtins" here is because even 430 when it's not referenced as a module, its dictionary is 431 referenced by almost every module's __builtins__. Since 432 deleting a module clears its dictionary (even if there are 433 references left to it), we need to delete the "builtins" 434 module last. Likewise, we don't delete sys until the very 435 end because it is implicitly referenced (e.g. by print). */ 436 if (weaklist != NULL) { 437 Py_ssize_t i, n; 438 n = PyList_GET_SIZE(weaklist); 439 for (i = 0; i < n; i++) { 440 PyObject *tup = PyList_GET_ITEM(weaklist, i); 441 PyObject *name = PyTuple_GET_ITEM(tup, 0); 442 PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1)); 443 if (mod == Py_None) 444 continue; 445 assert(PyModule_Check(mod)); 446 dict = PyModule_GetDict(mod); 447 if (dict == interp->builtins || dict == interp->sysdict) 448 continue; 449 Py_INCREF(mod); 450 if (Py_VerboseFlag && PyUnicode_Check(name)) 451 PySys_FormatStderr("# cleanup[3] wiping %U\n", name); 452 _PyModule_Clear(mod); 453 Py_DECREF(mod); 454 } 455 Py_DECREF(weaklist); 456 } 457 458 /* Next, delete sys and builtins (in that order) */ 459 if (Py_VerboseFlag) 460 PySys_FormatStderr("# cleanup[3] wiping sys\n"); 461 _PyModule_ClearDict(interp->sysdict); 462 if (Py_VerboseFlag) 463 PySys_FormatStderr("# cleanup[3] wiping builtins\n"); 464 _PyModule_ClearDict(interp->builtins); 465 466 /* Clear and delete the modules directory. Actual modules will 467 still be there only if imported during the execution of some 468 destructor. */ 469 interp->modules = NULL; 470 Py_DECREF(modules); 471 472 /* Once more */ 473 _PyGC_CollectNoFail(); 474 475 #undef STORE_MODULE_WEAKREF 476 } 477 478 479 /* Helper for pythonrun.c -- return magic number and tag. */ 480 481 long 482 PyImport_GetMagicNumber(void) 483 { 484 long res; 485 PyInterpreterState *interp = PyThreadState_Get()->interp; 486 PyObject *external, *pyc_magic; 487 488 external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external"); 489 if (external == NULL) 490 return -1; 491 pyc_magic = PyObject_GetAttrString(external, "_RAW_MAGIC_NUMBER"); 492 Py_DECREF(external); 493 if (pyc_magic == NULL) 494 return -1; 495 res = PyLong_AsLong(pyc_magic); 496 Py_DECREF(pyc_magic); 497 return res; 498 } 499 500 501 extern const char * _PySys_ImplCacheTag; 502 503 const char * 504 PyImport_GetMagicTag(void) 505 { 506 return _PySys_ImplCacheTag; 507 } 508 509 510 /* Magic for extension modules (built-in as well as dynamically 511 loaded). To prevent initializing an extension module more than 512 once, we keep a static dictionary 'extensions' keyed by the tuple 513 (module name, module name) (for built-in modules) or by 514 (filename, module name) (for dynamically loaded modules), containing these 515 modules. A copy of the module's dictionary is stored by calling 516 _PyImport_FixupExtensionObject() immediately after the module initialization 517 function succeeds. A copy can be retrieved from there by calling 518 _PyImport_FindExtensionObject(). 519 520 Modules which do support multiple initialization set their m_size 521 field to a non-negative number (indicating the size of the 522 module-specific state). They are still recorded in the extensions 523 dictionary, to avoid loading shared libraries twice. 524 */ 525 526 int 527 _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name, 528 PyObject *filename) 529 { 530 PyObject *modules, *dict, *key; 531 struct PyModuleDef *def; 532 int res; 533 if (extensions == NULL) { 534 extensions = PyDict_New(); 535 if (extensions == NULL) 536 return -1; 537 } 538 if (mod == NULL || !PyModule_Check(mod)) { 539 PyErr_BadInternalCall(); 540 return -1; 541 } 542 def = PyModule_GetDef(mod); 543 if (!def) { 544 PyErr_BadInternalCall(); 545 return -1; 546 } 547 modules = PyImport_GetModuleDict(); 548 if (PyDict_SetItem(modules, name, mod) < 0) 549 return -1; 550 if (_PyState_AddModule(mod, def) < 0) { 551 PyDict_DelItem(modules, name); 552 return -1; 553 } 554 if (def->m_size == -1) { 555 if (def->m_base.m_copy) { 556 /* Somebody already imported the module, 557 likely under a different name. 558 XXX this should really not happen. */ 559 Py_CLEAR(def->m_base.m_copy); 560 } 561 dict = PyModule_GetDict(mod); 562 if (dict == NULL) 563 return -1; 564 def->m_base.m_copy = PyDict_Copy(dict); 565 if (def->m_base.m_copy == NULL) 566 return -1; 567 } 568 key = PyTuple_Pack(2, filename, name); 569 if (key == NULL) 570 return -1; 571 res = PyDict_SetItem(extensions, key, (PyObject *)def); 572 Py_DECREF(key); 573 if (res < 0) 574 return -1; 575 return 0; 576 } 577 578 int 579 _PyImport_FixupBuiltin(PyObject *mod, const char *name) 580 { 581 int res; 582 PyObject *nameobj; 583 nameobj = PyUnicode_InternFromString(name); 584 if (nameobj == NULL) 585 return -1; 586 res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj); 587 Py_DECREF(nameobj); 588 return res; 589 } 590 591 PyObject * 592 _PyImport_FindExtensionObject(PyObject *name, PyObject *filename) 593 { 594 PyObject *mod, *mdict, *key; 595 PyModuleDef* def; 596 if (extensions == NULL) 597 return NULL; 598 key = PyTuple_Pack(2, filename, name); 599 if (key == NULL) 600 return NULL; 601 def = (PyModuleDef *)PyDict_GetItem(extensions, key); 602 Py_DECREF(key); 603 if (def == NULL) 604 return NULL; 605 if (def->m_size == -1) { 606 /* Module does not support repeated initialization */ 607 if (def->m_base.m_copy == NULL) 608 return NULL; 609 mod = PyImport_AddModuleObject(name); 610 if (mod == NULL) 611 return NULL; 612 mdict = PyModule_GetDict(mod); 613 if (mdict == NULL) 614 return NULL; 615 if (PyDict_Update(mdict, def->m_base.m_copy)) 616 return NULL; 617 } 618 else { 619 if (def->m_base.m_init == NULL) 620 return NULL; 621 mod = def->m_base.m_init(); 622 if (mod == NULL) 623 return NULL; 624 if (PyDict_SetItem(PyImport_GetModuleDict(), name, mod) == -1) { 625 Py_DECREF(mod); 626 return NULL; 627 } 628 Py_DECREF(mod); 629 } 630 if (_PyState_AddModule(mod, def) < 0) { 631 PyDict_DelItem(PyImport_GetModuleDict(), name); 632 Py_DECREF(mod); 633 return NULL; 634 } 635 if (Py_VerboseFlag) 636 PySys_FormatStderr("import %U # previously loaded (%R)\n", 637 name, filename); 638 return mod; 639 640 } 641 642 PyObject * 643 _PyImport_FindBuiltin(const char *name) 644 { 645 PyObject *res, *nameobj; 646 nameobj = PyUnicode_InternFromString(name); 647 if (nameobj == NULL) 648 return NULL; 649 res = _PyImport_FindExtensionObject(nameobj, nameobj); 650 Py_DECREF(nameobj); 651 return res; 652 } 653 654 /* Get the module object corresponding to a module name. 655 First check the modules dictionary if there's one there, 656 if not, create a new one and insert it in the modules dictionary. 657 Because the former action is most common, THIS DOES NOT RETURN A 658 'NEW' REFERENCE! */ 659 660 PyObject * 661 PyImport_AddModuleObject(PyObject *name) 662 { 663 PyObject *modules = PyImport_GetModuleDict(); 664 PyObject *m; 665 666 if ((m = PyDict_GetItemWithError(modules, name)) != NULL && 667 PyModule_Check(m)) { 668 return m; 669 } 670 if (PyErr_Occurred()) { 671 return NULL; 672 } 673 m = PyModule_NewObject(name); 674 if (m == NULL) 675 return NULL; 676 if (PyDict_SetItem(modules, name, m) != 0) { 677 Py_DECREF(m); 678 return NULL; 679 } 680 Py_DECREF(m); /* Yes, it still exists, in modules! */ 681 682 return m; 683 } 684 685 PyObject * 686 PyImport_AddModule(const char *name) 687 { 688 PyObject *nameobj, *module; 689 nameobj = PyUnicode_FromString(name); 690 if (nameobj == NULL) 691 return NULL; 692 module = PyImport_AddModuleObject(nameobj); 693 Py_DECREF(nameobj); 694 return module; 695 } 696 697 698 /* Remove name from sys.modules, if it's there. */ 699 static void 700 remove_module(PyObject *name) 701 { 702 PyObject *modules = PyImport_GetModuleDict(); 703 if (PyDict_GetItem(modules, name) == NULL) 704 return; 705 if (PyDict_DelItem(modules, name) < 0) 706 Py_FatalError("import: deleting existing key in" 707 "sys.modules failed"); 708 } 709 710 711 /* Execute a code object in a module and return the module object 712 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is 713 * removed from sys.modules, to avoid leaving damaged module objects 714 * in sys.modules. The caller may wish to restore the original 715 * module object (if any) in this case; PyImport_ReloadModule is an 716 * example. 717 * 718 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer 719 * interface. The other two exist primarily for backward compatibility. 720 */ 721 PyObject * 722 PyImport_ExecCodeModule(const char *name, PyObject *co) 723 { 724 return PyImport_ExecCodeModuleWithPathnames( 725 name, co, (char *)NULL, (char *)NULL); 726 } 727 728 PyObject * 729 PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname) 730 { 731 return PyImport_ExecCodeModuleWithPathnames( 732 name, co, pathname, (char *)NULL); 733 } 734 735 PyObject * 736 PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co, 737 const char *pathname, 738 const char *cpathname) 739 { 740 PyObject *m = NULL; 741 PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL, *external= NULL; 742 743 nameobj = PyUnicode_FromString(name); 744 if (nameobj == NULL) 745 return NULL; 746 747 if (cpathname != NULL) { 748 cpathobj = PyUnicode_DecodeFSDefault(cpathname); 749 if (cpathobj == NULL) 750 goto error; 751 } 752 else 753 cpathobj = NULL; 754 755 if (pathname != NULL) { 756 pathobj = PyUnicode_DecodeFSDefault(pathname); 757 if (pathobj == NULL) 758 goto error; 759 } 760 else if (cpathobj != NULL) { 761 PyInterpreterState *interp = PyThreadState_GET()->interp; 762 _Py_IDENTIFIER(_get_sourcefile); 763 764 if (interp == NULL) { 765 Py_FatalError("PyImport_ExecCodeModuleWithPathnames: " 766 "no interpreter!"); 767 } 768 769 external= PyObject_GetAttrString(interp->importlib, 770 "_bootstrap_external"); 771 if (external != NULL) { 772 pathobj = _PyObject_CallMethodIdObjArgs(external, 773 &PyId__get_sourcefile, cpathobj, 774 NULL); 775 Py_DECREF(external); 776 } 777 if (pathobj == NULL) 778 PyErr_Clear(); 779 } 780 else 781 pathobj = NULL; 782 783 m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj); 784 error: 785 Py_DECREF(nameobj); 786 Py_XDECREF(pathobj); 787 Py_XDECREF(cpathobj); 788 return m; 789 } 790 791 static PyObject * 792 module_dict_for_exec(PyObject *name) 793 { 794 PyObject *m, *d = NULL; 795 796 m = PyImport_AddModuleObject(name); 797 if (m == NULL) 798 return NULL; 799 /* If the module is being reloaded, we get the old module back 800 and re-use its dict to exec the new code. */ 801 d = PyModule_GetDict(m); 802 if (PyDict_GetItemString(d, "__builtins__") == NULL) { 803 if (PyDict_SetItemString(d, "__builtins__", 804 PyEval_GetBuiltins()) != 0) { 805 remove_module(name); 806 return NULL; 807 } 808 } 809 810 return d; /* Return a borrowed reference. */ 811 } 812 813 static PyObject * 814 exec_code_in_module(PyObject *name, PyObject *module_dict, PyObject *code_object) 815 { 816 PyObject *modules = PyImport_GetModuleDict(); 817 PyObject *v, *m; 818 819 v = PyEval_EvalCode(code_object, module_dict, module_dict); 820 if (v == NULL) { 821 remove_module(name); 822 return NULL; 823 } 824 Py_DECREF(v); 825 826 if ((m = PyDict_GetItem(modules, name)) == NULL) { 827 PyErr_Format(PyExc_ImportError, 828 "Loaded module %R not found in sys.modules", 829 name); 830 return NULL; 831 } 832 833 Py_INCREF(m); 834 835 return m; 836 } 837 838 PyObject* 839 PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, 840 PyObject *cpathname) 841 { 842 PyObject *d, *external, *res; 843 PyInterpreterState *interp = PyThreadState_GET()->interp; 844 _Py_IDENTIFIER(_fix_up_module); 845 846 d = module_dict_for_exec(name); 847 if (d == NULL) { 848 return NULL; 849 } 850 851 if (pathname == NULL) { 852 pathname = ((PyCodeObject *)co)->co_filename; 853 } 854 external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external"); 855 if (external == NULL) 856 return NULL; 857 res = _PyObject_CallMethodIdObjArgs(external, 858 &PyId__fix_up_module, 859 d, name, pathname, cpathname, NULL); 860 Py_DECREF(external); 861 if (res != NULL) { 862 Py_DECREF(res); 863 res = exec_code_in_module(name, d, co); 864 } 865 return res; 866 } 867 868 869 static void 870 update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname) 871 { 872 PyObject *constants, *tmp; 873 Py_ssize_t i, n; 874 875 if (PyUnicode_Compare(co->co_filename, oldname)) 876 return; 877 878 Py_INCREF(newname); 879 Py_XSETREF(co->co_filename, newname); 880 881 constants = co->co_consts; 882 n = PyTuple_GET_SIZE(constants); 883 for (i = 0; i < n; i++) { 884 tmp = PyTuple_GET_ITEM(constants, i); 885 if (PyCode_Check(tmp)) 886 update_code_filenames((PyCodeObject *)tmp, 887 oldname, newname); 888 } 889 } 890 891 static void 892 update_compiled_module(PyCodeObject *co, PyObject *newname) 893 { 894 PyObject *oldname; 895 896 if (PyUnicode_Compare(co->co_filename, newname) == 0) 897 return; 898 899 oldname = co->co_filename; 900 Py_INCREF(oldname); 901 update_code_filenames(co, oldname, newname); 902 Py_DECREF(oldname); 903 } 904 905 /*[clinic input] 906 _imp._fix_co_filename 907 908 code: object(type="PyCodeObject *", subclass_of="&PyCode_Type") 909 Code object to change. 910 911 path: unicode 912 File path to use. 913 / 914 915 Changes code.co_filename to specify the passed-in file path. 916 [clinic start generated code]*/ 917 918 static PyObject * 919 _imp__fix_co_filename_impl(PyObject *module, PyCodeObject *code, 920 PyObject *path) 921 /*[clinic end generated code: output=1d002f100235587d input=895ba50e78b82f05]*/ 922 923 { 924 update_compiled_module(code, path); 925 926 Py_RETURN_NONE; 927 } 928 929 930 /* Forward */ 931 static const struct _frozen * find_frozen(PyObject *); 932 933 934 /* Helper to test for built-in module */ 935 936 static int 937 is_builtin(PyObject *name) 938 { 939 int i; 940 for (i = 0; PyImport_Inittab[i].name != NULL; i++) { 941 if (_PyUnicode_EqualToASCIIString(name, PyImport_Inittab[i].name)) { 942 if (PyImport_Inittab[i].initfunc == NULL) 943 return -1; 944 else 945 return 1; 946 } 947 } 948 return 0; 949 } 950 951 952 /* Return a finder object for a sys.path/pkg.__path__ item 'p', 953 possibly by fetching it from the path_importer_cache dict. If it 954 wasn't yet cached, traverse path_hooks until a hook is found 955 that can handle the path item. Return None if no hook could; 956 this tells our caller that the path based finder could not find 957 a finder for this path item. Cache the result in 958 path_importer_cache. 959 Returns a borrowed reference. */ 960 961 static PyObject * 962 get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks, 963 PyObject *p) 964 { 965 PyObject *importer; 966 Py_ssize_t j, nhooks; 967 968 /* These conditions are the caller's responsibility: */ 969 assert(PyList_Check(path_hooks)); 970 assert(PyDict_Check(path_importer_cache)); 971 972 nhooks = PyList_Size(path_hooks); 973 if (nhooks < 0) 974 return NULL; /* Shouldn't happen */ 975 976 importer = PyDict_GetItem(path_importer_cache, p); 977 if (importer != NULL) 978 return importer; 979 980 /* set path_importer_cache[p] to None to avoid recursion */ 981 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0) 982 return NULL; 983 984 for (j = 0; j < nhooks; j++) { 985 PyObject *hook = PyList_GetItem(path_hooks, j); 986 if (hook == NULL) 987 return NULL; 988 importer = PyObject_CallFunctionObjArgs(hook, p, NULL); 989 if (importer != NULL) 990 break; 991 992 if (!PyErr_ExceptionMatches(PyExc_ImportError)) { 993 return NULL; 994 } 995 PyErr_Clear(); 996 } 997 if (importer == NULL) { 998 return Py_None; 999 } 1000 if (importer != NULL) { 1001 int err = PyDict_SetItem(path_importer_cache, p, importer); 1002 Py_DECREF(importer); 1003 if (err != 0) 1004 return NULL; 1005 } 1006 return importer; 1007 } 1008 1009 PyAPI_FUNC(PyObject *) 1010 PyImport_GetImporter(PyObject *path) { 1011 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL; 1012 1013 path_importer_cache = PySys_GetObject("path_importer_cache"); 1014 path_hooks = PySys_GetObject("path_hooks"); 1015 if (path_importer_cache != NULL && path_hooks != NULL) { 1016 importer = get_path_importer(path_importer_cache, 1017 path_hooks, path); 1018 } 1019 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */ 1020 return importer; 1021 } 1022 1023 /*[clinic input] 1024 _imp.create_builtin 1025 1026 spec: object 1027 / 1028 1029 Create an extension module. 1030 [clinic start generated code]*/ 1031 1032 static PyObject * 1033 _imp_create_builtin(PyObject *module, PyObject *spec) 1034 /*[clinic end generated code: output=ace7ff22271e6f39 input=37f966f890384e47]*/ 1035 { 1036 struct _inittab *p; 1037 PyObject *name; 1038 char *namestr; 1039 PyObject *mod; 1040 1041 name = PyObject_GetAttrString(spec, "name"); 1042 if (name == NULL) { 1043 return NULL; 1044 } 1045 1046 mod = _PyImport_FindExtensionObject(name, name); 1047 if (mod || PyErr_Occurred()) { 1048 Py_DECREF(name); 1049 Py_XINCREF(mod); 1050 return mod; 1051 } 1052 1053 namestr = PyUnicode_AsUTF8(name); 1054 if (namestr == NULL) { 1055 Py_DECREF(name); 1056 return NULL; 1057 } 1058 1059 for (p = PyImport_Inittab; p->name != NULL; p++) { 1060 PyModuleDef *def; 1061 if (_PyUnicode_EqualToASCIIString(name, p->name)) { 1062 if (p->initfunc == NULL) { 1063 /* Cannot re-init internal module ("sys" or "builtins") */ 1064 mod = PyImport_AddModule(namestr); 1065 Py_DECREF(name); 1066 return mod; 1067 } 1068 mod = (*p->initfunc)(); 1069 if (mod == NULL) { 1070 Py_DECREF(name); 1071 return NULL; 1072 } 1073 if (PyObject_TypeCheck(mod, &PyModuleDef_Type)) { 1074 Py_DECREF(name); 1075 return PyModule_FromDefAndSpec((PyModuleDef*)mod, spec); 1076 } else { 1077 /* Remember pointer to module init function. */ 1078 def = PyModule_GetDef(mod); 1079 if (def == NULL) { 1080 Py_DECREF(name); 1081 return NULL; 1082 } 1083 def->m_base.m_init = p->initfunc; 1084 if (_PyImport_FixupExtensionObject(mod, name, name) < 0) { 1085 Py_DECREF(name); 1086 return NULL; 1087 } 1088 Py_DECREF(name); 1089 return mod; 1090 } 1091 } 1092 } 1093 Py_DECREF(name); 1094 Py_RETURN_NONE; 1095 } 1096 1097 1098 /* Frozen modules */ 1099 1100 static const struct _frozen * 1101 find_frozen(PyObject *name) 1102 { 1103 const struct _frozen *p; 1104 1105 if (name == NULL) 1106 return NULL; 1107 1108 for (p = PyImport_FrozenModules; ; p++) { 1109 if (p->name == NULL) 1110 return NULL; 1111 if (_PyUnicode_EqualToASCIIString(name, p->name)) 1112 break; 1113 } 1114 return p; 1115 } 1116 1117 static PyObject * 1118 get_frozen_object(PyObject *name) 1119 { 1120 const struct _frozen *p = find_frozen(name); 1121 int size; 1122 1123 if (p == NULL) { 1124 PyErr_Format(PyExc_ImportError, 1125 "No such frozen object named %R", 1126 name); 1127 return NULL; 1128 } 1129 if (p->code == NULL) { 1130 PyErr_Format(PyExc_ImportError, 1131 "Excluded frozen object named %R", 1132 name); 1133 return NULL; 1134 } 1135 size = p->size; 1136 if (size < 0) 1137 size = -size; 1138 return PyMarshal_ReadObjectFromString((const char *)p->code, size); 1139 } 1140 1141 static PyObject * 1142 is_frozen_package(PyObject *name) 1143 { 1144 const struct _frozen *p = find_frozen(name); 1145 int size; 1146 1147 if (p == NULL) { 1148 PyErr_Format(PyExc_ImportError, 1149 "No such frozen object named %R", 1150 name); 1151 return NULL; 1152 } 1153 1154 size = p->size; 1155 1156 if (size < 0) 1157 Py_RETURN_TRUE; 1158 else 1159 Py_RETURN_FALSE; 1160 } 1161 1162 1163 /* Initialize a frozen module. 1164 Return 1 for success, 0 if the module is not found, and -1 with 1165 an exception set if the initialization failed. 1166 This function is also used from frozenmain.c */ 1167 1168 int 1169 PyImport_ImportFrozenModuleObject(PyObject *name) 1170 { 1171 const struct _frozen *p; 1172 PyObject *co, *m, *d; 1173 int ispackage; 1174 int size; 1175 1176 p = find_frozen(name); 1177 1178 if (p == NULL) 1179 return 0; 1180 if (p->code == NULL) { 1181 PyErr_Format(PyExc_ImportError, 1182 "Excluded frozen object named %R", 1183 name); 1184 return -1; 1185 } 1186 size = p->size; 1187 ispackage = (size < 0); 1188 if (ispackage) 1189 size = -size; 1190 co = PyMarshal_ReadObjectFromString((const char *)p->code, size); 1191 if (co == NULL) 1192 return -1; 1193 if (!PyCode_Check(co)) { 1194 PyErr_Format(PyExc_TypeError, 1195 "frozen object %R is not a code object", 1196 name); 1197 goto err_return; 1198 } 1199 if (ispackage) { 1200 /* Set __path__ to the empty list */ 1201 PyObject *l; 1202 int err; 1203 m = PyImport_AddModuleObject(name); 1204 if (m == NULL) 1205 goto err_return; 1206 d = PyModule_GetDict(m); 1207 l = PyList_New(0); 1208 if (l == NULL) { 1209 goto err_return; 1210 } 1211 err = PyDict_SetItemString(d, "__path__", l); 1212 Py_DECREF(l); 1213 if (err != 0) 1214 goto err_return; 1215 } 1216 d = module_dict_for_exec(name); 1217 if (d == NULL) { 1218 goto err_return; 1219 } 1220 m = exec_code_in_module(name, d, co); 1221 if (m == NULL) 1222 goto err_return; 1223 Py_DECREF(co); 1224 Py_DECREF(m); 1225 return 1; 1226 err_return: 1227 Py_DECREF(co); 1228 return -1; 1229 } 1230 1231 int 1232 PyImport_ImportFrozenModule(const char *name) 1233 { 1234 PyObject *nameobj; 1235 int ret; 1236 nameobj = PyUnicode_InternFromString(name); 1237 if (nameobj == NULL) 1238 return -1; 1239 ret = PyImport_ImportFrozenModuleObject(nameobj); 1240 Py_DECREF(nameobj); 1241 return ret; 1242 } 1243 1244 1245 /* Import a module, either built-in, frozen, or external, and return 1246 its module object WITH INCREMENTED REFERENCE COUNT */ 1247 1248 PyObject * 1249 PyImport_ImportModule(const char *name) 1250 { 1251 PyObject *pname; 1252 PyObject *result; 1253 1254 pname = PyUnicode_FromString(name); 1255 if (pname == NULL) 1256 return NULL; 1257 result = PyImport_Import(pname); 1258 Py_DECREF(pname); 1259 return result; 1260 } 1261 1262 /* Import a module without blocking 1263 * 1264 * At first it tries to fetch the module from sys.modules. If the module was 1265 * never loaded before it loads it with PyImport_ImportModule() unless another 1266 * thread holds the import lock. In the latter case the function raises an 1267 * ImportError instead of blocking. 1268 * 1269 * Returns the module object with incremented ref count. 1270 */ 1271 PyObject * 1272 PyImport_ImportModuleNoBlock(const char *name) 1273 { 1274 return PyImport_ImportModule(name); 1275 } 1276 1277 1278 /* Remove importlib frames from the traceback, 1279 * except in Verbose mode. */ 1280 static void 1281 remove_importlib_frames(void) 1282 { 1283 const char *importlib_filename = "<frozen importlib._bootstrap>"; 1284 const char *external_filename = "<frozen importlib._bootstrap_external>"; 1285 const char *remove_frames = "_call_with_frames_removed"; 1286 int always_trim = 0; 1287 int in_importlib = 0; 1288 PyObject *exception, *value, *base_tb, *tb; 1289 PyObject **prev_link, **outer_link = NULL; 1290 1291 /* Synopsis: if it's an ImportError, we trim all importlib chunks 1292 from the traceback. We always trim chunks 1293 which end with a call to "_call_with_frames_removed". */ 1294 1295 PyErr_Fetch(&exception, &value, &base_tb); 1296 if (!exception || Py_VerboseFlag) 1297 goto done; 1298 if (PyType_IsSubtype((PyTypeObject *) exception, 1299 (PyTypeObject *) PyExc_ImportError)) 1300 always_trim = 1; 1301 1302 prev_link = &base_tb; 1303 tb = base_tb; 1304 while (tb != NULL) { 1305 PyTracebackObject *traceback = (PyTracebackObject *)tb; 1306 PyObject *next = (PyObject *) traceback->tb_next; 1307 PyFrameObject *frame = traceback->tb_frame; 1308 PyCodeObject *code = frame->f_code; 1309 int now_in_importlib; 1310 1311 assert(PyTraceBack_Check(tb)); 1312 now_in_importlib = _PyUnicode_EqualToASCIIString(code->co_filename, importlib_filename) || 1313 _PyUnicode_EqualToASCIIString(code->co_filename, external_filename); 1314 if (now_in_importlib && !in_importlib) { 1315 /* This is the link to this chunk of importlib tracebacks */ 1316 outer_link = prev_link; 1317 } 1318 in_importlib = now_in_importlib; 1319 1320 if (in_importlib && 1321 (always_trim || 1322 _PyUnicode_EqualToASCIIString(code->co_name, remove_frames))) { 1323 Py_XINCREF(next); 1324 Py_XSETREF(*outer_link, next); 1325 prev_link = outer_link; 1326 } 1327 else { 1328 prev_link = (PyObject **) &traceback->tb_next; 1329 } 1330 tb = next; 1331 } 1332 done: 1333 PyErr_Restore(exception, value, base_tb); 1334 } 1335 1336 1337 static PyObject * 1338 resolve_name(PyObject *name, PyObject *globals, int level) 1339 { 1340 _Py_IDENTIFIER(__spec__); 1341 _Py_IDENTIFIER(__package__); 1342 _Py_IDENTIFIER(__path__); 1343 _Py_IDENTIFIER(__name__); 1344 _Py_IDENTIFIER(parent); 1345 PyObject *abs_name; 1346 PyObject *package = NULL; 1347 PyObject *spec; 1348 PyInterpreterState *interp = PyThreadState_GET()->interp; 1349 Py_ssize_t last_dot; 1350 PyObject *base; 1351 int level_up; 1352 1353 if (globals == NULL) { 1354 PyErr_SetString(PyExc_KeyError, "'__name__' not in globals"); 1355 goto error; 1356 } 1357 if (!PyDict_Check(globals)) { 1358 PyErr_SetString(PyExc_TypeError, "globals must be a dict"); 1359 goto error; 1360 } 1361 package = _PyDict_GetItemId(globals, &PyId___package__); 1362 if (package == Py_None) { 1363 package = NULL; 1364 } 1365 spec = _PyDict_GetItemId(globals, &PyId___spec__); 1366 1367 if (package != NULL) { 1368 Py_INCREF(package); 1369 if (!PyUnicode_Check(package)) { 1370 PyErr_SetString(PyExc_TypeError, "package must be a string"); 1371 goto error; 1372 } 1373 else if (spec != NULL && spec != Py_None) { 1374 int equal; 1375 PyObject *parent = _PyObject_GetAttrId(spec, &PyId_parent); 1376 if (parent == NULL) { 1377 goto error; 1378 } 1379 1380 equal = PyObject_RichCompareBool(package, parent, Py_EQ); 1381 Py_DECREF(parent); 1382 if (equal < 0) { 1383 goto error; 1384 } 1385 else if (equal == 0) { 1386 if (PyErr_WarnEx(PyExc_ImportWarning, 1387 "__package__ != __spec__.parent", 1) < 0) { 1388 goto error; 1389 } 1390 } 1391 } 1392 } 1393 else if (spec != NULL && spec != Py_None) { 1394 package = _PyObject_GetAttrId(spec, &PyId_parent); 1395 if (package == NULL) { 1396 goto error; 1397 } 1398 else if (!PyUnicode_Check(package)) { 1399 PyErr_SetString(PyExc_TypeError, 1400 "__spec__.parent must be a string"); 1401 goto error; 1402 } 1403 } 1404 else { 1405 if (PyErr_WarnEx(PyExc_ImportWarning, 1406 "can't resolve package from __spec__ or __package__, " 1407 "falling back on __name__ and __path__", 1) < 0) { 1408 goto error; 1409 } 1410 1411 package = _PyDict_GetItemId(globals, &PyId___name__); 1412 if (package == NULL) { 1413 PyErr_SetString(PyExc_KeyError, "'__name__' not in globals"); 1414 goto error; 1415 } 1416 1417 Py_INCREF(package); 1418 if (!PyUnicode_Check(package)) { 1419 PyErr_SetString(PyExc_TypeError, "__name__ must be a string"); 1420 goto error; 1421 } 1422 1423 if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) { 1424 Py_ssize_t dot; 1425 1426 if (PyUnicode_READY(package) < 0) { 1427 goto error; 1428 } 1429 1430 dot = PyUnicode_FindChar(package, '.', 1431 0, PyUnicode_GET_LENGTH(package), -1); 1432 if (dot == -2) { 1433 goto error; 1434 } 1435 1436 if (dot >= 0) { 1437 PyObject *substr = PyUnicode_Substring(package, 0, dot); 1438 if (substr == NULL) { 1439 goto error; 1440 } 1441 Py_SETREF(package, substr); 1442 } 1443 } 1444 } 1445 1446 last_dot = PyUnicode_GET_LENGTH(package); 1447 if (last_dot == 0) { 1448 PyErr_SetString(PyExc_ImportError, 1449 "attempted relative import with no known parent package"); 1450 goto error; 1451 } 1452 else if (PyDict_GetItem(interp->modules, package) == NULL) { 1453 PyErr_Format(PyExc_SystemError, 1454 "Parent module %R not loaded, cannot perform relative " 1455 "import", package); 1456 goto error; 1457 } 1458 1459 for (level_up = 1; level_up < level; level_up += 1) { 1460 last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1); 1461 if (last_dot == -2) { 1462 goto error; 1463 } 1464 else if (last_dot == -1) { 1465 PyErr_SetString(PyExc_ValueError, 1466 "attempted relative import beyond top-level " 1467 "package"); 1468 goto error; 1469 } 1470 } 1471 1472 base = PyUnicode_Substring(package, 0, last_dot); 1473 Py_DECREF(package); 1474 if (base == NULL || PyUnicode_GET_LENGTH(name) == 0) { 1475 return base; 1476 } 1477 1478 abs_name = PyUnicode_FromFormat("%U.%U", base, name); 1479 Py_DECREF(base); 1480 return abs_name; 1481 1482 error: 1483 Py_XDECREF(package); 1484 return NULL; 1485 } 1486 1487 PyObject * 1488 PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, 1489 PyObject *locals, PyObject *fromlist, 1490 int level) 1491 { 1492 _Py_IDENTIFIER(_find_and_load); 1493 _Py_IDENTIFIER(_handle_fromlist); 1494 PyObject *abs_name = NULL; 1495 PyObject *final_mod = NULL; 1496 PyObject *mod = NULL; 1497 PyObject *package = NULL; 1498 PyInterpreterState *interp = PyThreadState_GET()->interp; 1499 int has_from; 1500 1501 if (name == NULL) { 1502 PyErr_SetString(PyExc_ValueError, "Empty module name"); 1503 goto error; 1504 } 1505 1506 /* The below code is importlib.__import__() & _gcd_import(), ported to C 1507 for added performance. */ 1508 1509 if (!PyUnicode_Check(name)) { 1510 PyErr_SetString(PyExc_TypeError, "module name must be a string"); 1511 goto error; 1512 } 1513 if (PyUnicode_READY(name) < 0) { 1514 goto error; 1515 } 1516 if (level < 0) { 1517 PyErr_SetString(PyExc_ValueError, "level must be >= 0"); 1518 goto error; 1519 } 1520 1521 if (level > 0) { 1522 abs_name = resolve_name(name, globals, level); 1523 if (abs_name == NULL) 1524 goto error; 1525 } 1526 else { /* level == 0 */ 1527 if (PyUnicode_GET_LENGTH(name) == 0) { 1528 PyErr_SetString(PyExc_ValueError, "Empty module name"); 1529 goto error; 1530 } 1531 abs_name = name; 1532 Py_INCREF(abs_name); 1533 } 1534 1535 mod = PyDict_GetItem(interp->modules, abs_name); 1536 if (mod == Py_None) { 1537 PyObject *msg = PyUnicode_FromFormat("import of %R halted; " 1538 "None in sys.modules", abs_name); 1539 if (msg != NULL) { 1540 PyErr_SetImportErrorSubclass(PyExc_ModuleNotFoundError, msg, 1541 abs_name, NULL); 1542 Py_DECREF(msg); 1543 } 1544 mod = NULL; 1545 goto error; 1546 } 1547 else if (mod != NULL) { 1548 _Py_IDENTIFIER(__spec__); 1549 _Py_IDENTIFIER(_initializing); 1550 _Py_IDENTIFIER(_lock_unlock_module); 1551 PyObject *value = NULL; 1552 PyObject *spec; 1553 int initializing = 0; 1554 1555 Py_INCREF(mod); 1556 /* Optimization: only call _bootstrap._lock_unlock_module() if 1557 __spec__._initializing is true. 1558 NOTE: because of this, initializing must be set *before* 1559 stuffing the new module in sys.modules. 1560 */ 1561 spec = _PyObject_GetAttrId(mod, &PyId___spec__); 1562 if (spec != NULL) { 1563 value = _PyObject_GetAttrId(spec, &PyId__initializing); 1564 Py_DECREF(spec); 1565 } 1566 if (value == NULL) 1567 PyErr_Clear(); 1568 else { 1569 initializing = PyObject_IsTrue(value); 1570 Py_DECREF(value); 1571 if (initializing == -1) 1572 PyErr_Clear(); 1573 if (initializing > 0) { 1574 #ifdef WITH_THREAD 1575 _PyImport_AcquireLock(); 1576 #endif 1577 /* _bootstrap._lock_unlock_module() releases the import lock */ 1578 value = _PyObject_CallMethodIdObjArgs(interp->importlib, 1579 &PyId__lock_unlock_module, abs_name, 1580 NULL); 1581 if (value == NULL) 1582 goto error; 1583 Py_DECREF(value); 1584 } 1585 } 1586 } 1587 else { 1588 #ifdef WITH_THREAD 1589 _PyImport_AcquireLock(); 1590 #endif 1591 /* _bootstrap._find_and_load() releases the import lock */ 1592 mod = _PyObject_CallMethodIdObjArgs(interp->importlib, 1593 &PyId__find_and_load, abs_name, 1594 interp->import_func, NULL); 1595 if (mod == NULL) { 1596 goto error; 1597 } 1598 } 1599 1600 has_from = 0; 1601 if (fromlist != NULL && fromlist != Py_None) { 1602 has_from = PyObject_IsTrue(fromlist); 1603 if (has_from < 0) 1604 goto error; 1605 } 1606 if (!has_from) { 1607 Py_ssize_t len = PyUnicode_GET_LENGTH(name); 1608 if (level == 0 || len > 0) { 1609 Py_ssize_t dot; 1610 1611 dot = PyUnicode_FindChar(name, '.', 0, len, 1); 1612 if (dot == -2) { 1613 goto error; 1614 } 1615 1616 if (dot == -1) { 1617 /* No dot in module name, simple exit */ 1618 final_mod = mod; 1619 Py_INCREF(mod); 1620 goto error; 1621 } 1622 1623 if (level == 0) { 1624 PyObject *front = PyUnicode_Substring(name, 0, dot); 1625 if (front == NULL) { 1626 goto error; 1627 } 1628 1629 final_mod = PyImport_ImportModuleLevelObject(front, NULL, NULL, NULL, 0); 1630 Py_DECREF(front); 1631 } 1632 else { 1633 Py_ssize_t cut_off = len - dot; 1634 Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name); 1635 PyObject *to_return = PyUnicode_Substring(abs_name, 0, 1636 abs_name_len - cut_off); 1637 if (to_return == NULL) { 1638 goto error; 1639 } 1640 1641 final_mod = PyDict_GetItem(interp->modules, to_return); 1642 Py_DECREF(to_return); 1643 if (final_mod == NULL) { 1644 PyErr_Format(PyExc_KeyError, 1645 "%R not in sys.modules as expected", 1646 to_return); 1647 goto error; 1648 } 1649 Py_INCREF(final_mod); 1650 } 1651 } 1652 else { 1653 final_mod = mod; 1654 Py_INCREF(mod); 1655 } 1656 } 1657 else { 1658 final_mod = _PyObject_CallMethodIdObjArgs(interp->importlib, 1659 &PyId__handle_fromlist, mod, 1660 fromlist, interp->import_func, 1661 NULL); 1662 } 1663 1664 error: 1665 Py_XDECREF(abs_name); 1666 Py_XDECREF(mod); 1667 Py_XDECREF(package); 1668 if (final_mod == NULL) 1669 remove_importlib_frames(); 1670 return final_mod; 1671 } 1672 1673 PyObject * 1674 PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals, 1675 PyObject *fromlist, int level) 1676 { 1677 PyObject *nameobj, *mod; 1678 nameobj = PyUnicode_FromString(name); 1679 if (nameobj == NULL) 1680 return NULL; 1681 mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals, 1682 fromlist, level); 1683 Py_DECREF(nameobj); 1684 return mod; 1685 } 1686 1687 1688 /* Re-import a module of any kind and return its module object, WITH 1689 INCREMENTED REFERENCE COUNT */ 1690 1691 PyObject * 1692 PyImport_ReloadModule(PyObject *m) 1693 { 1694 _Py_IDENTIFIER(reload); 1695 PyObject *reloaded_module = NULL; 1696 PyObject *modules = PyImport_GetModuleDict(); 1697 PyObject *imp = PyDict_GetItemString(modules, "imp"); 1698 if (imp == NULL) { 1699 imp = PyImport_ImportModule("imp"); 1700 if (imp == NULL) { 1701 return NULL; 1702 } 1703 } 1704 else { 1705 Py_INCREF(imp); 1706 } 1707 1708 reloaded_module = _PyObject_CallMethodId(imp, &PyId_reload, "O", m); 1709 Py_DECREF(imp); 1710 return reloaded_module; 1711 } 1712 1713 1714 /* Higher-level import emulator which emulates the "import" statement 1715 more accurately -- it invokes the __import__() function from the 1716 builtins of the current globals. This means that the import is 1717 done using whatever import hooks are installed in the current 1718 environment. 1719 A dummy list ["__doc__"] is passed as the 4th argument so that 1720 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache")) 1721 will return <module "gencache"> instead of <module "win32com">. */ 1722 1723 PyObject * 1724 PyImport_Import(PyObject *module_name) 1725 { 1726 static PyObject *silly_list = NULL; 1727 static PyObject *builtins_str = NULL; 1728 static PyObject *import_str = NULL; 1729 PyObject *globals = NULL; 1730 PyObject *import = NULL; 1731 PyObject *builtins = NULL; 1732 PyObject *modules = NULL; 1733 PyObject *r = NULL; 1734 1735 /* Initialize constant string objects */ 1736 if (silly_list == NULL) { 1737 import_str = PyUnicode_InternFromString("__import__"); 1738 if (import_str == NULL) 1739 return NULL; 1740 builtins_str = PyUnicode_InternFromString("__builtins__"); 1741 if (builtins_str == NULL) 1742 return NULL; 1743 silly_list = PyList_New(0); 1744 if (silly_list == NULL) 1745 return NULL; 1746 } 1747 1748 /* Get the builtins from current globals */ 1749 globals = PyEval_GetGlobals(); 1750 if (globals != NULL) { 1751 Py_INCREF(globals); 1752 builtins = PyObject_GetItem(globals, builtins_str); 1753 if (builtins == NULL) 1754 goto err; 1755 } 1756 else { 1757 /* No globals -- use standard builtins, and fake globals */ 1758 builtins = PyImport_ImportModuleLevel("builtins", 1759 NULL, NULL, NULL, 0); 1760 if (builtins == NULL) 1761 return NULL; 1762 globals = Py_BuildValue("{OO}", builtins_str, builtins); 1763 if (globals == NULL) 1764 goto err; 1765 } 1766 1767 /* Get the __import__ function from the builtins */ 1768 if (PyDict_Check(builtins)) { 1769 import = PyObject_GetItem(builtins, import_str); 1770 if (import == NULL) 1771 PyErr_SetObject(PyExc_KeyError, import_str); 1772 } 1773 else 1774 import = PyObject_GetAttr(builtins, import_str); 1775 if (import == NULL) 1776 goto err; 1777 1778 /* Call the __import__ function with the proper argument list 1779 Always use absolute import here. 1780 Calling for side-effect of import. */ 1781 r = PyObject_CallFunction(import, "OOOOi", module_name, globals, 1782 globals, silly_list, 0, NULL); 1783 if (r == NULL) 1784 goto err; 1785 Py_DECREF(r); 1786 1787 modules = PyImport_GetModuleDict(); 1788 r = PyDict_GetItem(modules, module_name); 1789 if (r != NULL) 1790 Py_INCREF(r); 1791 1792 err: 1793 Py_XDECREF(globals); 1794 Py_XDECREF(builtins); 1795 Py_XDECREF(import); 1796 1797 return r; 1798 } 1799 1800 /*[clinic input] 1801 _imp.extension_suffixes 1802 1803 Returns the list of file suffixes used to identify extension modules. 1804 [clinic start generated code]*/ 1805 1806 static PyObject * 1807 _imp_extension_suffixes_impl(PyObject *module) 1808 /*[clinic end generated code: output=0bf346e25a8f0cd3 input=ecdeeecfcb6f839e]*/ 1809 { 1810 PyObject *list; 1811 const char *suffix; 1812 unsigned int index = 0; 1813 1814 list = PyList_New(0); 1815 if (list == NULL) 1816 return NULL; 1817 #ifdef HAVE_DYNAMIC_LOADING 1818 while ((suffix = _PyImport_DynLoadFiletab[index])) { 1819 PyObject *item = PyUnicode_FromString(suffix); 1820 if (item == NULL) { 1821 Py_DECREF(list); 1822 return NULL; 1823 } 1824 if (PyList_Append(list, item) < 0) { 1825 Py_DECREF(list); 1826 Py_DECREF(item); 1827 return NULL; 1828 } 1829 Py_DECREF(item); 1830 index += 1; 1831 } 1832 #endif 1833 return list; 1834 } 1835 1836 /*[clinic input] 1837 _imp.init_frozen 1838 1839 name: unicode 1840 / 1841 1842 Initializes a frozen module. 1843 [clinic start generated code]*/ 1844 1845 static PyObject * 1846 _imp_init_frozen_impl(PyObject *module, PyObject *name) 1847 /*[clinic end generated code: output=fc0511ed869fd69c input=13019adfc04f3fb3]*/ 1848 { 1849 int ret; 1850 PyObject *m; 1851 1852 ret = PyImport_ImportFrozenModuleObject(name); 1853 if (ret < 0) 1854 return NULL; 1855 if (ret == 0) { 1856 Py_INCREF(Py_None); 1857 return Py_None; 1858 } 1859 m = PyImport_AddModuleObject(name); 1860 Py_XINCREF(m); 1861 return m; 1862 } 1863 1864 /*[clinic input] 1865 _imp.get_frozen_object 1866 1867 name: unicode 1868 / 1869 1870 Create a code object for a frozen module. 1871 [clinic start generated code]*/ 1872 1873 static PyObject * 1874 _imp_get_frozen_object_impl(PyObject *module, PyObject *name) 1875 /*[clinic end generated code: output=2568cc5b7aa0da63 input=ed689bc05358fdbd]*/ 1876 { 1877 return get_frozen_object(name); 1878 } 1879 1880 /*[clinic input] 1881 _imp.is_frozen_package 1882 1883 name: unicode 1884 / 1885 1886 Returns True if the module name is of a frozen package. 1887 [clinic start generated code]*/ 1888 1889 static PyObject * 1890 _imp_is_frozen_package_impl(PyObject *module, PyObject *name) 1891 /*[clinic end generated code: output=e70cbdb45784a1c9 input=81b6cdecd080fbb8]*/ 1892 { 1893 return is_frozen_package(name); 1894 } 1895 1896 /*[clinic input] 1897 _imp.is_builtin 1898 1899 name: unicode 1900 / 1901 1902 Returns True if the module name corresponds to a built-in module. 1903 [clinic start generated code]*/ 1904 1905 static PyObject * 1906 _imp_is_builtin_impl(PyObject *module, PyObject *name) 1907 /*[clinic end generated code: output=3bfd1162e2d3be82 input=86befdac021dd1c7]*/ 1908 { 1909 return PyLong_FromLong(is_builtin(name)); 1910 } 1911 1912 /*[clinic input] 1913 _imp.is_frozen 1914 1915 name: unicode 1916 / 1917 1918 Returns True if the module name corresponds to a frozen module. 1919 [clinic start generated code]*/ 1920 1921 static PyObject * 1922 _imp_is_frozen_impl(PyObject *module, PyObject *name) 1923 /*[clinic end generated code: output=01f408f5ec0f2577 input=7301dbca1897d66b]*/ 1924 { 1925 const struct _frozen *p; 1926 1927 p = find_frozen(name); 1928 return PyBool_FromLong((long) (p == NULL ? 0 : p->size)); 1929 } 1930 1931 /* Common implementation for _imp.exec_dynamic and _imp.exec_builtin */ 1932 static int 1933 exec_builtin_or_dynamic(PyObject *mod) { 1934 PyModuleDef *def; 1935 void *state; 1936 1937 if (!PyModule_Check(mod)) { 1938 return 0; 1939 } 1940 1941 def = PyModule_GetDef(mod); 1942 if (def == NULL) { 1943 return 0; 1944 } 1945 1946 state = PyModule_GetState(mod); 1947 if (state) { 1948 /* Already initialized; skip reload */ 1949 return 0; 1950 } 1951 1952 return PyModule_ExecDef(mod, def); 1953 } 1954 1955 #ifdef HAVE_DYNAMIC_LOADING 1956 1957 /*[clinic input] 1958 _imp.create_dynamic 1959 1960 spec: object 1961 file: object = NULL 1962 / 1963 1964 Create an extension module. 1965 [clinic start generated code]*/ 1966 1967 static PyObject * 1968 _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file) 1969 /*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/ 1970 { 1971 PyObject *mod, *name, *path; 1972 FILE *fp; 1973 1974 name = PyObject_GetAttrString(spec, "name"); 1975 if (name == NULL) { 1976 return NULL; 1977 } 1978 1979 path = PyObject_GetAttrString(spec, "origin"); 1980 if (path == NULL) { 1981 Py_DECREF(name); 1982 return NULL; 1983 } 1984 1985 mod = _PyImport_FindExtensionObject(name, path); 1986 if (mod != NULL) { 1987 Py_DECREF(name); 1988 Py_DECREF(path); 1989 Py_INCREF(mod); 1990 return mod; 1991 } 1992 1993 if (file != NULL) { 1994 fp = _Py_fopen_obj(path, "r"); 1995 if (fp == NULL) { 1996 Py_DECREF(name); 1997 Py_DECREF(path); 1998 return NULL; 1999 } 2000 } 2001 else 2002 fp = NULL; 2003 2004 mod = _PyImport_LoadDynamicModuleWithSpec(spec, fp); 2005 2006 Py_DECREF(name); 2007 Py_DECREF(path); 2008 if (fp) 2009 fclose(fp); 2010 return mod; 2011 } 2012 2013 /*[clinic input] 2014 _imp.exec_dynamic -> int 2015 2016 mod: object 2017 / 2018 2019 Initialize an extension module. 2020 [clinic start generated code]*/ 2021 2022 static int 2023 _imp_exec_dynamic_impl(PyObject *module, PyObject *mod) 2024 /*[clinic end generated code: output=f5720ac7b465877d input=9fdbfcb250280d3a]*/ 2025 { 2026 return exec_builtin_or_dynamic(mod); 2027 } 2028 2029 2030 #endif /* HAVE_DYNAMIC_LOADING */ 2031 2032 /*[clinic input] 2033 _imp.exec_builtin -> int 2034 2035 mod: object 2036 / 2037 2038 Initialize a built-in module. 2039 [clinic start generated code]*/ 2040 2041 static int 2042 _imp_exec_builtin_impl(PyObject *module, PyObject *mod) 2043 /*[clinic end generated code: output=0262447b240c038e input=7beed5a2f12a60ca]*/ 2044 { 2045 return exec_builtin_or_dynamic(mod); 2046 } 2047 2048 /*[clinic input] 2049 dump buffer 2050 [clinic start generated code]*/ 2051 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/ 2052 2053 2054 PyDoc_STRVAR(doc_imp, 2055 "(Extremely) low-level import machinery bits as used by importlib and imp."); 2056 2057 static PyMethodDef imp_methods[] = { 2058 _IMP_EXTENSION_SUFFIXES_METHODDEF 2059 _IMP_LOCK_HELD_METHODDEF 2060 _IMP_ACQUIRE_LOCK_METHODDEF 2061 _IMP_RELEASE_LOCK_METHODDEF 2062 _IMP_GET_FROZEN_OBJECT_METHODDEF 2063 _IMP_IS_FROZEN_PACKAGE_METHODDEF 2064 _IMP_CREATE_BUILTIN_METHODDEF 2065 _IMP_INIT_FROZEN_METHODDEF 2066 _IMP_IS_BUILTIN_METHODDEF 2067 _IMP_IS_FROZEN_METHODDEF 2068 _IMP_CREATE_DYNAMIC_METHODDEF 2069 _IMP_EXEC_DYNAMIC_METHODDEF 2070 _IMP_EXEC_BUILTIN_METHODDEF 2071 _IMP__FIX_CO_FILENAME_METHODDEF 2072 {NULL, NULL} /* sentinel */ 2073 }; 2074 2075 2076 static struct PyModuleDef impmodule = { 2077 PyModuleDef_HEAD_INIT, 2078 "_imp", 2079 doc_imp, 2080 0, 2081 imp_methods, 2082 NULL, 2083 NULL, 2084 NULL, 2085 NULL 2086 }; 2087 2088 PyMODINIT_FUNC 2089 PyInit_imp(void) 2090 { 2091 PyObject *m, *d; 2092 2093 m = PyModule_Create(&impmodule); 2094 if (m == NULL) 2095 goto failure; 2096 d = PyModule_GetDict(m); 2097 if (d == NULL) 2098 goto failure; 2099 2100 return m; 2101 failure: 2102 Py_XDECREF(m); 2103 return NULL; 2104 } 2105 2106 2107 /* API for embedding applications that want to add their own entries 2108 to the table of built-in modules. This should normally be called 2109 *before* Py_Initialize(). When the table resize fails, -1 is 2110 returned and the existing table is unchanged. 2111 2112 After a similar function by Just van Rossum. */ 2113 2114 int 2115 PyImport_ExtendInittab(struct _inittab *newtab) 2116 { 2117 static struct _inittab *our_copy = NULL; 2118 struct _inittab *p; 2119 int i, n; 2120 2121 /* Count the number of entries in both tables */ 2122 for (n = 0; newtab[n].name != NULL; n++) 2123 ; 2124 if (n == 0) 2125 return 0; /* Nothing to do */ 2126 for (i = 0; PyImport_Inittab[i].name != NULL; i++) 2127 ; 2128 2129 /* Allocate new memory for the combined table */ 2130 p = our_copy; 2131 PyMem_RESIZE(p, struct _inittab, i+n+1); 2132 if (p == NULL) 2133 return -1; 2134 2135 /* Copy the tables into the new memory */ 2136 if (our_copy != PyImport_Inittab) 2137 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab)); 2138 PyImport_Inittab = our_copy = p; 2139 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab)); 2140 2141 return 0; 2142 } 2143 2144 /* Shorthand to add a single entry given a name and a function */ 2145 2146 int 2147 PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void)) 2148 { 2149 struct _inittab newtab[2]; 2150 2151 memset(newtab, '\0', sizeof newtab); 2152 2153 newtab[0].name = name; 2154 newtab[0].initfunc = initfunc; 2155 2156 return PyImport_ExtendInittab(newtab); 2157 } 2158 2159 #ifdef __cplusplus 2160 } 2161 #endif 2162