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