Skip to content

Commit

Permalink
Fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed Nov 1, 2023
1 parent 3a60137 commit 5f3b94a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
20 changes: 14 additions & 6 deletions Lib/test/test_capi/test_watchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ def test_error(self):
with catch_unraisable_exception() as cm:
d["foo"] = "bar"
self.assertIn(
"PyDict_EVENT_ADDED watcher callback for <dict at",
cm.unraisable.object
"Exception ignored in "
"PyDict_EVENT_ADDED watcher callback for <dict at ",
cm.unraisable.err_msg
)
self.assertIsNone(cm.unraisable.object)
self.assertEqual(str(cm.unraisable.exc_value), "boom!")
self.assert_events([])

Expand Down Expand Up @@ -278,7 +280,9 @@ class C: pass
self.watch(wid, C)
with catch_unraisable_exception() as cm:
C.foo = "bar"
self.assertIs(cm.unraisable.object, C)
self.assertEqual(cm.unraisable.err_msg,
f"Exception ignored in watcher callback #0 for {C!r}")
self.assertIs(cm.unraisable.object, None)
self.assertEqual(str(cm.unraisable.exc_value), "boom!")
self.assert_events([])

Expand Down Expand Up @@ -416,9 +420,11 @@ def test_error(self):
co = _testcapi.code_newempty("test_watchers", "dummy0", 0)

self.assertEqual(
cm.unraisable.object,
cm.unraisable.err_msg,
f"Exception ignored in "
f"PY_CODE_EVENT_CREATE watcher callback for {co!r}"
)
self.assertIsNone(cm.unraisable.object)
self.assertEqual(str(cm.unraisable.exc_value), "boom!")

def test_dealloc_error(self):
Expand Down Expand Up @@ -520,9 +526,11 @@ def myfunc():
pass

self.assertEqual(
cm.unraisable.object,
f"PyFunction_EVENT_CREATE watcher callback for {myfunc!r}"
cm.unraisable.err_msg,
f"Exception ignored in "
f"PyFunction_EVENT_CREATE watcher callback for {repr(myfunc)[1:-1]}"
)
self.assertIsNone(cm.unraisable.object)

def test_dealloc_watcher_raises_error(self):
class MyError(Exception):
Expand Down
2 changes: 1 addition & 1 deletion Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ delete_garbage(PyThreadState *tstate, GCState *gcstate,
Py_INCREF(op);
(void) clear(op);
if (_PyErr_Occurred(tstate)) {
PyErr_FormatUnraisable("Exception ignored in tp_clear of %.200s",
PyErr_FormatUnraisable("Exception ignored in tp_clear of %s",
Py_TYPE(op)->tp_name);
}
Py_DECREF(op);
Expand Down
2 changes: 1 addition & 1 deletion Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ module_clear(PyModuleObject *m)
{
int res = m->md_def->m_clear((PyObject*)m);
if (PyErr_Occurred()) {
PyErr_FormatUnraisable("Exception ignored in m_clear of module%s%V\n",
PyErr_FormatUnraisable("Exception ignored in m_clear of module%s%V",
m->md_name ? " " : "",
m->md_name, "");
}
Expand Down
4 changes: 3 additions & 1 deletion Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,9 @@ PyType_Modified(PyTypeObject *type)
if (bits & 1) {
PyType_WatchCallback cb = interp->type_watchers[i];
if (cb && (cb(type) < 0)) {
PyErr_FormatUnraisable("Exception ignored in watcher callback for %R", i, type);
PyErr_FormatUnraisable(
"Exception ignored in watcher callback #%d for %R",
i, type);
}
}
i++;
Expand Down

0 comments on commit 5f3b94a

Please sign in to comment.