gh-83754: Use the Py_TYPE() macro (#120599)

Don't access directly PyObject.ob_type, but use the Py_TYPE() macro
instead.
This commit is contained in:
Victor Stinner 2024-06-17 10:34:29 +02:00 committed by GitHub
parent 3df2022931
commit c2d5df5787
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 12 additions and 12 deletions

View File

@ -120,7 +120,7 @@ PyAPI_FUNC(PyLongObject*) _PyLong_FromDigits(
static inline int static inline int
_PyLong_IsCompact(const PyLongObject* op) { _PyLong_IsCompact(const PyLongObject* op) {
assert(PyType_HasFeature((op)->ob_base.ob_type, Py_TPFLAGS_LONG_SUBCLASS)); assert(PyType_HasFeature(Py_TYPE(op), Py_TPFLAGS_LONG_SUBCLASS));
return op->long_value.lv_tag < (2 << _PyLong_NON_SIZE_BITS); return op->long_value.lv_tag < (2 << _PyLong_NON_SIZE_BITS);
} }
@ -130,7 +130,7 @@ static inline Py_ssize_t
_PyLong_CompactValue(const PyLongObject *op) _PyLong_CompactValue(const PyLongObject *op)
{ {
Py_ssize_t sign; Py_ssize_t sign;
assert(PyType_HasFeature((op)->ob_base.ob_type, Py_TPFLAGS_LONG_SUBCLASS)); assert(PyType_HasFeature(Py_TYPE(op), Py_TPFLAGS_LONG_SUBCLASS));
assert(PyUnstable_Long_IsCompact(op)); assert(PyUnstable_Long_IsCompact(op));
sign = 1 - (op->long_value.lv_tag & _PyLong_SIGN_MASK); sign = 1 - (op->long_value.lv_tag & _PyLong_SIGN_MASK);
return sign * (Py_ssize_t)op->long_value.ob_digit[0]; return sign * (Py_ssize_t)op->long_value.ob_digit[0];

View File

@ -261,8 +261,8 @@ PyAPI_DATA(PyTypeObject) PyBool_Type;
// bpo-39573: The Py_SET_SIZE() function must be used to set an object size. // bpo-39573: The Py_SET_SIZE() function must be used to set an object size.
static inline Py_ssize_t Py_SIZE(PyObject *ob) { static inline Py_ssize_t Py_SIZE(PyObject *ob) {
assert(ob->ob_type != &PyLong_Type); assert(Py_TYPE(ob) != &PyLong_Type);
assert(ob->ob_type != &PyBool_Type); assert(Py_TYPE(ob) != &PyBool_Type);
return _PyVarObject_CAST(ob)->ob_size; return _PyVarObject_CAST(ob)->ob_size;
} }
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
@ -289,8 +289,8 @@ static inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
#endif #endif
static inline void Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) { static inline void Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
assert(ob->ob_base.ob_type != &PyLong_Type); assert(Py_TYPE(_PyObject_CAST(ob)) != &PyLong_Type);
assert(ob->ob_base.ob_type != &PyBool_Type); assert(Py_TYPE(_PyObject_CAST(ob)) != &PyBool_Type);
#ifdef Py_GIL_DISABLED #ifdef Py_GIL_DISABLED
_Py_atomic_store_ssize_relaxed(&ob->ob_size, size); _Py_atomic_store_ssize_relaxed(&ob->ob_size, size);
#else #else

View File

@ -3090,7 +3090,7 @@ static PyDateTime_Delta *
look_up_delta(int days, int seconds, int microseconds, PyTypeObject *type) look_up_delta(int days, int seconds, int microseconds, PyTypeObject *type)
{ {
if (days == 0 && seconds == 0 && microseconds == 0 if (days == 0 && seconds == 0 && microseconds == 0
&& type == zero_delta.ob_base.ob_type) && type == Py_TYPE(&zero_delta))
{ {
return &zero_delta; return &zero_delta;
} }

View File

@ -2389,7 +2389,7 @@ PyObject *
_PyDict_GetItemWithError(PyObject *dp, PyObject *kv) _PyDict_GetItemWithError(PyObject *dp, PyObject *kv)
{ {
assert(PyUnicode_CheckExact(kv)); assert(PyUnicode_CheckExact(kv));
Py_hash_t hash = kv->ob_type->tp_hash(kv); Py_hash_t hash = Py_TYPE(kv)->tp_hash(kv);
if (hash == -1) { if (hash == -1) {
return NULL; return NULL;
} }

View File

@ -2083,7 +2083,7 @@ PyVarObject *
_PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems) _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
{ {
const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems); const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
const size_t presize = _PyType_PreHeaderSize(((PyObject *)op)->ob_type); const size_t presize = _PyType_PreHeaderSize(Py_TYPE(op));
_PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op)); _PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op));
if (basicsize > (size_t)PY_SSIZE_T_MAX - presize) { if (basicsize > (size_t)PY_SSIZE_T_MAX - presize) {
return (PyVarObject *)PyErr_NoMemory(); return (PyVarObject *)PyErr_NoMemory();
@ -2101,7 +2101,7 @@ _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
void void
PyObject_GC_Del(void *op) PyObject_GC_Del(void *op)
{ {
size_t presize = _PyType_PreHeaderSize(((PyObject *)op)->ob_type); size_t presize = _PyType_PreHeaderSize(Py_TYPE(op));
PyGC_Head *g = AS_GC(op); PyGC_Head *g = AS_GC(op);
if (_PyObject_GC_IS_TRACKED(op)) { if (_PyObject_GC_IS_TRACKED(op)) {
gc_list_remove(g); gc_list_remove(g);
@ -2109,7 +2109,7 @@ PyObject_GC_Del(void *op)
PyObject *exc = PyErr_GetRaisedException(); PyObject *exc = PyErr_GetRaisedException();
if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0, if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
"gc", NULL, "Object of type %s is not untracked before destruction", "gc", NULL, "Object of type %s is not untracked before destruction",
((PyObject*)op)->ob_type->tp_name)) { Py_TYPE(op)->tp_name)) {
PyErr_WriteUnraisable(NULL); PyErr_WriteUnraisable(NULL);
} }
PyErr_SetRaisedException(exc); PyErr_SetRaisedException(exc);

View File

@ -637,7 +637,7 @@ specialize_module_load_attr(
) { ) {
_PyAttrCache *cache = (_PyAttrCache *)(instr + 1); _PyAttrCache *cache = (_PyAttrCache *)(instr + 1);
PyModuleObject *m = (PyModuleObject *)owner; PyModuleObject *m = (PyModuleObject *)owner;
assert((owner->ob_type->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0); assert((Py_TYPE(owner)->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0);
PyDictObject *dict = (PyDictObject *)m->md_dict; PyDictObject *dict = (PyDictObject *)m->md_dict;
if (dict == NULL) { if (dict == NULL) {
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_NO_DICT); SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_NO_DICT);