8000 gh-101758: Clean Up Uses of Import State by ericsnowcurrently · Pull Request #101919 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-101758: Clean Up Uses of Import State #101919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Feb 15, 2023
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4433124
Group code in import.c.
ericsnowcurrently Feb 14, 2023
7626bab
Move PyState_*() to import.c.
ericsnowcurrently Feb 14, 2023
5836e93
Add _PyImport_GetNextModuleIndex().
ericsnowcurrently Feb 14, 2023
bc031c1
Add _PyImport_SwapPackageContext() and _PyImport_ResolveNameWithPacka…
ericsnowcurrently Feb 14, 2023
f67c299
Add _PyImport_GetBuiltinModuleNames().
ericsnowcurrently Feb 14, 2023
9b312e1
Add an "extension modules" section.
ericsnowcurrently Feb 14, 2023
24ec4de
Move the "extension modules" section down.
ericsnowcurrently Feb 14, 2023
b2043e4
Add _PyImport_GetDLOpenFlags() and _PyImport_SetDLOpenFlags().
ericsnowcurrently Feb 14, 2023
43d8de7
Hide sys.modules.
ericsnowcurrently Feb 14, 2023
b7cabeb
_PyInterpreterState_ClearModules() -> _PyImport_ClearModulesByIndex().
ericsnowcurrently Feb 14, 2023
d4693ea
Hide interp->import_func.
ericsnowcurrently Feb 14, 2023
7120be2
Hide interp->importlib.
ericsnowcurrently Feb 14, 2023
1935ee5
Add _PyImport_InitCore() and _PyImport_InitExternal().
ericsnowcurrently Feb 14, 2023
7e29b7c
Re-order import.c sections.
ericsnowcurrently Feb 14, 2023
66de41d
Add _PyImport_FiniExternal() and _PyImport_FiniCore().
ericsnowcurrently Feb 14, 2023
384bd69
Factor out init_builtin_modules_table() and fini_builtin_builtins_tab…
ericsnowcurrently Feb 14, 2023
e4777b7
Add PyInterpreterState.imports.
ericsnowcurrently Feb 14, 2023
48fb47c
Move the lifecycle sections to the bottom.
ericsnowcurrently Feb 14, 2023
e8930c8
Add macros for runtime-global import state.
ericsnowcurrently Feb 14, 2023
79a72c4
minor fixes
ericsnowcurrently Feb 14, 2023
0527913
Drop _PyState_AddModule().
ericsnowcurrently Feb 15, 2023
a4deb8a
Drop a wrong assert.
ericsnowcurrently Feb 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Factor out init_builtin_modules_table() and fini_builtin_builtins_tab…
…le().
  • Loading branch information
ericsnowcurrently committed Feb 14, 2023
commit 384bd693d808342bc79a6b0ab4fa022cab4688ac
47 changes: 34 additions & 13 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,34 @@ static struct _inittab *inittab_copy = NULL;
/* runtime lifecycle */
/*********************/

static int init_builtin_modules_table(void);

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++;
PyStatus status = _PyStatus_OK();

/* 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) {
if (init_builtin_modules_table() != 0) {
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);
static void fini_builtin_modules_table(void);

void
_PyImport_Fini(void)
Expand All @@ -112,9 +107,7 @@ _PyImport_Fini(void)
_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);
fini_builtin_modules_table();

PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
}
Expand Down Expand Up @@ -1239,6 +1232,34 @@ PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
}


/* the internal table */

static int
init_builtin_modules_table(void)
{
size_t size;
for (size = 0; PyImport_Inittab[size].name != NULL; size++)
;
size++;

/* Make the copy. */
struct _inittab *copied = PyMem_RawMalloc(size * sizeof(struct _inittab));
if (copied == NULL) {
return -1;
}
memcpy(copied, PyImport_Inittab, size * sizeof(struct _inittab));
_PyRuntime.imports.inittab = copied;
return 0;
}

static void
fini_builtin_modules_table(void)
{
struct _inittab *inittab = _PyRuntime.imports.inittab;
_PyRuntime.imports.inittab = NULL;
PyMem_RawFree(inittab);
}

PyObject *
_PyImport_GetBuiltinModuleNames(void)
{
Expand Down
0