Skip to content

Commit c2d5df5

Browse files
authored
gh-83754: Use the Py_TYPE() macro (#120599)
Don't access directly PyObject.ob_type, but use the Py_TYPE() macro instead.
1 parent 3df2022 commit c2d5df5

File tree

6 files changed

+12
-12
lines changed

6 files changed

+12
-12
lines changed

Include/cpython/longintrepr.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ PyAPI_FUNC(PyLongObject*) _PyLong_FromDigits(
120120

121121
static inline int
122122
_PyLong_IsCompact(const PyLongObject* op) {
123-
assert(PyType_HasFeature((op)->ob_base.ob_type, Py_TPFLAGS_LONG_SUBCLASS));
123+
assert(PyType_HasFeature(Py_TYPE(op), Py_TPFLAGS_LONG_SUBCLASS));
124124
return op->long_value.lv_tag < (2 << _PyLong_NON_SIZE_BITS);
125125
}
126126

@@ -130,7 +130,7 @@ static inline Py_ssize_t
130130
_PyLong_CompactValue(const PyLongObject *op)
131131
{
132132
Py_ssize_t sign;
133-
assert(PyType_HasFeature((op)->ob_base.ob_type, Py_TPFLAGS_LONG_SUBCLASS));
133+
assert(PyType_HasFeature(Py_TYPE(op), Py_TPFLAGS_LONG_SUBCLASS));
134134
assert(PyUnstable_Long_IsCompact(op));
135135
sign = 1 - (op->long_value.lv_tag & _PyLong_SIGN_MASK);
136136
return sign * (Py_ssize_t)op->long_value.ob_digit[0];

Include/object.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ PyAPI_DATA(PyTypeObject) PyBool_Type;
261261

262262
// bpo-39573: The Py_SET_SIZE() function must be used to set an object size.
263263
static inline Py_ssize_t Py_SIZE(PyObject *ob) {
264-
assert(ob->ob_type != &PyLong_Type);
265-
assert(ob->ob_type != &PyBool_Type);
264+
assert(Py_TYPE(ob) != &PyLong_Type);
265+
assert(Py_TYPE(ob) != &PyBool_Type);
266266
return _PyVarObject_CAST(ob)->ob_size;
267267
}
268268
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
@@ -289,8 +289,8 @@ static inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
289289
#endif
290290

291291
static inline void Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
292-
assert(ob->ob_base.ob_type != &PyLong_Type);
293-
assert(ob->ob_base.ob_type != &PyBool_Type);
292+
assert(Py_TYPE(_PyObject_CAST(ob)) != &PyLong_Type);
293+
assert(Py_TYPE(_PyObject_CAST(ob)) != &PyBool_Type);
294294
#ifdef Py_GIL_DISABLED
295295
_Py_atomic_store_ssize_relaxed(&ob->ob_size, size);
296296
#else

Modules/_datetimemodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3090,7 +3090,7 @@ static PyDateTime_Delta *
30903090
look_up_delta(int days, int seconds, int microseconds, PyTypeObject *type)
30913091
{
30923092
if (days == 0 && seconds == 0 && microseconds == 0
3093-
&& type == zero_delta.ob_base.ob_type)
3093+
&& type == Py_TYPE(&zero_delta))
30943094
{
30953095
return &zero_delta;
30963096
}

Objects/dictobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2389,7 +2389,7 @@ PyObject *
23892389
_PyDict_GetItemWithError(PyObject *dp, PyObject *kv)
23902390
{
23912391
assert(PyUnicode_CheckExact(kv));
2392-
Py_hash_t hash = kv->ob_type->tp_hash(kv);
2392+
Py_hash_t hash = Py_TYPE(kv)->tp_hash(kv);
23932393
if (hash == -1) {
23942394
return NULL;
23952395
}

Python/gc.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -2083,7 +2083,7 @@ PyVarObject *
20832083
_PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
20842084
{
20852085
const size_t basicsize = _PyObject_VAR_SIZE(Py_TYPE(op), nitems);
2086-
const size_t presize = _PyType_PreHeaderSize(((PyObject *)op)->ob_type);
2086+
const size_t presize = _PyType_PreHeaderSize(Py_TYPE(op));
20872087
_PyObject_ASSERT((PyObject *)op, !_PyObject_GC_IS_TRACKED(op));
20882088
if (basicsize > (size_t)PY_SSIZE_T_MAX - presize) {
20892089
return (PyVarObject *)PyErr_NoMemory();
@@ -2101,15 +2101,15 @@ _PyObject_GC_Resize(PyVarObject *op, Py_ssize_t nitems)
21012101
void
21022102
PyObject_GC_Del(void *op)
21032103
{
2104-
size_t presize = _PyType_PreHeaderSize(((PyObject *)op)->ob_type);
2104+
size_t presize = _PyType_PreHeaderSize(Py_TYPE(op));
21052105
PyGC_Head *g = AS_GC(op);
21062106
if (_PyObject_GC_IS_TRACKED(op)) {
21072107
gc_list_remove(g);
21082108
#ifdef Py_DEBUG
21092109
PyObject *exc = PyErr_GetRaisedException();
21102110
if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
21112111
"gc", NULL, "Object of type %s is not untracked before destruction",
2112-
((PyObject*)op)->ob_type->tp_name)) {
2112+
Py_TYPE(op)->tp_name)) {
21132113
PyErr_WriteUnraisable(NULL);
21142114
}
21152115
PyErr_SetRaisedException(exc);

Python/specialize.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ specialize_module_load_attr(
637637
) {
638638
_PyAttrCache *cache = (_PyAttrCache *)(instr + 1);
639639
PyModuleObject *m = (PyModuleObject *)owner;
640-
assert((owner->ob_type->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0);
640+
assert((Py_TYPE(owner)->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0);
641641
PyDictObject *dict = (PyDictObject *)m->md_dict;
642642
if (dict == NULL) {
643643
SPECIALIZATION_FAIL(LOAD_ATTR, SPEC_FAIL_NO_DICT);

0 commit comments

Comments
 (0)