summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-11-02 09:16:34 (GMT)
committerGitHub <noreply@github.com>2023-11-02 09:16:34 (GMT)
commit970e719a7a829bddc647bbaa668dd8603abdddef (patch)
tree92eeeba01687f6e02482315db653ce57f3b10e4b /Objects
parenta12f624a9dc1c44bb20a20b13fd164c14b987892 (diff)
downloadcpython-970e719a7a829bddc647bbaa668dd8603abdddef.zip
cpython-970e719a7a829bddc647bbaa668dd8603abdddef.tar.gz
cpython-970e719a7a829bddc647bbaa668dd8603abdddef.tar.bz2
gh-108082: Use PyErr_FormatUnraisable() (GH-111580)
Replace most of calls of _PyErr_WriteUnraisableMsg() and some calls of PyErr_WriteUnraisable(NULL) with PyErr_FormatUnraisable(). Co-authored-by: Victor Stinner <vstinner@python.org>
Diffstat (limited to 'Objects')
-rw-r--r--Objects/codeobject.c20
-rw-r--r--Objects/dictobject.c9
-rw-r--r--Objects/funcobject.c20
-rw-r--r--Objects/moduleobject.c11
-rw-r--r--Objects/typeobject.c10
5 files changed, 19 insertions, 51 deletions
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index f662b8e..48ff5b8 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -12,8 +12,6 @@
#include "pycore_tuple.h" // _PyTuple_ITEMS()
#include "clinic/codeobject.c.h"
-static PyObject* code_repr(PyCodeObject *co);
-
static const char *
code_event_name(PyCodeEvent event) {
switch (event) {
@@ -41,21 +39,9 @@ notify_code_watchers(PyCodeEvent event, PyCodeObject *co)
// callback must be non-null if the watcher bit is set
assert(cb != NULL);
if (cb(event, co) < 0) {
- // Don't risk resurrecting the object if an unraisablehook keeps
- // a reference; pass a string as context.
- PyObject *context = NULL;
- PyObject *repr = code_repr(co);
- if (repr) {
- context = PyUnicode_FromFormat(
- "%s watcher callback for %U",
- code_event_name(event), repr);
- Py_DECREF(repr);
- }
- if (context == NULL) {
- context = Py_NewRef(Py_None);
- }
- PyErr_WriteUnraisable(context);
- Py_DECREF(context);
+ PyErr_FormatUnraisable(
+ "Exception ignored in %s watcher callback for %R",
+ code_event_name(event), co);
}
}
i++;
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 361f8e9..5aa2c0d 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -5912,14 +5912,9 @@ _PyDict_SendEvent(int watcher_bits,
// unraisablehook keep a reference to it, so we don't pass the
// dict as context, just an informative string message. Dict
// repr can call arbitrary code, so we invent a simpler version.
- PyObject *context = PyUnicode_FromFormat(
- "%s watcher callback for <dict at %p>",
+ PyErr_FormatUnraisable(
+ "Exception ignored in %s watcher callback for <dict at %p>",
dict_event_name(event), mp);
- if (context == NULL) {
- context = Py_NewRef(Py_None);
- }
- PyErr_WriteUnraisable(context);
- Py_DECREF(context);
}
}
watcher_bits >>= 1;
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index 56c5af6..8ce1bff 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -8,8 +8,6 @@
#include "pycore_pyerrors.h" // _PyErr_Occurred()
-static PyObject* func_repr(PyFunctionObject *op);
-
static const char *
func_event_name(PyFunction_WatchEvent event) {
switch (event) {
@@ -35,21 +33,9 @@ notify_func_watchers(PyInterpreterState *interp, PyFunction_WatchEvent event,
// callback must be non-null if the watcher bit is set
assert(cb != NULL);
if (cb(event, func, new_value) < 0) {
- // Don't risk resurrecting the func if an unraisablehook keeps a
- // reference; pass a string as context.
- PyObject *context = NULL;
- PyObject *repr = func_repr(func);
- if (repr != NULL) {
- context = PyUnicode_FromFormat(
- "%s watcher callback for %U",
- func_event_name(event), repr);
- Py_DECREF(repr);
- }
- if (context == NULL) {
- context = Py_NewRef(Py_None);
- }
- PyErr_WriteUnraisable(context);
- Py_DECREF(context);
+ PyErr_FormatUnraisable(
+ "Exception ignored in %s watcher callback for function %U at %p",
+ func_event_name(event), func->func_qualname, func);
}
}
i++;
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index 5b9f74d..a3d55b7 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -647,7 +647,7 @@ _PyModule_ClearDict(PyObject *d)
PyErr_Clear();
}
if (PyDict_SetItem(d, key, Py_None) != 0) {
- PyErr_WriteUnraisable(NULL);
+ PyErr_FormatUnraisable("Exception ignored on clearing module dict");
}
}
}
@@ -668,7 +668,7 @@ _PyModule_ClearDict(PyObject *d)
PyErr_Clear();
}
if (PyDict_SetItem(d, key, Py_None) != 0) {
- PyErr_WriteUnraisable(NULL);
+ PyErr_FormatUnraisable("Exception ignored on clearing module dict");
}
}
}
@@ -902,10 +902,9 @@ module_clear(PyModuleObject *m)
{
int res = m->md_def->m_clear((PyObject*)m);
if (PyErr_Occurred()) {
- PySys_FormatStderr("Exception ignored in m_clear of module%s%V\n",
- m->md_name ? " " : "",
- m->md_name, "");
- PyErr_WriteUnraisable(NULL);
+ PyErr_FormatUnraisable("Exception ignored in m_clear of module%s%V",
+ m->md_name ? " " : "",
+ m->md_name, "");
}
if (res)
return res;
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 2508569..f44e30c 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -828,7 +828,9 @@ PyType_Modified(PyTypeObject *type)
if (bits & 1) {
PyType_WatchCallback cb = interp->type_watchers[i];
if (cb && (cb(type) < 0)) {
- PyErr_WriteUnraisable((PyObject *)type);
+ PyErr_FormatUnraisable(
+ "Exception ignored in type watcher callback #%d for %R",
+ i, type);
}
}
i++;
@@ -9291,7 +9293,7 @@ releasebuffer_call_python(PyObject *self, Py_buffer *buffer)
// from a Python __buffer__ function.
mv = PyMemoryView_FromBuffer(buffer);
if (mv == NULL) {
- PyErr_WriteUnraisable(self);
+ PyErr_FormatUnraisable("Exception ignored in bf_releasebuffer of %s", Py_TYPE(self)->tp_name);
goto end;
}
// Set the memoryview to restricted mode, which forbids
@@ -9304,7 +9306,7 @@ releasebuffer_call_python(PyObject *self, Py_buffer *buffer)
PyObject *stack[2] = {self, mv};
PyObject *ret = vectorcall_method(&_Py_ID(__release_buffer__), stack, 2);
if (ret == NULL) {
- PyErr_WriteUnraisable(self);
+ PyErr_FormatUnraisable("Exception ignored in __release_buffer__ of %s", Py_TYPE(self)->tp_name);
}
else {
Py_DECREF(ret);
@@ -9312,7 +9314,7 @@ releasebuffer_call_python(PyObject *self, Py_buffer *buffer)
if (!is_buffer_wrapper) {
PyObject *res = PyObject_CallMethodNoArgs(mv, &_Py_ID(release));
if (res == NULL) {
- PyErr_WriteUnraisable(self);
+ PyErr_FormatUnraisable("Exception ignored in bf_releasebuffer of %s", Py_TYPE(self)->tp_name);
}
else {
Py_DECREF(res);