Skip to content
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

gh-117398: Use Per-Interpreter State for the _datetime Static Types #119929

Merged
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
8463ae8
Use the static types directly.
ericsnowcurrently May 28, 2024
ead0083
Use the UTC singleton directly.
ericsnowcurrently May 28, 2024
4c52d5d
Add module state.
ericsnowcurrently May 27, 2024
6b291c8
Clear the module state when destroying the module.
ericsnowcurrently May 28, 2024
69c7e1f
Create PyDateTime_IsoCalendarDateType in init_state().
ericsnowcurrently May 29, 2024
36dbeea
Track the "current" module in the internal per-interpreter dict.
ericsnowcurrently May 29, 2024
d0c0b2d
Copy the "current" module state, if available.
ericsnowcurrently May 29, 2024
0599dd6
Make state usage explicit.
ericsnowcurrently May 29, 2024
1f1f6fc
Reduce churn.
ericsnowcurrently May 29, 2024
d44d6e9
Drop _datetime_global_state.
ericsnowcurrently May 29, 2024
a309474
Drop datetime_state.initialized.
ericsnowcurrently May 29, 2024
ede4415
Fix refleaks.
ericsnowcurrently May 30, 2024
5a8b1aa
Clear the "current" module when finalizing the module.
ericsnowcurrently May 30, 2024
3de1cd3
Use a weakref when tracking the "current" module.
ericsnowcurrently May 30, 2024
62b3d5e
Fix clear_current_module().
ericsnowcurrently May 30, 2024
5c25927
Give each module its own heap types.
ericsnowcurrently May 31, 2024
da24674
Consolidate into init_state().
ericsnowcurrently May 31, 2024
f8420ea
Use _Py_ID() for INTERP_KEY.
ericsnowcurrently May 31, 2024
2a2c0b1
Handle PyWeakref_GetRef() returning None.
ericsnowcurrently May 31, 2024
c519e3c
Fix refleak.
ericsnowcurrently May 31, 2024
05acc56
Make sure the module exists in static type methods.
ericsnowcurrently May 31, 2024
07e3b65
Don't bother making the module temporary.
ericsnowcurrently May 31, 2024
1e3005d
_PyStaticType_Dealloc() -> _PyStaticType_FiniBuiltin()
ericsnowcurrently May 31, 2024
ab38857
Add _PyStaticType_InitForExtension() and _PyStaticType_FiniForExtensi…
ericsnowcurrently May 31, 2024
0fb6bb3
Use them for the _datetime module.
ericsnowcurrently Jun 1, 2024
9e4eee6
Mark the module as safe for subinterpreters.
ericsnowcurrently Jun 1, 2024
293fd2e
Add a NEWS entry.
ericsnowcurrently Jun 1, 2024
f0db33a
Ignore the new global vars in the C analyzer tool.
ericsnowcurrently Jun 1, 2024
326d957
Fix a smelly name.
ericsnowcurrently Jun 1, 2024
04eb383
Fix a comment.
ericsnowcurrently Jun 3, 2024
0e8c3b1
Fix callback_for_interp_exit().
ericsnowcurrently Jun 3, 2024
67812dc
Pass around "initial" instead of using _Py_IsMainInterpreter().
ericsnowcurrently Jun 3, 2024
e5ab548
Finalize the static types if PyUnstable_AtExit() fails.
ericsnowcurrently Jun 3, 2024
4f21a28
Make sure we're using the right index for extension static types.
ericsnowcurrently Jun 3, 2024
0fd9fae
Use PyErr_WriteUnraisable().
ericsnowcurrently Jun 3, 2024
3905ae8
Clean up clear_current_module() a little.
ericsnowcurrently Jun 3, 2024
7216e24
Update a TODO comment.
ericsnowcurrently Jun 3, 2024
349f910
Merge branch 'main' into datetime-static-types
ericsnowcurrently Jun 3, 2024
e057548
Update Misc/NEWS.d/next/Library/2024-06-01-16-58-43.gh-issue-117398.k…
ericsnowcurrently Jun 3, 2024
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
2 changes: 1 addition & 1 deletion Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ _PyObject_GET_WEAKREFS_LISTPTR(PyObject *op)
if (PyType_Check(op) &&
((PyTypeObject *)op)->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
PyInterpreterState *interp = _PyInterpreterState_GET();
static_builtin_state *state = _PyStaticType_GetState(
managed_static_type_state *state = _PyStaticType_GetState(
interp, (PyTypeObject *)op);
return _PyStaticType_GET_WEAKREFS_LISTPTR(state);
}
Expand Down
48 changes: 38 additions & 10 deletions Include/internal/pycore_typeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ struct type_cache {

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

typedef struct {
PyTypeObject *type;
int isbuiltin;
int readying;
int ready;
// XXX tp_dict can probably be statically allocated,
Expand All @@ -59,7 +61,7 @@ typedef struct {
are also some diagnostic uses for the list of weakrefs,
so we still keep it. */
PyObject *tp_weaklist;
} static_builtin_state;
} managed_static_type_state;

struct types_state {
/* Used to set PyTypeObject.tp_version_tag.
Expand Down Expand Up @@ -105,8 +107,16 @@ struct types_state {
num_builtins_initialized is incremented once for each static
builtin type. Once initialization is over for a subinterpreter,
the value will be the same as for all other interpreters. */
size_t num_builtins_initialized;
static_builtin_state builtins[_Py_MAX_STATIC_BUILTIN_TYPES];
struct {
size_t num_initialized;
managed_static_type_state initialized[_Py_MAX_MANAGED_STATIC_BUILTIN_TYPES];
} builtins;
/* We apply a similar strategy for managed extension modules. */
struct {
size_t num_initialized;
size_t next_index;
managed_static_type_state initialized[_Py_MAX_MANAGED_STATIC_EXT_TYPES];
} for_extensions;
PyMutex mutex;
};

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


static inline PyObject **
_PyStaticType_GET_WEAKREFS_LISTPTR(static_builtin_state *state)
_PyStaticType_GET_WEAKREFS_LISTPTR(managed_static_type_state *state)
{
assert(state != NULL);
return &state->tp_weaklist;
}

extern int _PyStaticType_InitBuiltin(
PyInterpreterState *interp,
PyTypeObject *type);
extern void _PyStaticType_FiniBuiltin(
PyInterpreterState *interp,
PyTypeObject *type);
extern void _PyStaticType_ClearWeakRefs(
PyInterpreterState *interp,
PyTypeObject *type);
extern managed_static_type_state * _PyStaticType_GetState(
PyInterpreterState *interp,
PyTypeObject *type);

// Export for '_datetime' shared extension.
PyAPI_FUNC(int) _PyStaticType_InitForExtension(
PyInterpreterState *interp,
PyTypeObject *self);
PyAPI_FUNC(void) _PyStaticType_FiniForExtension(
PyInterpreterState *interp,
PyTypeObject *self,
int final);


/* Like PyType_GetModuleState, but skips verification
* that type is a heap type with an associated module */
static inline void *
Expand All @@ -151,11 +184,6 @@ _PyType_GetModuleState(PyTypeObject *type)
}


extern int _PyStaticType_InitBuiltin(PyInterpreterState *, PyTypeObject *type);
extern static_builtin_state * _PyStaticType_GetState(PyInterpreterState *, PyTypeObject *);
extern void _PyStaticType_ClearWeakRefs(PyInterpreterState *, PyTypeObject *type);
extern void _PyStaticType_Dealloc(PyInterpreterState *, PyTypeObject *);

// Export for 'math' shared extension, used via _PyType_IsReady() static inline
// function
PyAPI_FUNC(PyObject *) _PyType_GetDict(PyTypeObject *);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The ``_datetime`` module (C implementation for ``datetime``) now supports
being imported in multiple interpreters.
Loading
Loading