Skip to content

Commit 2e8d3f2

Browse files
Isolate tp_mro.
1 parent fbe6eac commit 2e8d3f2

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

Include/internal/pycore_typeobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ extern void _PyTypes_Fini(PyInterpreterState *);
7676

7777
/* other API */
7878

79+
extern PyObject * _PyType_GetMRO(PyTypeObject *);
80+
7981
/* Length of array of slotdef pointers used to store slots with the
8082
same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
8183
the same __name__, for any __name__. Since that's a static property, it is

Modules/_abc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "pycore_moduleobject.h" // _PyModule_GetState()
88
#include "pycore_object.h" // _PyType_GetSubclasses()
99
#include "pycore_runtime.h" // _Py_ID()
10+
#include "pycore_typeobject.h" // _PyType_GetMRO()
1011
#include "clinic/_abc.c.h"
1112

1213
/*[clinic input]
@@ -742,7 +743,7 @@ _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self,
742743
Py_DECREF(ok);
743744

744745
/* 4. Check if it's a direct subclass. */
745-
PyObject *mro = ((PyTypeObject *)subclass)->tp_mro;
746+
PyObject *mro = _PyType_GetMRO((PyTypeObject *)subclass);
746747
assert(PyTuple_Check(mro));
747748
for (pos = 0; pos < PyTuple_GET_SIZE(mro); pos++) {
748749
PyObject *mro_item = PyTuple_GET_ITEM(mro, pos);

Objects/typeobject.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,18 +871,41 @@ type_get_bases(PyTypeObject *type, void *context)
871871
static inline PyObject *
872872
get_mro(PyTypeObject *type)
873873
{
874+
if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
875+
static_builtin_state *state = _PyStaticType_GetState(type);
876+
assert(state != NULL);
877+
return state->tp_mro;
878+
}
874879
return type->tp_mro;
875880
}
876881

882+
PyObject *
883+
_PyType_GetMRO(PyTypeObject *type)
884+
{
885+
return get_mro(type);
886+
}
887+
877888
static inline void
878889
set_mro(PyTypeObject *type, PyObject *mro)
879890
{
891+
if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
892+
static_builtin_state *state = _PyStaticType_GetState(type);
893+
assert(state != NULL);
894+
state->tp_mro = mro;
895+
return;
896+
}
880897
type->tp_mro = mro;
881898
}
882899

883900
static inline void
884901
clear_mro(PyTypeObject *type)
885902
{
903+
if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
904+
static_builtin_state *state = _PyStaticType_GetState(type);
905+
assert(state != NULL);
906+
Py_CLEAR(state->tp_mro);
907+
return;
908+
}
886909
Py_CLEAR(type->tp_mro);
887910
}
888911

@@ -4564,7 +4587,7 @@ clear_static_type_interp_data(PyTypeObject *type)
45644587

45654588
Py_CLEAR(type->tp_dict);
45664589
Py_CLEAR(state->tp_bases);
4567-
Py_CLEAR(type->tp_mro);
4590+
Py_CLEAR(state->tp_mro);
45684591
}
45694592
else {
45704593
Py_CLEAR(type->tp_dict);

0 commit comments

Comments
 (0)