From 2dc874b60a58c5009b36ce61831cfe79563e6574 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 26 Oct 2018 00:35:49 +0200 Subject: [PATCH 1/2] bpo-9263: _Py_NegativeRefcount() use _PyObject_AssertFailed() _Py_NegativeRefcount() now uses _PyObject_AssertFailed() to dump the object to help debugging. --- Lib/test/test_capi.py | 5 +++-- Objects/object.c | 13 +++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py index a732f4f82f31d6..b3600ebe993de5 100644 --- a/Lib/test/test_capi.py +++ b/Lib/test/test_capi.py @@ -329,8 +329,9 @@ def test_negative_refcount(self): """) rc, out, err = assert_python_failure('-c', code) self.assertRegex(err, - br'_testcapimodule\.c:[0-9]+ object at .* ' - br'has negative ref count', err) + br'_testcapimodule\.c:[0-9]+: ' + br'_Py_NegativeRefcount: Assertion ".*" failed; ' + br'object has negative ref count') class TestPendingCalls(unittest.TestCase): diff --git a/Objects/object.c b/Objects/object.c index 2252f98347566f..4311e5d965f772 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -205,13 +205,9 @@ void dec_count(PyTypeObject *tp) void _Py_NegativeRefcount(const char *filename, int lineno, PyObject *op) { - char buf[300]; - - PyOS_snprintf(buf, sizeof(buf), - "%s:%i object at %p has negative ref count " - "%" PY_FORMAT_SIZE_T "d", - filename, lineno, op, op->ob_refcnt); - Py_FatalError(buf); + _PyObject_AssertFailed(op, "object has negative ref count", + "op->ob_refcnt >= 0", + filename, lineno, __PRETTY_FUNCTION__); } #endif /* Py_REF_DEBUG */ @@ -356,13 +352,14 @@ PyObject_Print(PyObject *op, FILE *fp, int flags) Py_END_ALLOW_THREADS } else { - if (op->ob_refcnt <= 0) + if (op->ob_refcnt <= 0) { /* XXX(twouters) cast refcount to long until %zd is universally available */ Py_BEGIN_ALLOW_THREADS fprintf(fp, "", (long)op->ob_refcnt, op); Py_END_ALLOW_THREADS + } else { PyObject *s; if (flags & Py_PRINT_RAW) From b2b4ff47a93229afd726c03dc9781978045bbef8 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 26 Oct 2018 01:42:56 +0200 Subject: [PATCH 2/2] Replace __PRETTY_FUNCTION__ with __func__ --- Objects/object.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/object.c b/Objects/object.c index 4311e5d965f772..d6f27ff9487fd6 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -207,7 +207,7 @@ _Py_NegativeRefcount(const char *filename, int lineno, PyObject *op) { _PyObject_AssertFailed(op, "object has negative ref count", "op->ob_refcnt >= 0", - filename, lineno, __PRETTY_FUNCTION__); + filename, lineno, __func__); } #endif /* Py_REF_DEBUG */