Skip to content

Commit f531b68

Browse files
gh-81057: Add PyInterpreterState.static_objects (gh-99397)
As we consolidate global variables, we find some objects that are almost suitable to add to _PyRuntimeState.global_objects, but have some small/sneaky bit of per-interpreter state (e.g. a weakref list). We're adding PyInterpreterState.static_objects so we can move such objects there. (We'll removed the _not_used field once we've added others.) #81057
1 parent dd36b71 commit f531b68

File tree

6 files changed

+35
-5
lines changed

6 files changed

+35
-5
lines changed

Include/internal/pycore_global_objects.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ struct _Py_global_objects {
4949
PyObject *interned;
5050
};
5151

52+
#define _Py_INTERP_CACHED_OBJECT(interp, NAME) \
53+
(interp)->cached_objects.NAME
54+
55+
struct _Py_interp_cached_objects {
56+
int _not_set;
57+
};
58+
59+
#define _Py_INTERP_STATIC_OBJECT(interp, NAME) \
60+
(interp)->static_objects.NAME
61+
#define _Py_INTERP_SINGLETON(interp, NAME) \
62+
_Py_INTERP_STATIC_OBJECT(interp, singletons.NAME)
63+
64+
struct _Py_interp_static_objects {
65+
struct {
66+
int _not_used;
67+
} singletons;
68+
};
69+
5270

5371
#ifdef __cplusplus
5472
}

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_interp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extern "C" {
2020
#include "pycore_genobject.h" // struct _Py_async_gen_state
2121
#include "pycore_gc.h" // struct _gc_runtime_state
2222
#include "pycore_list.h" // struct _Py_list_state
23+
#include "pycore_global_objects.h" // struct _Py_interp_static_objects
2324
#include "pycore_tuple.h" // struct _Py_tuple_state
2425
#include "pycore_typeobject.h" // struct type_cache
2526
#include "pycore_unicodeobject.h" // struct _Py_unicode_state
@@ -207,6 +208,9 @@ struct _is {
207208
struct callable_cache callable_cache;
208209
PyCodeObject *interpreter_trampoline;
209210

211+
struct _Py_interp_cached_objects cached_objects;
212+
struct _Py_interp_static_objects static_objects;
213+
210214
/* The following fields are here to avoid allocation during init.
211215
The data is exposed through PyInterpreterState pointer fields.
212216
These fields should not be accessed directly outside of init.

Include/internal/pycore_runtime_init.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ extern "C" {
7777
{ .threshold = 10, }, \
7878
}, \
7979
}, \
80+
.static_objects = { \
81+
.singletons = { \
82+
._not_used = 1, \
83+
}, \
84+
}, \
8085
._initial_thread = _PyThreadState_INIT, \
8186
}
8287

Python/pylifecycle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,7 @@ finalize_interp_types(PyInterpreterState *interp)
17441744
_PyUnicode_Fini(interp);
17451745
_PyFloat_Fini(interp);
17461746
#ifdef Py_DEBUG
1747-
_PyStaticObjects_CheckRefcnt();
1747+
_PyStaticObjects_CheckRefcnt(interp);
17481748
#endif
17491749
}
17501750

Tools/build/generate_global_objects.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,10 @@ def generate_global_object_finalizers(generated_immortal_objects):
383383
printer.write(START)
384384
printer.write('#ifdef Py_DEBUG')
385385
printer.write("static inline void")
386-
with printer.block("_PyStaticObjects_CheckRefcnt(void)"):
387-
printer.write('/* generated (see pycore_runtime_init_generated.h) */')
386+
with printer.block(
387+
"_PyStaticObjects_CheckRefcnt(PyInterpreterState *interp)"):
388+
printer.write('/* generated runtime-global */')
389+
printer.write('// (see pycore_runtime_init_generated.h)')
388390
for ref in generated_immortal_objects:
389391
printer.write(f'_PyStaticObject_CheckRefcnt({ref});')
390392
printer.write('/* non-generated */')

0 commit comments

Comments
 (0)