gh-94673: Hide Objects in PyTypeObject Behind Accessors (gh-104074)

This makes it much cleaner to move more PyTypeObject fields to PyInterpreterState.
This commit is contained in:
Eric Snow 2023-05-01 20:34:43 -06:00 committed by GitHub
parent fdd878650d
commit f73abf8e03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 352 additions and 244 deletions

View File

@ -331,10 +331,6 @@ extern int _Py_CheckSlotResult(
const char *slot_name,
int success);
// PyType_Ready() must be called if _PyType_IsReady() is false.
// See also the Py_TPFLAGS_READY flag.
#define _PyType_IsReady(type) ((type)->tp_dict != NULL)
// Test if a type supports weak references
static inline int _PyType_SUPPORTS_WEAKREFS(PyTypeObject *type) {
return (type->tp_weaklistoffset != 0);
@ -392,8 +388,6 @@ _PyDictOrValues_SetValues(PyDictOrValues *ptr, PyDictValues *values)
extern PyObject ** _PyObject_ComputedDictPointer(PyObject *);
extern void _PyObject_FreeInstanceAttributes(PyObject *obj);
extern int _PyObject_IsInstanceDictEmpty(PyObject *);
extern int _PyType_HasSubclasses(PyTypeObject *);
extern PyObject* _PyType_GetSubclasses(PyTypeObject *);
// Access macro to the members which are floating "behind" the object
static inline PyMemberDef* _PyHeapType_GET_MEMBERS(PyHeapTypeObject *etype) {

View File

@ -109,6 +109,20 @@ extern static_builtin_state * _PyStaticType_GetState(PyInterpreterState *, PyTyp
extern void _PyStaticType_ClearWeakRefs(PyInterpreterState *, PyTypeObject *type);
extern void _PyStaticType_Dealloc(PyInterpreterState *, PyTypeObject *);
PyAPI_FUNC(PyObject *) _PyType_GetDict(PyTypeObject *);
extern PyObject * _PyType_GetBases(PyTypeObject *type);
extern PyObject * _PyType_GetMRO(PyTypeObject *type);
extern PyObject* _PyType_GetSubclasses(PyTypeObject *);
extern int _PyType_HasSubclasses(PyTypeObject *);
// PyType_Ready() must be called if _PyType_IsReady() is false.
// See also the Py_TPFLAGS_READY flag.
static inline int
_PyType_IsReady(PyTypeObject *type)
{
return _PyType_GetDict(type) != NULL;
}
PyObject *
_Py_type_getattro_impl(PyTypeObject *type, PyObject *name, int *suppress_missing_attribute);
PyObject *

View File

@ -452,7 +452,8 @@ _abc__abc_init(PyObject *module, PyObject *self)
* their special status w.r.t. pattern matching. */
if (PyType_Check(self)) {
PyTypeObject *cls = (PyTypeObject *)self;
PyObject *flags = PyDict_GetItemWithError(cls->tp_dict,
PyObject *dict = _PyType_GetDict(cls);
PyObject *flags = PyDict_GetItemWithError(dict,
&_Py_ID(__abc_tpflags__));
if (flags == NULL) {
if (PyErr_Occurred()) {
@ -471,7 +472,7 @@ _abc__abc_init(PyObject *module, PyObject *self)
}
((PyTypeObject *)self)->tp_flags |= (val & COLLECTION_FLAGS);
}
if (PyDict_DelItem(cls->tp_dict, &_Py_ID(__abc_tpflags__)) < 0) {
if (PyDict_DelItem(dict, &_Py_ID(__abc_tpflags__)) < 0) {
return NULL;
}
}
@ -742,7 +743,7 @@ _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self,
Py_DECREF(ok);
/* 4. Check if it's a direct subclass. */
PyObject *mro = ((PyTypeObject *)subclass)->tp_mro;
PyObject *mro = _PyType_GetMRO((PyTypeObject *)subclass);
assert(PyTuple_Check(mro));
for (pos = 0; pos < PyTuple_GET_SIZE(mro); pos++) {
PyObject *mro_item = PyTuple_GET_ITEM(mro, pos);

View File

@ -26,7 +26,7 @@ const char * const PyStructSequence_UnnamedField = "unnamed field";
static Py_ssize_t
get_type_attr_as_size(PyTypeObject *tp, PyObject *name)
{
PyObject *v = PyDict_GetItemWithError(tp->tp_dict, name);
PyObject *v = PyDict_GetItemWithError(_PyType_GetDict(tp), name);
if (v == NULL && !PyErr_Occurred()) {
PyErr_Format(PyExc_TypeError,
"Missed attribute '%U' of type %s",
@ -493,7 +493,7 @@ initialize_static_type(PyTypeObject *type, PyStructSequence_Desc *desc,
Py_INCREF(type);
if (initialize_structseq_dict(
desc, type->tp_dict, n_members, n_unnamed_members) < 0) {
desc, _PyType_GetDict(type), n_members, n_unnamed_members) < 0) {
Py_DECREF(type);
return -1;
}
@ -549,7 +549,7 @@ _PyStructSequence_InitBuiltinWithFlags(PyInterpreterState *interp,
}
if (initialize_structseq_dict(
desc, type->tp_dict, n_members, n_unnamed_members) < 0) {
desc, _PyType_GetDict(type), n_members, n_unnamed_members) < 0) {
goto error;
}
@ -675,7 +675,7 @@ _PyStructSequence_NewType(PyStructSequence_Desc *desc, unsigned long tp_flags)
}
if (initialize_structseq_dict(
desc, type->tp_dict, n_members, n_unnamed_members) < 0) {
desc, _PyType_GetDict(type), n_members, n_unnamed_members) < 0) {
Py_DECREF(type);
return NULL;
}

File diff suppressed because it is too large Load Diff

View File

@ -1309,7 +1309,7 @@ _PyContext_Init(PyInterpreterState *interp)
PyObject *missing = get_token_missing();
if (PyDict_SetItemString(
PyContextToken_Type.tp_dict, "MISSING", missing))
_PyType_GetDict(&PyContextToken_Type), "MISSING", missing))
{
Py_DECREF(missing);
return _PyStatus_ERR("can't init context types");