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

gh-94673: Properly Initialize and Finalize Static Builtin Types for Each Interpreter #104072

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

Merged
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
Pass interp to _PyStaticType_InitBuiltin() and _PyStaticType_Dealloc().
  • Loading branch information
ericsnowcurrently committed May 1, 2023
commit 4a78881aa023083c27d57c36233b167f54eb05f2
3 changes: 2 additions & 1 deletion 3 Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,9 @@ _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(
(PyTypeObject *)op);
interp, (PyTypeObject *)op);
return _PyStaticType_GET_WEAKREFS_LISTPTR(state);
}
// Essentially _PyObject_GET_WEAKREFS_LISTPTR_FROM_OFFSET():
Expand Down
8 changes: 4 additions & 4 deletions 8 Include/internal/pycore_typeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ _PyType_GetModuleState(PyTypeObject *type)
}


extern int _PyStaticType_InitBuiltin(PyTypeObject *type);
extern static_builtin_state * _PyStaticType_GetState(PyTypeObject *);
extern void _PyStaticType_ClearWeakRefs(PyTypeObject *type);
extern void _PyStaticType_Dealloc(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 *);

PyObject *
_Py_type_getattro_impl(PyTypeObject *type, PyObject *name, int *suppress_missing_attribute);
Expand Down
4 changes: 2 additions & 2 deletions 4 Modules/_io/_iomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ _PyIO_InitTypes(PyInterpreterState *interp)

for (size_t i=0; i < Py_ARRAY_LENGTH(static_types); i++) {
PyTypeObject *type = static_types[i];
if (_PyStaticType_InitBuiltin(type) < 0) {
if (_PyStaticType_InitBuiltin(interp, type) < 0) {
return _PyStatus_ERR("Can't initialize builtin type");
}
}
Expand All @@ -699,7 +699,7 @@ _PyIO_FiniTypes(PyInterpreterState *interp)
// their base classes.
for (Py_ssize_t i=Py_ARRAY_LENGTH(static_types) - 1; i >= 0; i--) {
PyTypeObject *type = static_types[i];
_PyStaticType_Dealloc(type);
_PyStaticType_Dealloc(interp, type);
}
}

