Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 105f22e

Browse filesBrowse files
gh-117398: Use Per-Interpreter State for the _datetime Static Types (gh-119929)
We make use of the same mechanism that we use for the static builtin types. This required a few tweaks. The relevant code could use some cleanup but I opted to avoid the significant churn in this change. I'll tackle that separately. This change is the final piece needed to make _datetime support multiple interpreters. I've updated the module slot accordingly.
1 parent dba7a16 commit 105f22e
Copy full SHA for 105f22e

File tree

Expand file treeCollapse file tree

13 files changed

+381
-166
lines changed
Filter options
Expand file treeCollapse file tree

13 files changed

+381
-166
lines changed

‎Include/internal/pycore_object.h

Copy file name to clipboardExpand all lines: Include/internal/pycore_object.h
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ _PyObject_GET_WEAKREFS_LISTPTR(PyObject *op)
589589
if (PyType_Check(op) &&
590590
((PyTypeObject *)op)->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
591591
PyInterpreterState *interp = _PyInterpreterState_GET();
592-
static_builtin_state *state = _PyStaticType_GetState(
592+
managed_static_type_state *state = _PyStaticType_GetState(
593593
interp, (PyTypeObject *)op);
594594
return _PyStaticType_GET_WEAKREFS_LISTPTR(state);
595595
}

‎Include/internal/pycore_typeobject.h

Copy file name to clipboardExpand all lines: Include/internal/pycore_typeobject.h
+38-10Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ struct type_cache {
4444

4545
/* For now we hard-code this to a value for which we are confident
4646
all the static builtin types will fit (for all builds). */
47-
#define _Py_MAX_STATIC_BUILTIN_TYPES 200
47+
#define _Py_MAX_MANAGED_STATIC_BUILTIN_TYPES 200
48+
#define _Py_MAX_MANAGED_STATIC_EXT_TYPES 10
4849

4950
typedef struct {
5051
PyTypeObject *type;
52+
int isbuiltin;
5153
int readying;
5254
int ready;
5355
// XXX tp_dict can probably be statically allocated,
@@ -59,7 +61,7 @@ typedef struct {
5961
are also some diagnostic uses for the list of weakrefs,
6062
so we still keep it. */
6163
PyObject *tp_weaklist;
62-
} static_builtin_state;
64+
} managed_static_type_state;
6365

6466
struct types_state {
6567
/* Used to set PyTypeObject.tp_version_tag.
@@ -105,8 +107,16 @@ struct types_state {
105107
num_builtins_initialized is incremented once for each static
106108
builtin type. Once initialization is over for a subinterpreter,
107109
the value will be the same as for all other interpreters. */
108-
size_t num_builtins_initialized;
109-
static_builtin_state builtins[_Py_MAX_STATIC_BUILTIN_TYPES];
110+
struct {
111+
size_t num_initialized;
112+
managed_static_type_state initialized[_Py_MAX_MANAGED_STATIC_BUILTIN_TYPES];
113+
} builtins;
114+
/* We apply a similar strategy for managed extension modules. */
115+
struct {
116+
size_t num_initialized;
117+
size_t next_index;
118+
managed_static_type_state initialized[_Py_MAX_MANAGED_STATIC_EXT_TYPES];
119+
} for_extensions;
110120
PyMutex mutex;
111121
};
112122

@@ -130,12 +140,35 @@ typedef struct wrapperbase pytype_slotdef;
130140

131141

132142
static inline PyObject **
133-
_PyStaticType_GET_WEAKREFS_LISTPTR(static_builtin_state *state)
143+
_PyStaticType_GET_WEAKREFS_LISTPTR(managed_static_type_state *state)
134144
{
135145
assert(state != NULL);
136146
return &state->tp_weaklist;
137147
}
138148

149+
extern int _PyStaticType_InitBuiltin(
150+
PyInterpreterState *interp,
151+
PyTypeObject *type);
152+
extern void _PyStaticType_FiniBuiltin(
153+
PyInterpreterState *interp,
154+
PyTypeObject *type);
155+
extern void _PyStaticType_ClearWeakRefs(
156+
PyInterpreterState *interp,
157+
PyTypeObject *type);
158+
extern managed_static_type_state * _PyStaticType_GetState(
159+
PyInterpreterState *interp,
160+
PyTypeObject *type);
161+
162+
// Export for '_datetime' shared extension.
163+
PyAPI_FUNC(int) _PyStaticType_InitForExtension(
164+
PyInterpreterState *interp,
165+
PyTypeObject *self);
166+
PyAPI_FUNC(void) _PyStaticType_FiniForExtension(
167+
PyInterpreterState *interp,
168+
PyTypeObject *self,
169+
int final);
170+
171+
139172
/* Like PyType_GetModuleState, but skips verification
140173
* that type is a heap type with an associated module */
141174
static inline void *
@@ -151,11 +184,6 @@ _PyType_GetModuleState(PyTypeObject *type)
151184
}
152185

153186

154-
extern int _PyStaticType_InitBuiltin(PyInterpreterState *, PyTypeObject *type);
155-
extern static_builtin_state * _PyStaticType_GetState(PyInterpreterState *, PyTypeObject *);
156-
extern void _PyStaticType_ClearWeakRefs(PyInterpreterState *, PyTypeObject *type);
157-
extern void _PyStaticType_Dealloc(PyInterpreterState *, PyTypeObject *);
158-
159187
// Export for 'math' shared extension, used via _PyType_IsReady() static inline
160188
// function
161189
PyAPI_FUNC(PyObject *) _PyType_GetDict(PyTypeObject *);
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The ``_datetime`` module (C implementation for :mod:`datetime`) now supports
2+
being imported in multiple interpreters.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.