Skip to content

Commit 0862505

Browse files
authored
bpo-9263: Use _PyObject_ASSERT() in typeobject.c (GH-10111)
Replace assert() with _PyObject_ASSERT() in Objects/typeobject.c to dump the faulty object on assertion failure to ease debugging.
1 parent a4b2bc7 commit 0862505

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

‎Objects/typeobject.c‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ subtype_dealloc(PyObject *self)
11161116

11171117
/* Extract the type; we expect it to be a heap type */
11181118
type = Py_TYPE(self);
1119-
assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
1119+
_PyObject_ASSERT((PyObject *)type, type->tp_flags & Py_TPFLAGS_HEAPTYPE);
11201120

11211121
/* Test whether the type has GC exactly once */
11221122

@@ -2208,17 +2208,19 @@ subtype_getweakref(PyObject *obj, void *context)
22082208
{
22092209
PyObject **weaklistptr;
22102210
PyObject *result;
2211+
PyTypeObject *type = Py_TYPE(obj);
22112212

2212-
if (Py_TYPE(obj)->tp_weaklistoffset == 0) {
2213+
if (type->tp_weaklistoffset == 0) {
22132214
PyErr_SetString(PyExc_AttributeError,
22142215
"This object has no __weakref__");
22152216
return NULL;
22162217
}
2217-
assert(Py_TYPE(obj)->tp_weaklistoffset > 0);
2218-
assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <=
2219-
(size_t)(Py_TYPE(obj)->tp_basicsize));
2220-
weaklistptr = (PyObject **)
2221-
((char *)obj + Py_TYPE(obj)->tp_weaklistoffset);
2218+
_PyObject_ASSERT((PyObject *)type,
2219+
type->tp_weaklistoffset > 0);
2220+
_PyObject_ASSERT((PyObject *)type,
2221+
((type->tp_weaklistoffset + sizeof(PyObject *))
2222+
<= (size_t)(type->tp_basicsize)));
2223+
weaklistptr = (PyObject **)((char *)obj + type->tp_weaklistoffset);
22222224
if (*weaklistptr == NULL)
22232225
result = Py_None;
22242226
else
@@ -3279,7 +3281,7 @@ type_dealloc(PyTypeObject *type)
32793281
PyObject *tp, *val, *tb;
32803282

32813283
/* Assert this is a heap-allocated type object */
3282-
assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
3284+
_PyObject_ASSERT((PyObject *)type, type->tp_flags & Py_TPFLAGS_HEAPTYPE);
32833285
_PyObject_GC_UNTRACK(type);
32843286
PyErr_Fetch(&tp, &val, &tb);
32853287
remove_all_subclasses(type, type->tp_bases);
@@ -3503,7 +3505,7 @@ type_clear(PyTypeObject *type)
35033505
PyDictKeysObject *cached_keys;
35043506
/* Because of type_is_gc(), the collector only calls this
35053507
for heaptypes. */
3506-
assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
3508+
_PyObject_ASSERT((PyObject *)type, type->tp_flags & Py_TPFLAGS_HEAPTYPE);
35073509

35083510
/* We need to invalidate the method cache carefully before clearing
35093511
the dict, so that other objects caught in a reference cycle
@@ -5117,7 +5119,8 @@ PyType_Ready(PyTypeObject *type)
51175119
assert(_PyType_CheckConsistency(type));
51185120
return 0;
51195121
}
5120-
assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
5122+
_PyObject_ASSERT((PyObject *)type,
5123+
(type->tp_flags & Py_TPFLAGS_READYING) == 0);
51215124

51225125
type->tp_flags |= Py_TPFLAGS_READYING;
51235126

0 commit comments

Comments
 (0)