Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
  • Loading branch information
vstinner committed Jan 8, 2026
commit d879d3b108515aeb584103034a7442d1d1e7afbe
3 changes: 2 additions & 1 deletion Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,7 @@ def test_disable_gil_abi(self):


@test.support.cpython_only
@force_not_colorized
@test.support.force_not_colorized_test_class
class UnraisableHookTest(unittest.TestCase):
def test_original_unraisablehook(self):
_testcapi = import_helper.import_module('_testcapi')
Expand Down Expand Up @@ -1492,6 +1492,7 @@ def hook_func(args):
def test_custom_unraisablehook_fail(self):
_testcapi = import_helper.import_module('_testcapi')
from _testcapi import err_writeunraisable

Comment thread
aisk marked this conversation as resolved.
def hook_func(*args):
raise Exception("hook_func failed")

Expand Down
4 changes: 2 additions & 2 deletions Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,7 @@ format_unraisable_v(const char *format, va_list va, PyObject *obj)
}
}

PyObject *hook = NULL;
PyObject *hook_args = make_unraisable_hook_args(
tstate, exc_type, exc_value, exc_tb, err_msg, obj);
if (hook_args == NULL) {
Expand All @@ -1700,7 +1701,6 @@ format_unraisable_v(const char *format, va_list va, PyObject *obj)
goto error;
}

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

PyObject *res = PyObject_CallOneArg(hook, hook_args);
Py_DECREF(hook);
Py_DECREF(hook_args);
if (res != NULL) {
Py_DECREF(res);
Expand Down Expand Up @@ -1757,6 +1756,7 @@ format_unraisable_v(const char *format, va_list va, PyObject *obj)
Py_XDECREF(exc_value);
Py_XDECREF(exc_tb);
Py_XDECREF(err_msg);
Py_XDECREF(hook);
_PyErr_Clear(tstate); /* Just in case */
}

Expand Down
Loading