1 #ifndef Py_LIMITED_API 2 #ifndef Py_INTERNAL_IMPORT_H 3 #define Py_INTERNAL_IMPORT_H 4 #ifdef __cplusplus 5 extern "C" { 6 #endif 7 8 #ifndef Py_BUILD_CORE 9 # error "this header requires Py_BUILD_CORE define" 10 #endif 11 12 #include "pycore_lock.h" // PyMutex 13 #include "pycore_hashtable.h" // _Py_hashtable_t 14 15 extern int _PyImport_IsInitialized(PyInterpreterState *); 16 17 // Export for 'pyexpat' shared extension 18 PyAPI_FUNC(int) _PyImport_SetModule(PyObject *name, PyObject *module); 19 20 extern int _PyImport_SetModuleString(const char *name, PyObject* module); 21 22 extern void _PyImport_AcquireLock(PyInterpreterState *interp); 23 extern void _PyImport_ReleaseLock(PyInterpreterState *interp); 24 extern void _PyImport_ReInitLock(PyInterpreterState *interp); 25 26 // This is used exclusively for the sys and builtins modules: 27 extern int _PyImport_FixupBuiltin( 28 PyThreadState *tstate, 29 PyObject *mod, 30 const char *name, /* UTF-8 encoded string */ 31 PyObject *modules 32 ); 33 34 // Export for many shared extensions, like '_json' 35 PyAPI_FUNC(PyObject*) _PyImport_GetModuleAttr(PyObject *, PyObject *); 36 37 // Export for many shared extensions, like '_datetime' 38 PyAPI_FUNC(PyObject*) _PyImport_GetModuleAttrString(const char *, const char *); 39 40 41 struct _import_runtime_state { 42 /* The builtin modules (defined in config.c). */ 43 struct _inittab *inittab; 44 /* The most recent value assigned to a PyModuleDef.m_base.m_index. 45 This is incremented each time PyModuleDef_Init() is called, 46 which is just about every time an extension module is imported. 47 See PyInterpreterState.modules_by_index for more info. */ 48 Py_ssize_t last_module_index; 49 struct { 50 /* A lock to guard the cache. */ 51 PyMutex mutex; 52 /* The actual cache of (filename, name, PyModuleDef) for modules. 53 Only legacy (single-phase init) extension modules are added 54 and only if they support multiple initialization (m_size >- 0) 55 or are imported in the main interpreter. 56 This is initialized lazily in fix_up_extension() in import.c. 57 Modules are added there and looked up in _imp.find_extension(). */ 58 _Py_hashtable_t *hashtable; 59 } extensions; 60 /* Package context -- the full module name for package imports */ 61 const char * pkgcontext; 62 }; 63 64 struct _import_state { 65 /* cached sys.modules dictionary */ 66 PyObject *modules; 67 /* This is the list of module objects for all legacy (single-phase init) 68 extension modules ever loaded in this process (i.e. imported 69 in this interpreter or in any other). Py_None stands in for 70 modules that haven't actually been imported in this interpreter. 71 72 A module's index (PyModuleDef.m_base.m_index) is used to look up 73 the corresponding module object for this interpreter, if any. 74 (See PyState_FindModule().) When any extension module 75 is initialized during import, its moduledef gets initialized by 76 PyModuleDef_Init(), and the first time that happens for each 77 PyModuleDef, its index gets set to the current value of 78 a global counter (see _PyRuntimeState.imports.last_module_index). 79 The entry for that index in this interpreter remains unset until 80 the module is actually imported here. (Py_None is used as 81 a placeholder.) Note that multi-phase init modules always get 82 an index for which there will never be a module set. 83 84 This is initialized lazily in PyState_AddModule(), which is also 85 where modules get added. */ 86 PyObject *modules_by_index; 87 /* importlib module._bootstrap */ 88 PyObject *importlib; 89 /* override for config->use_frozen_modules (for tests) 90 (-1: "off", 1: "on", 0: no override) */ 91 int override_frozen_modules; 92 int override_multi_interp_extensions_check; 93 #ifdef HAVE_DLOPEN 94 int dlopenflags; 95 #endif 96 PyObject *import_func; 97 /* The global import lock. */ 98 _PyRecursiveMutex lock; 99 /* diagnostic info in PyImport_ImportModuleLevelObject() */ 100 struct { 101 int import_level; 102 PyTime_t accumulated; 103 int header; 104 } find_and_load; 105 }; 106 107 #ifdef HAVE_DLOPEN 108 # include <dlfcn.h> // RTLD_NOW, RTLD_LAZY 109 # if HAVE_DECL_RTLD_NOW 110 # define _Py_DLOPEN_FLAGS RTLD_NOW 111 # else 112 # define _Py_DLOPEN_FLAGS RTLD_LAZY 113 # endif 114 # define DLOPENFLAGS_INIT .dlopenflags = _Py_DLOPEN_FLAGS, 115 #else 116 # define _Py_DLOPEN_FLAGS 0 117 # define DLOPENFLAGS_INIT 118 #endif 119 120 #define IMPORTS_INIT \ 121 { \ 122 DLOPENFLAGS_INIT \ 123 .find_and_load = { \ 124 .header = 1, \ 125 }, \ 126 } 127 128 extern void _PyImport_ClearCore(PyInterpreterState *interp); 129 130 extern Py_ssize_t _PyImport_GetNextModuleIndex(void); 131 extern const char * _PyImport_ResolveNameWithPackageContext(const char *name); 132 extern const char * _PyImport_SwapPackageContext(const char *newcontext); 133 134 extern int _PyImport_GetDLOpenFlags(PyInterpreterState *interp); 135 extern void _PyImport_SetDLOpenFlags(PyInterpreterState *interp, int new_val); 136 137 extern PyObject * _PyImport_InitModules(PyInterpreterState *interp); 138 extern PyObject * _PyImport_GetModules(PyInterpreterState *interp); 139 extern void _PyImport_ClearModules(PyInterpreterState *interp); 140 141 extern void _PyImport_ClearModulesByIndex(PyInterpreterState *interp); 142 143 extern int _PyImport_InitDefaultImportFunc(PyInterpreterState *interp); 144 extern int _PyImport_IsDefaultImportFunc( 145 PyInterpreterState *interp, 146 PyObject *func); 147 148 extern PyObject * _PyImport_GetImportlibLoader( 149 PyInterpreterState *interp, 150 const char *loader_name); 151 extern PyObject * _PyImport_GetImportlibExternalLoader( 152 PyInterpreterState *interp, 153 const char *loader_name); 154 extern PyObject * _PyImport_BlessMyLoader( 155 PyInterpreterState *interp, 156 PyObject *module_globals); 157 extern PyObject * _PyImport_ImportlibModuleRepr( 158 PyInterpreterState *interp, 159 PyObject *module); 160 161 162 extern PyStatus _PyImport_Init(void); 163 extern void _PyImport_Fini(void); 164 extern void _PyImport_Fini2(void); 165 166 extern PyStatus _PyImport_InitCore( 167 PyThreadState *tstate, 168 PyObject *sysmod, 169 int importlib); 170 extern PyStatus _PyImport_InitExternal(PyThreadState *tstate); 171 extern void _PyImport_FiniCore(PyInterpreterState *interp); 172 extern void _PyImport_FiniExternal(PyInterpreterState *interp); 173 174 175 extern PyObject* _PyImport_GetBuiltinModuleNames(void); 176 177 struct _module_alias { 178 const char *name; /* ASCII encoded string */ 179 const char *orig; /* ASCII encoded string */ 180 }; 181 182 // Export these 3 symbols for test_ctypes 183 PyAPI_DATA(const struct _frozen*) _PyImport_FrozenBootstrap; 184 PyAPI_DATA(const struct _frozen*) _PyImport_FrozenStdlib; 185 PyAPI_DATA(const struct _frozen*) _PyImport_FrozenTest; 186 187 extern const struct _module_alias * _PyImport_FrozenAliases; 188 189 extern int _PyImport_CheckSubinterpIncompatibleExtensionAllowed( 190 const char *name); 191 192 193 // Export for '_testinternalcapi' shared extension 194 PyAPI_FUNC(int) _PyImport_ClearExtension(PyObject *name, PyObject *filename); 195 196 #ifdef Py_GIL_DISABLED 197 // Assuming that the GIL is enabled from a call to 198 // _PyEval_EnableGILTransient(), resolve the transient request depending on the 199 // state of the module argument: 200 // - If module is NULL or a PyModuleObject with md_gil == Py_MOD_GIL_NOT_USED, 201 // call _PyEval_DisableGIL(). 202 // - Otherwise, call _PyEval_EnableGILPermanent(). If the GIL was not already 203 // enabled permanently, issue a warning referencing the module's name. 204 // 205 // This function may raise an exception. 206 extern int _PyImport_CheckGILForModule(PyObject *module, PyObject *module_name); 207 #endif 208 209 #ifdef __cplusplus 210 } 211 #endif 212 #endif /* !Py_INTERNAL_IMPORT_H */ 213 #endif /* !Py_LIMITED_API */ 214