Commit f1d002c1 authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

bpo-35059: Enhance _PyObject_AssertFailed() (GH-10642)

Enhance _PyObject_AssertFailed()

* Exchange 'expr' and 'msg' parameters
* 'expr' and 'func' arguments can now be NULL
parent bcda8f1d
...@@ -1158,8 +1158,8 @@ _PyObject_DebugTypeStats(FILE *out); ...@@ -1158,8 +1158,8 @@ _PyObject_DebugTypeStats(FILE *out);
((expr) \ ((expr) \
? (void)(0) \ ? (void)(0) \
: _PyObject_AssertFailed((obj), \ : _PyObject_AssertFailed((obj), \
(msg), \
Py_STRINGIFY(expr), \ Py_STRINGIFY(expr), \
(msg), \
__FILE__, \ __FILE__, \
__LINE__, \ __LINE__, \
__func__)) __func__))
...@@ -1169,11 +1169,13 @@ _PyObject_DebugTypeStats(FILE *out); ...@@ -1169,11 +1169,13 @@ _PyObject_DebugTypeStats(FILE *out);
/* Declare and define _PyObject_AssertFailed() even when NDEBUG is defined, /* Declare and define _PyObject_AssertFailed() even when NDEBUG is defined,
to avoid causing compiler/linker errors when building extensions without to avoid causing compiler/linker errors when building extensions without
NDEBUG against a Python built with NDEBUG defined. */ NDEBUG against a Python built with NDEBUG defined.
msg, expr and function can be NULL. */
PyAPI_FUNC(void) _PyObject_AssertFailed( PyAPI_FUNC(void) _PyObject_AssertFailed(
PyObject *obj, PyObject *obj,
const char *msg,
const char *expr, const char *expr,
const char *msg,
const char *file, const char *file,
int line, int line,
const char *function); const char *function);
......
...@@ -330,7 +330,7 @@ class CAPITest(unittest.TestCase): ...@@ -330,7 +330,7 @@ class CAPITest(unittest.TestCase):
rc, out, err = assert_python_failure('-c', code) rc, out, err = assert_python_failure('-c', code)
self.assertRegex(err, self.assertRegex(err,
br'_testcapimodule\.c:[0-9]+: ' br'_testcapimodule\.c:[0-9]+: '
br'_Py_NegativeRefcount: Assertion ".*" failed; ' br'_Py_NegativeRefcount: Assertion failed: '
br'object has negative ref count') br'object has negative ref count')
......
...@@ -205,8 +205,7 @@ void _Py_dec_count(PyTypeObject *tp) ...@@ -205,8 +205,7 @@ void _Py_dec_count(PyTypeObject *tp)
void void
_Py_NegativeRefcount(const char *filename, int lineno, PyObject *op) _Py_NegativeRefcount(const char *filename, int lineno, PyObject *op)
{ {
_PyObject_AssertFailed(op, "object has negative ref count", _PyObject_AssertFailed(op, NULL, "object has negative ref count",
"op->ob_refcnt >= 0",
filename, lineno, __func__); filename, lineno, __func__);
} }
...@@ -2219,20 +2218,25 @@ _PyTrash_thread_destroy_chain(void) ...@@ -2219,20 +2218,25 @@ _PyTrash_thread_destroy_chain(void)
void void
_PyObject_AssertFailed(PyObject *obj, const char *msg, const char *expr, _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
const char *file, int line, const char *function) const char *file, int line, const char *function)
{ {
fprintf(stderr, fprintf(stderr, "%s:%d: ", file, line);
"%s:%d: %s: Assertion \"%s\" failed", if (function) {
file, line, function, expr); fprintf(stderr, "%s: ", function);
}
fflush(stderr); fflush(stderr);
if (expr) {
if (msg) { fprintf(stderr, "Assertion \"%s\" failed", expr);
fprintf(stderr, "; %s.\n", msg);
} }
else { else {
fprintf(stderr, ".\n"); fprintf(stderr, "Assertion failed");
}
fflush(stderr);
if (msg) {
fprintf(stderr, ": %s", msg);
} }
fprintf(stderr, "\n");
fflush(stderr); fflush(stderr);
if (obj == NULL) { if (obj == NULL) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment