GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cpython/Python/import.c at main · python/cpython Expand file tree/ Copy pathimport.cMore file actions 5641 lines (4942 loc) · 169 KB / Copy pathimport.cFile metadata and controls5641 lines (4942 loc) · 169 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 /* Module definition and import implementation */ #include "Python.h" #include "pycore_audit.h" // _PySys_Audit() #include "pycore_ceval.h" #include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION() #include "pycore_dict.h" // _PyDict_Contains_KnownHash() #include "pycore_hashtable.h" // _Py_hashtable_new_full() #include "pycore_import.h" // _PyImport_BootstrapImp() #include "pycore_initconfig.h" // _PyStatus_OK() #include "pycore_interp.h" // struct _import_runtime_state #include "pycore_interpframe.h" #include "pycore_lazyimportobject.h" #include "pycore_magic_number.h" // PYC_MAGIC_NUMBER_TOKEN #include "pycore_moduleobject.h" // _PyModule_GetDef() #include "pycore_namespace.h" // _PyNamespace_Type #include "pycore_object.h" // _Py_SetImmortal() #include "pycore_pyatomic_ft_wrappers.h" #include "pycore_pyerrors.h" // _PyErr_SetString() #include "pycore_pyhash.h" // _Py_KeyedHash() #include "pycore_pylifecycle.h" #include "pycore_pymem.h" // _PyMem_DefaultRawFree() #include "pycore_pystate.h" // _PyInterpreterState_GET() #include "pycore_setobject.h" // _PySet_NextEntry() #include "pycore_sysmodule.h" // _PySys_ClearAttrString() #include "pycore_time.h" // _PyTime_AsMicroseconds() #include "pycore_unicodeobject.h" // _PyUnicode_AsUTF8NoNUL() #include "pycore_weakref.h" // _PyWeakref_GET_REF() #include "marshal.h" // PyMarshal_ReadObjectFromString() #include "pycore_importdl.h" // _PyImport_DynLoadFiletab #include "pydtrace.h" // PyDTrace_IMPORT_FIND_LOAD_START_ENABLED() #include <stdbool.h> // bool #ifdef HAVE_FCNTL_H #include <fcntl.h> #endif /*[clinic input] module _imp [clinic start generated code]*/ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=9c332475d8686284]*/ #include "clinic/import.c.h" #ifndef NDEBUG static bool is_interpreter_isolated(PyInterpreterState *interp) { return !_Py_IsMainInterpreter(interp) && !(interp->feature_flags & Py_RTFLAGS_USE_MAIN_OBMALLOC) && interp->ceval.own_gil; } #endif /*******************************/ /* process-global import state */ /*******************************/ /* This table is defined in config.c: */ extern struct _inittab _PyImport_Inittab[]; // This is not used after Py_Initialize() is called. // (See _PyRuntimeState.imports.inittab.) struct _inittab *PyImport_Inittab = _PyImport_Inittab; // When we dynamically allocate a larger table for PyImport_ExtendInittab(), // we track the pointer here so we can deallocate it during finalization. static struct _inittab *inittab_copy = NULL; /*******************************/ /* runtime-global import state */ /*******************************/ #define INITTAB _PyRuntime.imports.inittab #define LAST_MODULE_INDEX _PyRuntime.imports.last_module_index #define EXTENSIONS _PyRuntime.imports.extensions /*******************************/ /* interpreter import state */ /*******************************/ #define MODULES(interp) \ (interp)->imports.modules #define MODULES_BY_INDEX(interp) \ (interp)->imports.modules_by_index #define LAZY_MODULES(interp) \ (interp)->imports.lazy_modules #define LAZY_PENDING_SUBMODULES(interp) \ (interp)->imports.lazy_pending_submodules #define IMPORTLIB(interp) \ (interp)->imports.importlib #define OVERRIDE_MULTI_INTERP_EXTENSIONS_CHECK(interp) \ (interp)->imports.override_multi_interp_extensions_check #define OVERRIDE_FROZEN_MODULES(interp) \ (interp)->imports.override_frozen_modules #ifdef HAVE_DLOPEN # define DLOPENFLAGS(interp) \ (interp)->imports.dlopenflags #endif #define IMPORT_FUNC(interp) \ (interp)->imports.import_func #define LAZY_IMPORT_FUNC(interp) \ (interp)->imports.lazy_import_func #define IMPORT_LOCK(interp) \ (interp)->imports.lock #define FIND_AND_LOAD(interp) \ (interp)->imports.find_and_load #define LAZY_IMPORTS_MODE(interp) \ (interp)->imports.lazy_imports_mode #define LAZY_IMPORTS_FILTER(interp) \ (interp)->imports.lazy_imports_filter #ifdef Py_GIL_DISABLED #define LAZY_IMPORTS_LOCK(interp) PyMutex_Lock(&(interp)->imports.lazy_mutex) #define LAZY_IMPORTS_UNLOCK(interp) PyMutex_Unlock(&(interp)->imports.lazy_mutex) #else #define LAZY_IMPORTS_LOCK(interp) #define LAZY_IMPORTS_UNLOCK(interp) #endif #define _IMPORT_TIME_HEADER(interp) \ do { \ if (FIND_AND_LOAD((interp)).header) { \ fputs("import time: self [us] | cumulative | imported package\n", \ stderr); \ FIND_AND_LOAD((interp)).header = 0; \ } \ } while (0) /*******************/ /* the import lock */ /*******************/ /* Locking primitives to prevent parallel imports of the same module in different threads to return with a partially loaded module. These calls are serialized by the global interpreter lock. */ void _PyImport_AcquireLock(PyInterpreterState *interp) { _PyRecursiveMutex_Lock(&IMPORT_LOCK(interp)); } void _PyImport_ReleaseLock(PyInterpreterState *interp) { _PyRecursiveMutex_Unlock(&IMPORT_LOCK(interp)); } void _PyImport_ReInitLock(PyInterpreterState *interp) { // gh-126688: Thread id may change after fork() on some operating systems. IMPORT_LOCK(interp).thread = PyThread_get_thread_ident_ex(); } /***************/ /* sys.modules */ /***************/ PyObject * _PyImport_InitModules(PyInterpreterState *interp) { assert(MODULES(interp) == NULL); MODULES(interp) = PyDict_New(); if (MODULES(interp) == NULL) { return NULL; } return MODULES(interp); } PyObject * _PyImport_GetModules(PyInterpreterState *interp) { return MODULES(interp); } PyObject * _PyImport_GetModulesRef(PyInterpreterState *interp) { _PyImport_AcquireLock(interp); PyObject *modules = MODULES(interp); if (modules == NULL) { /* The interpreter hasn't been initialized yet. */ modules = Py_None; } Py_INCREF(modules); _PyImport_ReleaseLock(interp); return modules; } void _PyImport_ClearModules(PyInterpreterState *interp) { Py_SETREF(MODULES(interp), NULL); } static inline PyObject * get_modules_dict(PyThreadState *tstate, bool fatal) { /* Technically, it would make sense to incref the dict, * since sys.modules could be swapped out and decref'ed to 0 * before the caller is done using it. However, that is highly * unlikely, especially since we can rely on a global lock * (i.e. the GIL) for thread-safety. */ PyObject *modules = MODULES(tstate->interp); if (modules == NULL) { if (fatal) { Py_FatalError("interpreter has no modules dictionary"); } _PyErr_SetString(tstate, PyExc_RuntimeError, "unable to get sys.modules"); return NULL; } return modules; } PyObject * PyImport_GetModuleDict(void) { PyThreadState *tstate = _PyThreadState_GET(); return get_modules_dict(tstate, true); } int _PyImport_SetModule(PyObject *name, PyObject *m) { PyThreadState *tstate = _PyThreadState_GET(); PyObject *modules = get_modules_dict(tstate, true); return PyObject_SetItem(modules, name, m); } int _PyImport_SetModuleString(const char *name, PyObject *m) { PyThreadState *tstate = _PyThreadState_GET(); PyObject *modules = get_modules_dict(tstate, true); return PyMapping_SetItemString(modules, name, m); } static PyObject * import_get_module(PyThreadState *tstate, PyObject *name) { PyObject *modules = get_modules_dict(tstate, false); if (modules == NULL) { return NULL; } PyObject *m; Py_INCREF(modules); (void)PyMapping_GetOptionalItem(modules, name, &m); Py_DECREF(modules); return m; } PyObject * _PyImport_InitLazyModules(PyInterpreterState *interp) { assert(LAZY_MODULES(interp) == NULL && LAZY_PENDING_SUBMODULES(interp) == NULL); LAZY_PENDING_SUBMODULES(interp) = PyDict_New(); LAZY_MODULES(interp) = PySet_New(0); return LAZY_MODULES(interp); } void _PyImport_ClearLazyModules(PyInterpreterState *interp) { Py_CLEAR(LAZY_MODULES(interp)); Py_CLEAR(LAZY_PENDING_SUBMODULES(interp)); } int _PyImport_DiscardLazyModule(PyInterpreterState *interp, PyObject *name) { if (LAZY_MODULES(interp) == NULL) { return 0; } return PySet_Discard(LAZY_MODULES(interp), name); } static PyObject * get_importtime_name(PyObject *name) { PyObject *exc = PyErr_GetRaisedException(); PyObject *encoded = PyUnicode_AsEncodedString(name, "utf-8", "backslashreplace"); if (encoded == NULL) { PyErr_Clear(); } PyErr_SetRaisedException(exc); return encoded; } // Return whether the cached module was still initializing. static int import_ensure_initialized(PyInterpreterState *interp, PyObject *mod, PyObject *name) { PyObject *spec; /* Optimization: only call _bootstrap._lock_unlock_module() if __spec__._initializing is true. NOTE: because of this, initializing must be set *before* stuffing the new module in sys.modules. */ int rc = PyObject_GetOptionalAttr(mod, &_Py_ID(__spec__), &spec); if (rc > 0) { rc = _PyModuleSpec_IsInitializing(spec); Py_DECREF(spec); } if (rc == 0) { goto done; } else if (rc < 0) { return rc; } /* Wait until module is done importing. */ PyObject *value = PyObject_CallMethodOneArg( IMPORTLIB(interp), &_Py_ID(_lock_unlock_module), name); if (value == NULL) { return -1; } Py_DECREF(value); done: /* When -X importtime=2, print an import time entry even if an imported module has already been loaded. */ if (_PyInterpreterState_GetConfig(interp)->import_time == 2) { _IMPORT_TIME_HEADER(interp); #define import_level FIND_AND_LOAD(interp).import_level PyObject *encoded_name = get_importtime_name(name); fprintf(stderr, "import time: cached | cached | %*s\n", import_level*2, encoded_name != NULL ? PyBytes_AS_STRING(encoded_name) : "?"); Py_XDECREF(encoded_name); #undef import_level } return rc; } static void remove_importlib_frames(PyThreadState *tstate); PyObject * PyImport_GetModule(PyObject *name) { PyThreadState *tstate = _PyThreadState_GET(); PyObject *mod; mod = import_get_module(tstate, name); if (mod != NULL && mod != Py_None) { if (import_ensure_initialized(tstate->interp, mod, name) < 0) { goto error; } /* Verify the module is still in sys.modules. Another thread may have removed it (due to import failure) between our import_get_module() call and the _initializing check in import_ensure_initialized(). */ PyObject *mod_check = import_get_module(tstate, name); if (mod_check != mod) { Py_XDECREF(mod_check); if (_PyErr_Occurred(tstate)) { goto error; } /* The module was removed or replaced. Return NULL to report "not found" rather than trying to keep up with racing modifications to sys.modules; returning the new value would require looping to redo the ensure_initialized check. */ Py_DECREF(mod); return NULL; } Py_DECREF(mod_check); } return mod; error: Py_DECREF(mod); remove_importlib_frames(tstate); return NULL; } /* Get the module object corresponding to a module name. First check the modules dictionary if there's one there, if not, create a new one and insert it in the modules dictionary. */ static PyObject * import_add_module_lock_held(PyObject *modules, PyObject *name) { PyObject *m; if (PyMapping_GetOptionalItem(modules, name, &m) < 0) { return NULL; } if (m != NULL && PyModule_Check(m)) { return m; } Py_XDECREF(m); m = PyModule_NewObject(name); if (m == NULL) return NULL; if (PyObject_SetItem(modules, name, m) != 0) { Py_DECREF(m); return NULL; } return m; } static PyObject * import_add_module(PyThreadState *tstate, PyObject *name) { PyObject *modules = get_modules_dict(tstate, false); if (modules == NULL) { return NULL; } PyObject *m; Py_BEGIN_CRITICAL_SECTION(modules); m = import_add_module_lock_held(modules, name); Py_END_CRITICAL_SECTION(); return m; } PyObject * PyImport_AddModuleRef(const char *name) { PyObject *name_obj = PyUnicode_FromString(name); if (name_obj == NULL) { return NULL; } PyThreadState *tstate = _PyThreadState_GET(); PyObject *module = import_add_module(tstate, name_obj); Py_DECREF(name_obj); return module; } PyObject * PyImport_AddModuleObject(PyObject *name) { PyThreadState *tstate = _PyThreadState_GET(); PyObject *mod = import_add_module(tstate, name); if (!mod) { return NULL; } // gh-86160: PyImport_AddModuleObject() returns a borrowed reference. // Create a weak reference to produce a borrowed reference, since it can // become NULL. sys.modules type can be different than dict and it is not // guaranteed that it keeps a strong reference to the module. It can be a // custom mapping with __getitem__() which returns a new object or removes // returned object, or __setitem__ which does nothing. There is so much // unknown. With weakref we can be sure that we get either a reference to // live object or NULL. // // Use PyImport_AddModuleRef() to avoid these issues. PyObject *ref = PyWeakref_NewRef(mod, NULL); Py_DECREF(mod); if (ref == NULL) { return NULL; } mod = _PyWeakref_GET_REF(ref); Py_DECREF(ref); Py_XDECREF(mod); if (mod == NULL && !PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, "sys.modules does not hold a strong reference " "to the module"); } return mod; /* borrowed reference */ } PyObject * PyImport_AddModule(const char *name) { PyObject *nameobj = PyUnicode_FromString(name); if (nameobj == NULL) { return NULL; } PyObject *module = PyImport_AddModuleObject(nameobj); Py_DECREF(nameobj); return module; } /* Remove name from sys.modules, if it's there. * Can be called with an exception raised. * If fail to remove name a new exception will be chained with the old * exception, otherwise the old exception is preserved. */ static void remove_module(PyThreadState *tstate, PyObject *name) { PyObject *exc = _PyErr_GetRaisedException(tstate); PyObject *modules = get_modules_dict(tstate, true); if (PyDict_CheckExact(modules)) { // Error is reported to the caller (void)PyDict_Pop(modules, name, NULL); } else if (PyMapping_DelItem(modules, name) < 0) { if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { _PyErr_Clear(tstate); } } _PyErr_ChainExceptions1(exc); } /************************************/ /* per-interpreter modules-by-index */ /************************************/ Py_ssize_t _PyImport_GetNextModuleIndex(void) { return _Py_atomic_add_ssize(&LAST_MODULE_INDEX, 1) + 1; } #ifndef NDEBUG struct extensions_cache_value; static struct extensions_cache_value * _find_cached_def(PyModuleDef *); static Py_ssize_t _get_cached_module_index(struct extensions_cache_value *); #endif static Py_ssize_t _get_module_index_from_def(PyModuleDef *def) { Py_ssize_t index = def->m_base.m_index; #ifndef NDEBUG struct extensions_cache_value *cached = _find_cached_def(def); assert(cached == NULL || index == _get_cached_module_index(cached)); #endif return index; } static void _set_module_index(PyModuleDef *def, Py_ssize_t index) { assert(index > 0); if (index == def->m_base.m_index) { /* There's nothing to do. */ } else if (def->m_base.m_index == 0) { /* It should have been initialized by PyModuleDef_Init(). * We assert here to catch this in dev, but keep going otherwise. */ assert(def->m_base.m_index != 0); def->m_base.m_index = index; } else { /* It was already set for a different module. * We replace the old value. */ assert(def->m_base.m_index > 0); def->m_base.m_index = index; } } static const char * _modules_by_index_check(PyInterpreterState *interp, Py_ssize_t index) { if (index <= 0) { return "invalid module index"; } if (MODULES_BY_INDEX(interp) == NULL) { return "Interpreters module-list not accessible."; } if (index >= PyList_GET_SIZE(MODULES_BY_INDEX(interp))) { return "Module index out of bounds."; } return NULL; } static PyObject * _modules_by_index_get(PyInterpreterState *interp, Py_ssize_t index) { if (_modules_by_index_check(interp, index) != NULL) { return NULL; } PyObject *res = PyList_GET_ITEM(MODULES_BY_INDEX(interp), index); return res==Py_None ? NULL : res; } static int _modules_by_index_set(PyInterpreterState *interp, Py_ssize_t index, PyObject *module) { assert(index > 0); if (MODULES_BY_INDEX(interp) == NULL) { MODULES_BY_INDEX(interp) = PyList_New(0); if (MODULES_BY_INDEX(interp) == NULL) { return -1; } } while (PyList_GET_SIZE(MODULES_BY_INDEX(interp)) <= index) { if (PyList_Append(MODULES_BY_INDEX(interp), Py_None) < 0) { return -1; } } return PyList_SetItem(MODULES_BY_INDEX(interp), index, Py_NewRef(module)); } static int _modules_by_index_clear_one(PyInterpreterState *interp, Py_ssize_t index) { const char *err = _modules_by_index_check(interp, index); if (err != NULL) { Py_FatalError(err); return -1; } return PyList_SetItem(MODULES_BY_INDEX(interp), index, Py_NewRef(Py_None)); } PyObject* PyState_FindModule(PyModuleDef* module) { PyInterpreterState *interp = _PyInterpreterState_GET(); if (module->m_slots) { return NULL; } Py_ssize_t index = _get_module_index_from_def(module); return _modules_by_index_get(interp, index); } /* _PyState_AddModule() has been completely removed from the C-API (and was removed from the limited API in 3.6). However, we're playing it safe and keeping it around for any stable ABI extensions built against 3.2-3.5. */ int _PyState_AddModule(PyThreadState *tstate, PyObject* module, PyModuleDef* def) { if (!def) { assert(_PyErr_Occurred(tstate)); return -1; } if (def->m_slots) { _PyErr_SetString(tstate, PyExc_SystemError, "PyState_AddModule called on module with slots"); return -1; } assert(def->m_slots == NULL); Py_ssize_t index = _get_module_index_from_def(def); return _modules_by_index_set(tstate->interp, index, module); } int PyState_AddModule(PyObject* module, PyModuleDef* def) { if (!def) { Py_FatalError("module definition is NULL"); return -1; } PyThreadState *tstate = _PyThreadState_GET(); if (def->m_slots) { _PyErr_SetString(tstate, PyExc_SystemError, "PyState_AddModule called on module with slots"); return -1; } PyInterpreterState *interp = tstate->interp; Py_ssize_t index = _get_module_index_from_def(def); if (MODULES_BY_INDEX(interp) && index < PyList_GET_SIZE(MODULES_BY_INDEX(interp)) && module == PyList_GET_ITEM(MODULES_BY_INDEX(interp), index)) { _Py_FatalErrorFormat(__func__, "module %p already added", module); return -1; } assert(def->m_slots == NULL); return _modules_by_index_set(interp, index, module); } int PyState_RemoveModule(PyModuleDef* def) { PyThreadState *tstate = _PyThreadState_GET(); if (def->m_slots) { _PyErr_SetString(tstate, PyExc_SystemError, "PyState_RemoveModule called on module with slots"); return -1; } Py_ssize_t index = _get_module_index_from_def(def); return _modules_by_index_clear_one(tstate->interp, index); } // Used by finalize_modules() void _PyImport_ClearModulesByIndex(PyInterpreterState *interp) { if (!MODULES_BY_INDEX(interp)) { return; } Py_ssize_t i; for (i = 0; i < PyList_GET_SIZE(MODULES_BY_INDEX(interp)); i++) { PyObject *m = PyList_GET_ITEM(MODULES_BY_INDEX(interp), i); if (PyModule_Check(m)) { /* cleanup the saved copy of module dicts */ PyModuleDef *md = PyModule_GetDef(m); if (md) { // XXX Do this more carefully. The dict might be owned // by another interpreter. Py_CLEAR(md->m_base.m_copy); } } } /* Setting modules_by_index to NULL could be dangerous, so we clear the list instead. */ if (PyList_SetSlice(MODULES_BY_INDEX(interp), 0, PyList_GET_SIZE(MODULES_BY_INDEX(interp)), NULL)) { PyErr_FormatUnraisable("Exception ignored while " "clearing interpreters module list"); } } /*********************/ /* extension modules */ /*********************/ /* It may help to have a big picture view of what happens when an extension is loaded. This includes when it is imported for the first time. Here's a summary, using importlib._bootstrap._load() as a starting point. 1. importlib._bootstrap._load() 2. _load(): acquire import lock 3. _load() -> importlib._bootstrap._load_unlocked() 4. _load_unlocked() -> importlib._bootstrap.module_from_spec() 5. module_from_spec() -> ExtensionFileLoader.create_module() 6. create_module() -> _imp.create_dynamic() (see below) 7. module_from_spec() -> importlib._bootstrap._init_module_attrs() 8. _load_unlocked(): sys.modules[name] = module 9. _load_unlocked() -> ExtensionFileLoader.exec_module() 10. exec_module() -> _imp.exec_dynamic() (see below) 11. _load(): release import lock ...for single-phase init modules, where m_size == -1: (6). first time (not found in _PyRuntime.imports.extensions): A. _imp_create_dynamic_impl() -> import_find_extension() B. _imp_create_dynamic_impl() -> _PyImport_GetModuleExportHooks() C. _PyImport_GetModuleExportHooks(): load <module init func> D. _imp_create_dynamic_impl() -> import_run_extension() E. import_run_extension() -> _PyImport_RunModInitFunc() F. _PyImport_RunModInitFunc(): call <module init func> G. <module init func> -> PyModule_Create() -> PyModule_Create2() -> PyModule_CreateInitialized() H. PyModule_CreateInitialized() -> PyModule_New() I. PyModule_CreateInitialized(): allocate mod->md_state J. PyModule_CreateInitialized() -> PyModule_AddFunctions() K. PyModule_CreateInitialized() -> PyModule_SetDocString() L. PyModule_CreateInitialized(): set mod->md_def M. <module init func>: initialize the module, etc. N. import_run_extension() -> _PyImport_CheckSubinterpIncompatibleExtensionAllowed() O. import_run_extension(): set __file__ P. import_run_extension() -> update_global_state_for_extension() Q. update_global_state_for_extension(): copy __dict__ into def->m_base.m_copy R. update_global_state_for_extension(): add it to _PyRuntime.imports.extensions S. import_run_extension() -> finish_singlephase_extension() T. finish_singlephase_extension(): add it to interp->imports.modules_by_index U. finish_singlephase_extension(): add it to sys.modules Step (Q) is skipped for core modules (sys/builtins). (6). subsequent times (found in _PyRuntime.imports.extensions): A. _imp_create_dynamic_impl() -> import_find_extension() B. import_find_extension() -> reload_singlephase_extension() C. reload_singlephase_extension() -> _PyImport_CheckSubinterpIncompatibleExtensionAllowed() D. reload_singlephase_extension() -> import_add_module() E. if name in sys.modules: use that module F. else: 1. import_add_module() -> PyModule_NewObject() 2. import_add_module(): set it on sys.modules G. reload_singlephase_extension(): copy the "m_copy" dict into __dict__ H. reload_singlephase_extension(): add to modules_by_index (10). (every time): A. noop ...for single-phase init modules, where m_size >= 0: (6). not main interpreter and never loaded there - every time (not found in _PyRuntime.imports.extensions): A-P. (same as for m_size == -1) Q. _PyImport_RunModInitFunc(): set def->m_base.m_init R. (skipped) S-U. (same as for m_size == -1) (6). main interpreter - first time (not found in _PyRuntime.imports.extensions): A-P. (same as for m_size == -1) Q. _PyImport_RunModInitFunc(): set def->m_base.m_init R-U. (same as for m_size == -1) (6). subsequent times (found in _PyRuntime.imports.extensions): A. _imp_create_dynamic_impl() -> import_find_extension() B. import_find_extension() -> reload_singlephase_extension() C. reload_singlephase_extension() -> _PyImport_CheckSubinterpIncompatibleExtensionAllowed() D. reload_singlephase_extension(): call def->m_base.m_init (see above) E. reload_singlephase_extension(): add the module to sys.modules F. reload_singlephase_extension(): add to modules_by_index (10). every time: A. noop ...for multi-phase init modules from PyModInit_* (PyModuleDef): (6). every time: A. _imp_create_dynamic_impl() -> import_find_extension() (not found) B. _imp_create_dynamic_impl() -> _PyImport_GetModuleExportHooks() C. _PyImport_GetModuleExportHooks(): load <module init func> D. _imp_create_dynamic_impl() -> import_run_extension() E. import_run_extension() -> _PyImport_RunModInitFunc() F. _PyImport_RunModInitFunc(): call <module init func> G. import_run_extension() -> PyModule_FromDefAndSpec() PyModule_FromDefAndSpec(): H. PyModule_FromDefAndSpec(): gather/check moduledef slots I. if there's a Py_mod_create slot: 1. PyModule_FromDefAndSpec(): call its function J. else: 1. PyModule_FromDefAndSpec() -> PyModule_NewObject() K: PyModule_FromDefAndSpec(): set mod->md_def L. PyModule_FromDefAndSpec() -> _add_methods_to_object() M. PyModule_FromDefAndSpec() -> PyModule_SetDocString() (10). every time: A. _imp_exec_dynamic_impl() -> exec_builtin_or_dynamic() B. if mod->md_state == NULL (including if m_size == 0): 1. exec_builtin_or_dynamic() -> PyModule_Exec() 2. PyModule_Exec(): allocate mod->md_state 3. if there's a Py_mod_exec slot: 1. PyModule_Exec(): call its function ...for multi-phase init modules from PyModExport_* (slots array): (6). every time: A. _imp_create_dynamic_impl() -> import_find_extension() (not found) B. _imp_create_dynamic_impl() -> _PyImport_GetModuleExportHooks() C. _PyImport_GetModuleExportHooks(): load <module export func> D. _imp_create_dynamic_impl() -> import_run_modexport() E. import_run_modexport(): call <module init func> F. import_run_modexport() -> PyModule_FromSlotsAndSpec() G. PyModule_FromSlotsAndSpec(): create temporary PyModuleDef-like H. PyModule_FromSlotsAndSpec() -> PyModule_FromDefAndSpec() (PyModule_FromDefAndSpec behaves as for PyModInit_*, above) (10). every time: as for PyModInit_*, above */ /* Make sure name is fully qualified. This is a bit of a hack: when the shared library is loaded, the module name is "package.module", but the module calls PyModule_Create*() with just "module" for the name. The shared library loader squirrels away the true name of the module in _PyRuntime.imports.pkgcontext, and PyModule_Create*() will substitute this (if the name actually matches). */ static _Py_thread_local const char *pkgcontext = NULL; # define PKGCONTEXT pkgcontext const char * _PyImport_ResolveNameWithPackageContext(const char *name) { if (PKGCONTEXT != NULL) { const char *p = strrchr(PKGCONTEXT, '.'); if (p != NULL && strcmp(name, p+1) == 0) { name = PKGCONTEXT; PKGCONTEXT = NULL; } } return name; } const char * _PyImport_SwapPackageContext(const char *newcontext) { const char *oldcontext = PKGCONTEXT; PKGCONTEXT = newcontext; return oldcontext; } #ifdef HAVE_DLOPEN int _PyImport_GetDLOpenFlags(PyInterpreterState *interp) { return FT_ATOMIC_LOAD_INT_RELAXED(DLOPENFLAGS(interp)); } void _PyImport_SetDLOpenFlags(PyInterpreterState *interp, int new_val) { FT_ATOMIC_STORE_INT_RELAXED(DLOPENFLAGS(interp), new_val); } #endif // HAVE_DLOPEN /* Common implementation for _imp.exec_dynamic and _imp.exec_builtin */ static int exec_builtin_or_dynamic(PyObject *mod) { void *state; if (!PyModule_Check(mod)) { return 0; } state = PyModule_GetState(mod); if (state) { /* Already initialized; skip reload */ return 0; } return PyModule_Exec(mod); } static int clear_singlephase_extension(PyInterpreterState *interp, PyObject *name, PyObject *filename); // Currently, this is only used for testing. // (See _testinternalcapi.clear_extension().) // If adding another use, be careful about modules that import themselves // recursively (see gh-123880). int _PyImport_ClearExtension(PyObject *name, PyObject *filename) { PyInterpreterState *interp = _PyInterpreterState_GET(); /* Clearing a module's C globals is up to the module. */ if (clear_singlephase_extension(interp, name, filename) < 0) { return -1; } // In the future we'll probably also make sure the extension's // file handle (and DL handle) is closed (requires saving it). return 0; } /*****************************/ /* single-phase init modules */ /*****************************/ /* We support a number of kinds of single-phase init builtin/extension modules: * "basic" * no module state (PyModuleDef.m_size == -1) * does not support repeated init (we use PyModuleDef.m_base.m_copy) * may have process-global state * the module's def is cached in _PyRuntime.imports.extensions, |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |