8000 bpo-35059: Enhance _PyObject_AssertFailed() (GH-10642) · python/cpython@f1d002c · GitHub
[go: up one dir, main page]

Skip to content

Commit f1d002c

Browse files
authored
bpo-35059: Enhance _PyObject_AssertFailed() (GH-10642)
Enhance _PyObject_AssertFailed() * Exchange 'expr' and 'msg' parameters * 'expr' and 'func' arguments can now be NULL
1 parent bcda8f1 commit f1d002c

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

Include/object.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,8 +1158,8 @@ _PyObject_DebugTypeStats(FILE *out);
11581158
((expr) \
11591159
? (void)(0) \
11601160
: _PyObject_AssertFailed((obj), \
1161-
(msg), \
11621161
Py_STRINGIFY(expr), \
1162+
(msg), \
11631163
__FILE__, \
11641164
__LINE__, \
11651165
__func__))
@@ -1169,11 +1169,13 @@ _PyObject_DebugTypeStats(FILE *out);
11691169

11701170
/* Declare and define _PyObject_AssertFailed() even when NDEBUG is defined,
11711171
to avoid causing compiler/linker errors when building extensions without
1172-
NDEBUG against a Python built with NDEBUG defined. */
1172+
NDEBUG against a Python built with NDEBUG defined.
1173+
1174+
msg, expr and function can be NULL. */
11731175
PyAPI_FUNC(void) _PyObject_AssertFailed(
11741176
PyObject *obj,
1175-
const char *msg,
11761177
const char *expr,
1178+
const char *msg,
11771179
const char *file,
11781180
int line,
11791181
const char *function);

Lib/test/test_capi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ def test_negative_refcount(self):
330330
rc, out, err = assert_python_failure('-c', code)
331331
self.assertRegex(err,
332332
br'_testcapimodule\.c:[0-9]+: '
333-
br'_Py_NegativeRefcount: Assertion ".*" failed; '
333+
br'_Py_NegativeRefcount: Assertion failed: '
334334
br'object has negative ref count')
335335

336336

Objects/object.c

Lines changed: 14 additions & 10 deletions
< 10720 tr class="diff-line-row">
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ void _Py_dec_count(PyTypeObject *tp)
205205
void
206206
_Py_NegativeRefcount(const char *filename, int lineno, PyObject *op)
207207
{
208-
_PyObject_AssertFailed(op, "object has negative ref count",
209-
"op->ob_refcnt >= 0",
208+
_PyObject_AssertFailed(op, NULL, "object has negative ref count",
210209
filename, lineno, __func__);
211210
}
212211

@@ -2219,20 +2218,25 @@ _PyTrash_thread_destroy_chain(void)
22192218

22202219

22212220
void
2222-
_PyObject_AssertFailed(PyObject *obj, const char *msg, const char *expr,
2221+
_PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
22232222
const char *file, int line, const char *function)
22242223
{
2225-
fprintf(stderr,
2226-
"%s:%d: %s: Assertion \"%s\" failed",
2227-
file, line, function, expr);
2224+
fprintf(stderr, "%s:%d: ", file, line);
2225+
if (function) {
2226+
fprintf(stderr, "%s: ", function);
2227+
}
22282228
fflush(stderr);
2229-
2230-
if (msg) {
2231-
fprintf(stderr, "; %s.\n", msg);
2229+
if (expr) {
2230+
fprintf(stderr, "Assertion \"%s\" failed", expr);
22322231
}
22332232
else {
2234-
fprintf(stderr, ".\n");
2233+
fprintf(stderr, "Assertion failed");
2234+
}
2235+
fflush(stderr);
2236+
if (msg) {
2237+
fprintf(stderr, ": %s", msg);
22352238
}
2239+
fprintf(stderr, "\n");
22362240
fflush(stderr);
22372241

22382242
if (obj == NULL) {

0 commit comments

Comments
 (0)
0