Skip to content

Commit e874c2f

Browse files
gh-81057: Move the Remaining Import State Globals to _PyRuntimeState (gh-99488)
#81057
1 parent 4e4b13e commit e874c2f

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

Include/internal/pycore_import.h

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ struct _import_runtime_state {
2121
This is initialized lazily in _PyImport_FixupExtensionObject().
2222
Modules are added there and looked up in _imp.find_extension(). */
2323
PyObject *extensions;
24+
/* The global import lock. */
25+
struct {
26+
PyThread_type_lock mutex;
27+
unsigned long thread;
28+
int level;
29+
} lock;
30+
struct {
31+
int import_level;
32+
_PyTime_t accumulated;
33+
int header;
34+
} find_and_load;
2435
};
2536

2637

Include/internal/pycore_runtime_init.h

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ extern "C" {
3939
.types = { \
4040
.next_version_tag = 1, \
4141
}, \
42+
.imports = { \
43+
.lock = { \
44+
.mutex = NULL, \
45+
.thread = PYTHREAD_INVALID_THREAD_ID, \
46+
.level = 0, \
47+
}, \
48+
.find_and_load = { \
49+
.header = 1, \
50+
}, \
51+
}, \
4252
.global_objects = { \
4353
.singletons = { \
4454
.small_ints = _Py_small_ints_INIT, \

Python/import.c

+9-6
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ _PyImportZip_Init(PyThreadState *tstate)
9494
in different threads to return with a partially loaded module.
9595
These calls are serialized by the global interpreter lock. */
9696

97-
static PyThread_type_lock import_lock = NULL;
98-
static unsigned long import_lock_thread = PYTHREAD_INVALID_THREAD_ID;
99-
static int import_lock_level = 0;
97+
#define import_lock _PyRuntime.imports.lock.mutex
98+
#define import_lock_thread _PyRuntime.imports.lock.thread
99+
#define import_lock_level _PyRuntime.imports.lock.level
100100

101101
void
102102
_PyImport_AcquireLock(void)
@@ -1759,8 +1759,8 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name)
17591759
PyObject *mod = NULL;
17601760
PyInterpreterState *interp = tstate->interp;
17611761
int import_time = _PyInterpreterState_GetConfig(interp)->import_time;
1762-
static int import_level;
1763-
static _PyTime_t accumulated;
1762+
#define import_level _PyRuntime.imports.find_and_load.import_level
1763+
#define accumulated _PyRuntime.imports.find_and_load.accumulated
17641764

17651765
_PyTime_t t1 = 0, accumulated_copy = accumulated;
17661766

@@ -1781,12 +1781,13 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name)
17811781
* _PyDict_GetItemIdWithError().
17821782
*/
17831783
if (import_time) {
1784-
static int header = 1;
1784+
#define header _PyRuntime.imports.find_and_load.header
17851785
if (header) {
17861786
fputs("import time: self [us] | cumulative | imported package\n",
17871787
stderr);
17881788
header = 0;
17891789
}
1790+
#undef header
17901791

17911792
import_level++;
17921793
t1 = _PyTime_GetPerfCounter();
@@ -1816,6 +1817,8 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name)
18161817
}
18171818

18181819
return mod;
1820+
#undef import_level
1821+
#undef accumulated
18191822
}
18201823

18211824
PyObject *

Tools/c-analyzer/cpython/globals-to-fix.tsv

-6
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,6 @@ Python/dtoa.c - p5s -
368368
Python/fileutils.c - _Py_open_cloexec_works -
369369
Python/fileutils.c - force_ascii -
370370
Python/fileutils.c set_inheritable ioctl_works -
371-
Python/import.c - import_lock -
372-
Python/import.c import_find_and_load header -
373371

374372
#-----------------------
375373
# unlikely to change after init (or main thread)
@@ -431,10 +429,6 @@ Python/bootstrap_hash.c - urandom_cache -
431429
Python/ceval_gil.c make_pending_calls busy -
432430
Python/ceval.c _PyEval_SetProfile reentrant -
433431
Python/ceval.c _PyEval_SetTrace reentrant -
434-
Python/import.c - import_lock_level -
435-
Python/import.c - import_lock_thread -
436-
Python/import.c import_find_and_load accumulated -
437-
Python/import.c import_find_and_load import_level -
438432
Python/modsupport.c - _Py_PackageContext -
439433
Python/thread_pthread_stubs.h - py_tls_entries -
440434
Python/pyfpe.c - PyFPE_counter -

0 commit comments

Comments
 (0)