Skip to content

Commit 3ec9af7

Browse files
authored
bpo-9263: _Py_NegativeRefcount() use _PyObject_AssertFailed() (GH-10109)
_Py_NegativeRefcount() now uses _PyObject_AssertFailed() to dump the object to help debugging.
1 parent c89a932 commit 3ec9af7

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

Lib/test/test_capi.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,9 @@ def test_negative_refcount(self):
329329
""")
330330
rc, out, err = assert_python_failure('-c', code)
331331
self.assertRegex(err,
332-
br'_testcapimodule\.c:[0-9]+ object at .* '
333-
br'has negative ref count', err)
332+
br'_testcapimodule\.c:[0-9]+: '
333+
br'_Py_NegativeRefcount: Assertion ".*" failed; '
334+
br'object has negative ref count')
334335

335336

336337
class TestPendingCalls(unittest.TestCase):

Objects/object.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,9 @@ void dec_count(PyTypeObject *tp)
205205
void
206206
_Py_NegativeRefcount(const char *filename, int lineno, PyObject *op)
207207
{
208-
char buf[300];
209-
210-
PyOS_snprintf(buf, sizeof(buf),
211-
"%s:%i object at %p has negative ref count "
212-
"%" PY_FORMAT_SIZE_T "d",
213-
filename, lineno, op, op->ob_refcnt);
214-
Py_FatalError(buf);
208+
_PyObject_AssertFailed(op, "object has negative ref count",
209+
"op->ob_refcnt >= 0",
210+
filename, lineno, __func__);
215211
}
216212

217213
#endif /* Py_REF_DEBUG */
@@ -356,13 +352,14 @@ PyObject_Print(PyObject *op, FILE *fp, int flags)
356352
Py_END_ALLOW_THREADS
357353
}
358354
else {
359-
if (op->ob_refcnt <= 0)
355+
if (op->ob_refcnt <= 0) {
360356
/* XXX(twouters) cast refcount to long until %zd is
361357
universally available */
362358
Py_BEGIN_ALLOW_THREADS
363359
fprintf(fp, "<refcnt %ld at %p>",
364360
(long)op->ob_refcnt, op);
365361
Py_END_ALLOW_THREADS
362+
}
366363
else {
367364
PyObject *s;
368365
if (flags & Py_PRINT_RAW)

0 commit comments

Comments
 (0)