Expand Down
4 changes: 2 additions & 2 deletions 4 Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -3598,7 +3598,7 @@ _PyExc_InitTypes(PyInterpreterState *interp)
{
for (size_t i=0; i < Py_ARRAY_LENGTH(static_exceptions); i++) {
PyTypeObject *exc = static_exceptions[i].exc;
if (_PyStaticType_InitBuiltin(exc) < 0) {
if (_PyStaticType_InitBuiltin(interp, exc) < 0) {
return -1;
}
}
Expand All @@ -3615,7 +3615,7 @@ _PyExc_FiniTypes(PyInterpreterState *interp)

for (Py_ssize_t i=Py_ARRAY_LENGTH(static_exceptions) - 1; i >= 0; i--) {
PyTypeObject *exc = static_exceptions[i].exc;
_PyStaticType_Dealloc(exc);
_PyStaticType_Dealloc(interp, exc);
}
}

Expand Down
4 changes: 2 additions & 2 deletions 4 Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2105,7 +2105,7 @@ _PyTypes_InitTypes(PyInterpreterState *interp)
// All other static types (unless initialized elsewhere)
for (size_t i=0; i < Py_ARRAY_LENGTH(static_types); i++) {
PyTypeObject *type = static_types[i];
if (_PyStaticType_InitBuiltin(type) < 0) {
if (_PyStaticType_InitBuiltin(interp, type) < 0) {
return _PyStatus_ERR("Can't initialize builtin type");
}
if (type == &PyType_Type) {
Expand Down Expand Up @@ -2136,7 +2136,7 @@ _PyTypes_FiniTypes(PyInterpreterState *interp)
// their base classes.
for (Py_ssize_t i=Py_ARRAY_LENGTH(static_types)-1; i>=0; i--) {
PyTypeObject *type = static_types[i];
_PyStaticType_Dealloc(type);
_PyStaticType_Dealloc(interp, type);
}
}

Expand Down
16 changes: 9 additions & 7 deletions 16 Objects/structseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ _PyStructSequence_InitBuiltinWithFlags(PyInterpreterState *interp,
}
#endif

if (_PyStaticType_InitBuiltin(type) < 0) {
if (_PyStaticType_InitBuiltin(interp, type) < 0) {
PyErr_Format(PyExc_RuntimeError,
"Can't initialize builtin type %s",
desc->name);
Expand Down Expand Up @@ -621,13 +621,15 @@ _PyStructSequence_FiniBuiltin(PyInterpreterState *interp, PyTypeObject *type)
return;
}

_PyStaticType_Dealloc(type);
_PyStaticType_Dealloc(interp, type);

// Undo _PyStructSequence_InitBuiltinWithFlags().
type->tp_name = NULL;
PyMem_Free(type->tp_members);
type->tp_members = NULL;
type->tp_base = NULL;
if (_Py_IsMainInterpreter(interp)) {
// Undo _PyStructSequence_InitBuiltinWithFlags().
type->tp_name = NULL;
PyMem_Free(type->tp_members);
type->tp_members = NULL;
type->tp_base = NULL;
}
}


Expand Down
41 changes: 20 additions & 21 deletions 41 Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,16 @@ static_builtin_state_get(PyInterpreterState *interp, PyTypeObject *self)

/* For static types we store some state in an array on each interpreter. */
static_builtin_state *
_PyStaticType_GetState(PyTypeObject *self)
_PyStaticType_GetState(PyInterpreterState *interp, PyTypeObject *self)
{
assert(self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN);
PyInterpreterState *interp = _PyInterpreterState_GET();
return static_builtin_state_get(interp, self);
}

/* Set the type's per-interpreter state. */
static void
static_builtin_state_init(PyTypeObject *self)
static_builtin_state_init(PyInterpreterState *interp, PyTypeObject *self)
{
/* Set the type's per-interpreter state. */
PyInterpreterState *interp = _PyInterpreterState_GET();

/* It should only be called once for each builtin type. */
assert(!static_builtin_index_is_set(self));

Expand All @@ -133,13 +130,11 @@ static_builtin_state_init(PyTypeObject *self)
(in weakrefobject.c) sets it. */
}

/* Reset the type's per-interpreter state.
This basically undoes what static_builtin_state_init() did. */
static void
static_builtin_state_clear(PyTypeObject *self)
static_builtin_state_clear(PyInterpreterState *interp, PyTypeObject *self)
{
/* Reset the type's per-interpreter state.
This basically undoes what static_builtin_state_init() did. */
PyInterpreterState *interp = _PyInterpreterState_GET();

static_builtin_state *state = static_builtin_state_get(interp, self);
state->type = NULL;
assert(state->tp_weaklist == NULL); // It was already cleared out.
Expand Down Expand Up @@ -4492,7 +4487,7 @@ clear_static_tp_subclasses(PyTypeObject *type)
}

void
_PyStaticType_Dealloc(PyTypeObject *type)
_PyStaticType_Dealloc(PyInterpreterState *interp, PyTypeObject *type)
{
assert(!(type->tp_flags & Py_TPFLAGS_HEAPTYPE));

Expand All @@ -4514,8 +4509,8 @@ _PyStaticType_Dealloc(PyTypeObject *type)
type->tp_version_tag = 0;

if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
_PyStaticType_ClearWeakRefs(type);
static_builtin_state_clear(type);
_PyStaticType_ClearWeakRefs(interp, type);
static_builtin_state_clear(interp, type);
/* We leave _Py_TPFLAGS_STATIC_BUILTIN set on tp_flags. */
}
}
Expand Down Expand Up @@ -4564,7 +4559,8 @@ static PyObject *
lookup_subclasses(PyTypeObject *self)
{
if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
static_builtin_state *state = _PyStaticType_GetState(self);
PyInterpreterState *interp = _PyInterpreterState_GET();
static_builtin_state *state = _PyStaticType_GetState(interp, self);
assert(state != NULL);
return state->tp_subclasses;
}
Expand All @@ -4574,8 +4570,9 @@ lookup_subclasses(PyTypeObject *self)
int
_PyType_HasSubclasses(PyTypeObject *self)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN &&
_PyStaticType_GetState(self) == NULL) {
_PyStaticType_GetState(interp, self) == NULL) {
return 0;
}
if (lookup_subclasses(self) == NULL) {
Expand Down Expand Up @@ -7030,7 +7027,7 @@ PyType_Ready(PyTypeObject *type)
}

int
_PyStaticType_InitBuiltin(PyTypeObject *self)
_PyStaticType_InitBuiltin(PyInterpreterState *interp, PyTypeObject *self)
{
assert(_Py_IsImmortal((PyObject *)self));
assert(!(self->tp_flags & Py_TPFLAGS_HEAPTYPE));
Expand All @@ -7048,11 +7045,11 @@ _PyStaticType_InitBuiltin(PyTypeObject *self)
self->tp_version_tag = NEXT_GLOBAL_VERSION_TAG++;
self->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG;

static_builtin_state_init(self);
static_builtin_state_init(interp, self);

int res = type_ready(self);
if (res < 0) {
static_builtin_state_clear(self);
static_builtin_state_clear(interp, self);
}
return res;
}
Expand All @@ -7066,7 +7063,8 @@ init_subclasses(PyTypeObject *self)
return NULL;
}
if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
static_builtin_state *state = _PyStaticType_GetState(self);
PyInterpreterState *interp = _PyInterpreterState_GET();
static_builtin_state *state = _PyStaticType_GetState(interp, self);
state->tp_subclasses = subclasses;
return subclasses;
}
Expand All @@ -7081,7 +7079,8 @@ clear_subclasses(PyTypeObject *self)
callers also test if tp_subclasses is NULL to check if a static type
has no subclass. */
if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
static_builtin_state *state = _PyStaticType_GetState(self);
PyInterpreterState *interp = _PyInterpreterState_GET();
static_builtin_state *state = _PyStaticType_GetState(interp, self);
Py_CLEAR(state->tp_subclasses);
return;
}
Expand Down
12 changes: 6 additions & 6 deletions 12 Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -14573,13 +14573,13 @@ _PyUnicode_InitGlobalObjects(PyInterpreterState *interp)
PyStatus
_PyUnicode_InitTypes(PyInterpreterState *interp)
{
if (_PyStaticType_InitBuiltin(&EncodingMapType) < 0) {
if (_PyStaticType_InitBuiltin(interp, &EncodingMapType) < 0) {
goto error;
}
if (_PyStaticType_InitBuiltin(&PyFieldNameIter_Type) < 0) {
if (_PyStaticType_InitBuiltin(interp, &PyFieldNameIter_Type) < 0) {
goto error;
}
if (_PyStaticType_InitBuiltin(&PyFormatterIter_Type) < 0) {
if (_PyStaticType_InitBuiltin(interp, &PyFormatterIter_Type) < 0) {
goto error;
}
return _PyStatus_OK();
Expand Down Expand Up @@ -15162,9 +15162,9 @@ _PyUnicode_FiniTypes(PyInterpreterState *interp)
return;
}

_PyStaticType_Dealloc(&EncodingMapType);
_PyStaticType_Dealloc(&PyFieldNameIter_Type);
_PyStaticType_Dealloc(&PyFormatterIter_Type);
_PyStaticType_Dealloc(interp, &EncodingMapType);
_PyStaticType_Dealloc(interp, &PyFieldNameIter_Type);
_PyStaticType_Dealloc(interp, &PyFormatterIter_Type);
}


Expand Down
4 changes: 2 additions & 2 deletions 4 Objects/weakrefobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1017,9 +1017,9 @@ PyObject_ClearWeakRefs(PyObject *object)
* or anything else.
*/
void
_PyStaticType_ClearWeakRefs(PyTypeObject *type)
_PyStaticType_ClearWeakRefs(PyInterpreterState *interp, PyTypeObject *type)
{
static_builtin_state *state = _PyStaticType_GetState(type);
static_builtin_state *state = _PyStaticType_GetState(interp, type);
PyObject **list = _PyStaticType_GET_WEAKREFS_LISTPTR(state);
while (*list != NULL) {
/* Note that clear_weakref() pops the first ref off the type's
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.