Skip to content

Commit 2470204

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

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

Objects/object.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ _Py_AddToAllObjects(PyObject *op, int force)
7272
/* If it's initialized memory, op must be in or out of
7373
* the list unambiguously.
7474
*/
75-
assert((op->_ob_prev == NULL) == (op->_ob_next == NULL));
75+
_PyObject_ASSERT(op, (op->_ob_prev == NULL) == (op->_ob_next == NULL));
7676
}
7777
#endif
7878
if (force || op->_ob_prev == NULL) {
@@ -305,7 +305,9 @@ PyObject_CallFinalizerFromDealloc(PyObject *self)
305305
/* Undo the temporary resurrection; can't use DECREF here, it would
306306
* cause a recursive call.
307307
*/
308-
assert(self->ob_refcnt > 0);
308+
_PyObject_ASSERT_WITH_MSG(self,
309+
self->ob_refcnt > 0,
310+
"refcount is too small");
309311
if (--self->ob_refcnt == 0)
310312
return 0; /* this is the normal path out */
311313

@@ -316,7 +318,9 @@ PyObject_CallFinalizerFromDealloc(PyObject *self)
316318
_Py_NewReference(self);
317319
self->ob_refcnt = refcnt;
318320

319-
assert(!PyType_IS_GC(Py_TYPE(self)) || _PyObject_GC_IS_TRACKED(self));
321+
_PyObject_ASSERT(self,
322+
(!PyType_IS_GC(Py_TYPE(self))
323+
|| _PyObject_GC_IS_TRACKED(self)));
320324
/* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
321325
* we need to undo that. */
322326
_Py_DEC_REFTOTAL;
@@ -1020,7 +1024,7 @@ PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
10201024
return err;
10211025
}
10221026
Py_DECREF(name);
1023-
assert(name->ob_refcnt >= 1);
1027+
_PyObject_ASSERT(name, name->ob_refcnt >= 1);
10241028
if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
10251029
PyErr_Format(PyExc_TypeError,
10261030
"'%.100s' object has no attributes "
@@ -1059,8 +1063,8 @@ _PyObject_GetDictPtr(PyObject *obj)
10591063
size = _PyObject_VAR_SIZE(tp, tsize);
10601064

10611065
dictoffset += (long)size;
1062-
assert(dictoffset > 0);
1063-
assert(dictoffset % SIZEOF_VOID_P == 0);
1066+
_PyObject_ASSERT(obj, dictoffset > 0);
1067+
_PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
10641068
}
10651069
return (PyObject **) ((char *)obj + dictoffset);
10661070
}
@@ -1247,11 +1251,11 @@ _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
12471251
if (tsize < 0)
12481252
tsize = -tsize;
12491253
size = _PyObject_VAR_SIZE(tp, tsize);
1250-
assert(size <= PY_SSIZE_T_MAX);
1254+
_PyObject_ASSERT(obj, size <= PY_SSIZE_T_MAX);
12511255

12521256
dictoffset += (Py_ssize_t)size;
1253-
assert(dictoffset > 0);
1254-
assert(dictoffset % SIZEOF_VOID_P == 0);
1257+
_PyObject_ASSERT(obj, dictoffset > 0);
1258+
_PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
12551259
}
12561260
dictptr = (PyObject **) ((char *)obj + dictoffset);
12571261
dict = *dictptr;
@@ -1486,7 +1490,7 @@ _dir_object(PyObject *obj)
14861490
PyObject *result, *sorted;
14871491
PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
14881492

1489-
assert(obj);
1493+
assert(obj != NULL);
14901494
if (dirfunc == NULL) {
14911495
if (!PyErr_Occurred())
14921496
PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
@@ -2129,9 +2133,9 @@ Py_ReprLeave(PyObject *obj)
21292133
void
21302134
_PyTrash_deposit_object(PyObject *op)
21312135
{
2132-
assert(PyObject_IS_GC(op));
2133-
assert(!_PyObject_GC_IS_TRACKED(op));
2134-
assert(op->ob_refcnt == 0);
2136+
_PyObject_ASSERT(op, PyObject_IS_GC(op));
2137+
_PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
2138+
_PyObject_ASSERT(op, op->ob_refcnt == 0);
21352139
_PyGCHead_SET_PREV(_Py_AS_GC(op), _PyRuntime.gc.trash_delete_later);
21362140
_PyRuntime.gc.trash_delete_later = op;
21372141
}
@@ -2141,9 +2145,9 @@ void
21412145
_PyTrash_thread_deposit_object(PyObject *op)
21422146
{
21432147
PyThreadState *tstate = PyThreadState_GET();
2144-
assert(PyObject_IS_GC(op));
2145-
assert(!_PyObject_GC_IS_TRACKED(op));
2146-
assert(op->ob_refcnt == 0);
2148+
_PyObject_ASSERT(op, PyObject_IS_GC(op));
2149+
_PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
2150+
_PyObject_ASSERT(op, op->ob_refcnt == 0);
21472151
_PyGCHead_SET_PREV(_Py_AS_GC(op), tstate->trash_delete_later);
21482152
tstate->trash_delete_later = op;
21492153
}
@@ -2167,7 +2171,7 @@ _PyTrash_destroy_chain(void)
21672171
* assorted non-release builds calling Py_DECREF again ends
21682172
* up distorting allocation statistics.
21692173
*/
2170-
assert(op->ob_refcnt == 0);
2174+
_PyObject_ASSERT(op, op->ob_refcnt == 0);
21712175
++_PyRuntime.gc.trash_delete_nesting;
21722176
(*dealloc)(op);
21732177
--_PyRuntime.gc.trash_delete_nesting;
@@ -2205,7 +2209,7 @@ _PyTrash_thread_destroy_chain(void)
22052209
* assorted non-release builds calling Py_DECREF again ends
22062210
* up distorting allocation statistics.
22072211
*/
2208-
assert(op->ob_refcnt == 0);
2212+
_PyObject_ASSERT(op, op->ob_refcnt == 0);
22092213
(*dealloc)(op);
22102214
assert(tstate->trash_delete_nesting == 1);
22112215
}

0 commit comments

Comments
 (0)