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 #include "pycore_hashtable.h" // _Py_hashtable_t
9 #include "pycore_time.h" // _PyTime_t
10
11
12 struct _import_runtime_state {
13 /* The builtin modules (defined in config.c). */
14 struct _inittab *inittab;
15 /* The most recent value assigned to a PyModuleDef.m_base.m_index.
16 This is incremented each time PyModuleDef_Init() is called,
17 which is just about every time an extension module is imported.
18 See PyInterpreterState.modules_by_index for more info. */
19 Py_ssize_t last_module_index;
20 struct {
21 /* A lock to guard the cache. */
22 PyThread_type_lock mutex;
23 /* The actual cache of (filename, name, PyModuleDef) for modules.
24 Only legacy (single-phase init) extension modules are added
25 and only if they support multiple initialization (m_size >- 0)
26 or are imported in the main interpreter.
27 This is initialized lazily in _PyImport_FixupExtensionObject().
28 Modules are added there and looked up in _imp.find_extension(). */
29 _Py_hashtable_t *hashtable;
30 } extensions;
31 /* Package context -- the full module name for package imports */
32 const char * pkgcontext;
33 };
34
35 struct _import_state {
36 /* cached sys.modules dictionary */
37 PyObject *modules;
38 /* This is the list of module objects for all legacy (single-phase init)
39 extension modules ever loaded in this process (i.e. imported
40 in this interpreter or in any other). Py_None stands in for
41 modules that haven't actually been imported in this interpreter.
42
43 A module's index (PyModuleDef.m_base.m_index) is used to look up
44 the corresponding module object for this interpreter, if any.
45 (See PyState_FindModule().) When any extension module
46 is initialized during import, its moduledef gets initialized by
47 PyModuleDef_Init(), and the first time that happens for each
48 PyModuleDef, its index gets set to the current value of
49 a global counter (see _PyRuntimeState.imports.last_module_index).
50 The entry for that index in this interpreter remains unset until
51 the module is actually imported here. (Py_None is used as
52 a placeholder.) Note that multi-phase init modules always get
53 an index for which there will never be a module set.
54
55 This is initialized lazily in PyState_AddModule(), which is also
56 where modules get added. */
57 PyObject *modules_by_index;
58 /* importlib module._bootstrap */
59 PyObject *importlib;
60 /* override for config->use_frozen_modules (for tests)
61 (-1: "off", 1: "on", 0: no override) */
62 int override_frozen_modules;
63 int override_multi_interp_extensions_check;
64 #ifdef HAVE_DLOPEN
65 int dlopenflags;
66 #endif
67 PyObject *import_func;
68 /* The global import lock. */
69 struct {
70 PyThread_type_lock mutex;
71 unsigned long thread;
72 int level;
73 } lock;
74 /* diagnostic info in PyImport_ImportModuleLevelObject() */
75 struct {
76 int import_level;
77 _PyTime_t accumulated;
78 int header;
79 } find_and_load;
80 };
81
82 #ifdef HAVE_DLOPEN
83 # include <dlfcn.h>
84 # if HAVE_DECL_RTLD_NOW
85 # define _Py_DLOPEN_FLAGS RTLD_NOW
86 # else
87 # define _Py_DLOPEN_FLAGS RTLD_LAZY
88 # endif
89 # define DLOPENFLAGS_INIT .dlopenflags = _Py_DLOPEN_FLAGS,
90 #else
91 # define _Py_DLOPEN_FLAGS 0
92 # define DLOPENFLAGS_INIT
93 #endif
94
95 #define IMPORTS_INIT \
96 { \
97 DLOPENFLAGS_INIT \
98 .lock = { \
99 .mutex = NULL, \
100 .thread = PYTHREAD_INVALID_THREAD_ID, \
101 .level = 0, \
102 }, \
103 .find_and_load = { \
104 .header = 1, \
105 }, \
106 }
107
108 extern void _PyImport_ClearCore(PyInterpreterState *interp);
109
110 extern Py_ssize_t _PyImport_GetNextModuleIndex(void);
111 extern const char * _PyImport_ResolveNameWithPackageContext(const char *name);
112 extern const char * _PyImport_SwapPackageContext(const char *newcontext);
113
114 extern int _PyImport_GetDLOpenFlags(PyInterpreterState *interp);
115 extern void _PyImport_SetDLOpenFlags(PyInterpreterState *interp, int new_val);
116
117 extern PyObject * _PyImport_InitModules(PyInterpreterState *interp);
118 extern PyObject * _PyImport_GetModules(PyInterpreterState *interp);
119 extern void _PyImport_ClearModules(PyInterpreterState *interp);
120
121 extern void _PyImport_ClearModulesByIndex(PyInterpreterState *interp);
122
123 extern int _PyImport_InitDefaultImportFunc(PyInterpreterState *interp);
124 extern int _PyImport_IsDefaultImportFunc(
125 PyInterpreterState *interp,
126 PyObject *func);
127
128 extern PyObject * _PyImport_GetImportlibLoader(
129 PyInterpreterState *interp,
130 const char *loader_name);
131 extern PyObject * _PyImport_GetImportlibExternalLoader(
132 PyInterpreterState *interp,
133 const char *loader_name);
134 extern PyObject * _PyImport_BlessMyLoader(
135 PyInterpreterState *interp,
136 PyObject *module_globals);
137 extern PyObject * _PyImport_ImportlibModuleRepr(
138 PyInterpreterState *interp,
139 PyObject *module);
140
141
142 extern PyStatus _PyImport_Init(void);
143 extern void _PyImport_Fini(void);
144 extern void _PyImport_Fini2(void);
145
146 extern PyStatus _PyImport_InitCore(
147 PyThreadState *tstate,
148 PyObject *sysmod,
149 int importlib);
150 extern PyStatus _PyImport_InitExternal(PyThreadState *tstate);
151 extern void _PyImport_FiniCore(PyInterpreterState *interp);
152 extern void _PyImport_FiniExternal(PyInterpreterState *interp);
153
154
155 #ifdef HAVE_FORK
156 extern PyStatus _PyImport_ReInitLock(PyInterpreterState *interp);
157 #endif
158
159
160 extern PyObject* _PyImport_GetBuiltinModuleNames(void);
161
162 struct _module_alias {
163 const char *name; /* ASCII encoded string */
164 const char *orig; /* ASCII encoded string */
165 };
166
167 PyAPI_DATA(const struct _frozen *) _PyImport_FrozenBootstrap;
168 PyAPI_DATA(const struct _frozen *) _PyImport_FrozenStdlib;
169 PyAPI_DATA(const struct _frozen *) _PyImport_FrozenTest;
170 extern const struct _module_alias * _PyImport_FrozenAliases;
171
172 PyAPI_FUNC(int) _PyImport_CheckSubinterpIncompatibleExtensionAllowed(
173 const char *name);
174
175
176 // for testing
177 PyAPI_FUNC(int) _PyImport_ClearExtension(PyObject *name, PyObject *filename);
178
179 #ifdef __cplusplus
180 }
181 #endif
182 #endif /* !Py_INTERNAL_IMPORT_H */
183 #endif /* !Py_LIMITED_API */