/* 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;
/*[clinic input]
Py_RETURN_NONE;
}
+PyStatus
+_PyImport_Init(void)
+{
+ if (_PyRuntime.imports.inittab != NULL) {
+ return _PyStatus_ERR("global import state already initialized");
+ }
+ PyStatus status = _PyStatus_OK();
+
+ size_t size;
+ for (size = 0; PyImport_Inittab[size].name != NULL; size++)
+ ;
+ size++;
+
+ /* Force default raw memory allocator to get a known allocator to be able
+ to release the memory in _PyImport_Fini() */
+ PyMemAllocatorEx old_alloc;
+ _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
+
+ /* Make the copy. */
+ struct _inittab *copied = PyMem_RawMalloc(size * sizeof(struct _inittab));
+ if (copied == NULL) {
+ status = PyStatus_NoMemory();
+ goto done;
+ }
+ memcpy(copied, PyImport_Inittab, size * sizeof(struct _inittab));
+ _PyRuntime.imports.inittab = copied;
+
+done:
+ PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
+ return status;
+}
+
static inline void _extensions_cache_clear(void);
void
PyThread_free_lock(import_lock);
import_lock = NULL;
}
+
+ /* Use the same memory allocator as _PyImport_Init(). */
+ PyMemAllocatorEx old_alloc;
+ _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
+
+ /* Free memory allocated by _PyImport_Init() */
+ struct _inittab *inittab = _PyRuntime.imports.inittab;
+ _PyRuntime.imports.inittab = NULL;
+ PyMem_RawFree(inittab);
+
+ PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
}
void
is_builtin(PyObject *name)
{
int i;
- for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
- if (_PyUnicode_EqualToASCIIString(name, PyImport_Inittab[i].name)) {
- if (PyImport_Inittab[i].initfunc == NULL)
+ struct _inittab *inittab = _PyRuntime.imports.inittab;
+ for (i = 0; inittab[i].name != NULL; i++) {
+ if (_PyUnicode_EqualToASCIIString(name, inittab[i].name)) {
+ if (inittab[i].initfunc == NULL)
return -1;
else
return 1;
}
PyObject *modules = tstate->interp->modules;
- for (struct _inittab *p = PyImport_Inittab; p->name != NULL; p++) {
+ for (struct _inittab *p = _PyRuntime.imports.inittab; p->name != NULL; p++) {
if (_PyUnicode_EqualToASCIIString(name, p->name)) {
if (p->initfunc == NULL) {
/* Cannot re-init internal module ("sys" or "builtins") */
size_t i, n;
int res = 0;
+ if (_PyRuntime.imports.inittab != NULL) {
+ Py_FatalError("PyImport_ExtendInittab() may be be called after Py_Initialize()");
+ }
+
/* Count the number of entries in both tables */
for (n = 0; newtab[n].name != NULL; n++)
;
{
struct _inittab newtab[2];
+ if (_PyRuntime.imports.inittab != NULL) {
+ Py_FatalError("PyImport_AppendInittab() may be be called after Py_Initialize()");
+ }
+
memset(newtab, '\0', sizeof newtab);
newtab[0].name = name;