Skip to content

Commit d879d3b

Browse files
committed
gh-143547: Fix PyErr_FormatUnraisable() fallback
Hold a strong reference to 'hook' while calling the default unraisable took to log hook failure. Fix test_sys.UnraisableHookTest: use the right decorator function to disable colors. Previously, tests were always skipped.
1 parent 6c9f7b4 commit d879d3b

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

Lib/test/test_sys.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ def test_disable_gil_abi(self):
13501350

13511351

13521352
@test.support.cpython_only
1353-
@force_not_colorized
1353+
@test.support.force_not_colorized_test_class
13541354
class UnraisableHookTest(unittest.TestCase):
13551355
def test_original_unraisablehook(self):
13561356
_testcapi = import_helper.import_module('_testcapi')
@@ -1492,6 +1492,7 @@ def hook_func(args):
14921492
def test_custom_unraisablehook_fail(self):
14931493
_testcapi = import_helper.import_module('_testcapi')
14941494
from _testcapi import err_writeunraisable
1495+
14951496
def hook_func(*args):
14961497
raise Exception("hook_func failed")
14971498

Python/errors.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,6 +1692,7 @@ format_unraisable_v(const char *format, va_list va, PyObject *obj)
16921692
}
16931693
}
16941694

1695+
PyObject *hook = NULL;
16951696
PyObject *hook_args = make_unraisable_hook_args(
16961697
tstate, exc_type, exc_value, exc_tb, err_msg, obj);
16971698
if (hook_args == NULL) {
@@ -1700,7 +1701,6 @@ format_unraisable_v(const char *format, va_list va, PyObject *obj)
17001701
goto error;
17011702
}
17021703

1703-
PyObject *hook;
17041704
if (PySys_GetOptionalAttr(&_Py_ID(unraisablehook), &hook) < 0) {
17051705
Py_DECREF(hook_args);
17061706
err_msg_str = NULL;
@@ -1727,7 +1727,6 @@ format_unraisable_v(const char *format, va_list va, PyObject *obj)
17271727
}
17281728

17291729
PyObject *res = PyObject_CallOneArg(hook, hook_args);
1730-
Py_DECREF(hook);
17311730
Py_DECREF(hook_args);
17321731
if (res != NULL) {
17331732
Py_DECREF(res);
@@ -1757,6 +1756,7 @@ format_unraisable_v(const char *format, va_list va, PyObject *obj)
17571756
Py_XDECREF(exc_value);
17581757
Py_XDECREF(exc_tb);
17591758
Py_XDECREF(err_msg);
1759+
Py_XDECREF(hook);
17601760
_PyErr_Clear(tstate); /* Just in case */
17611761
}
17621762

0 commit comments

Comments
 (0)