Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Add _PyRuntimeState.imports.extensions.
  • Loading branch information
ericsnowcurrently committed Nov 10, 2022
commit 3b93ab91d59d968ac6b319b2223230dcf2bc7e01
12 changes: 12 additions & 0 deletions Include/internal/pycore_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
extern "C" {
#endif


struct _import_runtime_state {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also move the global import lock to runtime state?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's next. :)

/* A dict mapping (filename, name) to PyModuleDef for modules.
Only legacy (single-phase init) extension modules are added
and only if they support multiple initialization (m_size >- 0)
or are imported in the main interpreter.
This is initialized lazily in _PyImport_FixupExtensionObject().
Modules are added there and looked up in _imp.find_extension(). */
PyObject *extensions;
};


#ifdef HAVE_FORK
extern PyStatus _PyImport_ReInitLock(void);
#endif
Expand Down
2 changes: 2 additions & 0 deletions Include/internal/pycore_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern "C" {
#include "pycore_atomic.h" /* _Py_atomic_address */
#include "pycore_gil.h" // struct _gil_runtime_state
#include "pycore_global_objects.h" // struct _Py_global_objects
#include "pycore_import.h" // struct _import_runtime_state
#include "pycore_interp.h" // PyInterpreterState
#include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_ids

Expand Down Expand Up @@ -115,6 +116,7 @@ typedef struct pyruntimestate {
void (*exitfuncs[NEXITFUNCS])(void);
int nexitfuncs;

struct _import_runtime_state imports;
struct _ceval_runtime_state ceval;
struct _gilstate_runtime_state gilstate;
struct _getargs_runtime_state getargs;
Expand Down
38 changes: 30 additions & 8 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ extern "C" {
/* Forward references */
static PyObject *import_add_module(PyThreadState *tstate, PyObject *name);

/* See _PyImport_FixupExtensionObject() below */
static PyObject *extensions = NULL;

/* This table is defined in config.c: */
extern struct _inittab _PyImport_Inittab[];

Expand Down Expand Up @@ -221,10 +218,12 @@ _imp_release_lock_impl(PyObject *module)
Py_RETURN_NONE;
}

static inline void _clear_extensions_cache(void);

void
_PyImport_Fini(void)
{
Py_CLEAR(extensions);
_clear_extensions_cache();
if (import_lock != NULL) {
PyThread_free_lock(import_lock);
import_lock = NULL;
Expand Down Expand Up @@ -398,6 +397,30 @@ PyImport_GetMagicTag(void)
dictionary, to avoid loading shared libraries twice.
*/

static inline PyObject *
_get_extensions_cache(void)
{
return _PyRuntime.imports.extensions;
}

static inline PyObject *
_ensure_extensions_cache(void)
{
if (_PyRuntime.imports.extensions == NULL) {
_PyRuntime.imports.extensions = PyDict_New();
if (_PyRuntime.imports.extensions == NULL) {
return NULL;
}
}
return _PyRuntime.imports.extensions;
}

static inline void
_clear_extensions_cache(void)
{
Py_CLEAR(_PyRuntime.imports.extensions);
}

int
_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
PyObject *filename, PyObject *modules)
Expand Down Expand Up @@ -442,11 +465,9 @@ _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
}
}

PyObject *extensions = _ensure_extensions_cache();
if (extensions == NULL) {
extensions = PyDict_New();
if (extensions == NULL) {
return -1;
}
return -1;
}

PyObject *key = PyTuple_Pack(2, filename, name);
Expand Down Expand Up @@ -480,6 +501,7 @@ static PyObject *
import_find_extension(PyThreadState *tstate, PyObject *name,
PyObject *filename)
{
PyObject *extensions = _get_extensions_cache();
if (extensions == NULL) {
return NULL;
}
Expand Down
1 change: 0 additions & 1 deletion Tools/c-analyzer/cpython/globals-to-fix.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ Python/hamt.c - _empty_hamt -

# state
Objects/typeobject.c resolve_slotdups pname -
Python/import.c - extensions -


##################################
Expand Down