]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-35059: Enhance _PyObject_AssertFailed() (GH-10642)
authorVictor Stinner <vstinner@redhat.com>
Wed, 21 Nov 2018 22:53:44 +0000 (23:53 +0100)
committerGitHub <noreply@github.com>
Wed, 21 Nov 2018 22:53:44 +0000 (23:53 +0100)
Enhance _PyObject_AssertFailed()

* Exchange 'expr' and 'msg' parameters
* 'expr' and 'func' arguments can now be NULL

Include/object.h
Lib/test/test_capi.py
Objects/object.c

index 48ce9d2b970a5d131e898ef4ff505572736773d0..0d84d3604f0a13f121bc6e6b1472f3168730d8cd 100644 (file)
@@ -1158,8 +1158,8 @@ _PyObject_DebugTypeStats(FILE *out);
      ((expr)                                           \
       ? (void)(0)                                      \
       : _PyObject_AssertFailed((obj),                  \
-                               (msg),                  \
                                Py_STRINGIFY(expr),     \
+                               (msg),                  \
                                __FILE__,               \
                                __LINE__,               \
                                __func__))
@@ -1169,11 +1169,13 @@ _PyObject_DebugTypeStats(FILE *out);
 
 /* Declare and define _PyObject_AssertFailed() even when NDEBUG is defined,
    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(
     PyObject *obj,
-    const char *msg,
     const char *expr,
+    const char *msg,
     const char *file,
     int line,
     const char *function);
index 3c8c3f02bf7874eb4f09221ad371bcd81d385094..7c68b2c0fc2c20ca93e4002557a3ef806e4fe7f4 100644 (file)
@@ -330,7 +330,7 @@ class CAPITest(unittest.TestCase):
         rc, out, err = assert_python_failure('-c', code)
         self.assertRegex(err,
                          br'_testcapimodule\.c:[0-9]+: '
-                         br'_Py_NegativeRefcount: Assertion ".*" failed; '
+                         br'_Py_NegativeRefcount: Assertion failed: '
                          br'object has negative ref count')
 
 
index 801b205c9b6bd0928056d5ba153e1aa1edc48190..9d2614bb6d110ac450193bbb2c45ca2df729f3d3 100644 (file)
@@ -205,8 +205,7 @@ void _Py_dec_count(PyTypeObject *tp)
 void
 _Py_NegativeRefcount(const char *filename, int lineno, PyObject *op)
 {
-    _PyObject_AssertFailed(op, "object has negative ref count",
-                           "op->ob_refcnt >= 0",
+    _PyObject_AssertFailed(op, NULL, "object has negative ref count",
                            filename, lineno, __func__);
 }
 
@@ -2219,20 +2218,25 @@ _PyTrash_thread_destroy_chain(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)
 {
-    fprintf(stderr,
-            "%s:%d: %s: Assertion \"%s\" failed",
-            file, line, function, expr);
+    fprintf(stderr, "%s:%d: ", file, line);
+    if (function) {
+        fprintf(stderr, "%s: ", function);
+    }
     fflush(stderr);
-
-    if (msg) {
-        fprintf(stderr, "; %s.\n", msg);
+    if (expr) {
+        fprintf(stderr, "Assertion \"%s\" failed", expr);
     }
     else {
-        fprintf(stderr, ".\n");
+        fprintf(stderr, "Assertion failed");
+    }
+    fflush(stderr);
+    if (msg) {
+        fprintf(stderr, ": %s", msg);
     }
+    fprintf(stderr, "\n");
     fflush(stderr);
 
     if (obj == NULL) {