Skip to content

Commit b5256e7

Browse files
committed
Try to handle errors in context string building
1 parent 8e8843e commit b5256e7

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

Objects/codeobject.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,18 @@ notify_code_watchers(PyCodeEvent event, PyCodeObject *co)
3030
if (cb(event, co) < 0) {
3131
// Don't risk resurrecting the object if an unraisablehook keeps
3232
// a reference; pass a string as context.
33+
PyObject *context = NULL;
3334
PyObject *repr = code_repr(co);
34-
PyObject *context = PyUnicode_FromFormat("watcher callback for %U", repr);
35+
if (repr) {
36+
context = PyUnicode_FromFormat("watcher callback for %U", repr);
37+
Py_DECREF(repr);
38+
}
39+
if (context == NULL) {
40+
context = Py_None;
41+
Py_INCREF(context);
42+
}
3543
PyErr_WriteUnraisable(context);
3644
Py_DECREF(context);
37-
Py_DECREF(repr);
3845
}
3946
}
4047
i++;

Objects/dictobject.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5756,6 +5756,10 @@ _PyDict_SendEvent(int watcher_bits,
57565756
// dict as context, just an informative string message. Dict
57575757
// repr can call arbitrary code, so we invent a simpler version.
57585758
PyObject *context = PyUnicode_FromFormat("watcher callback for <dict at %p>", mp);
5759+
if (context == NULL) {
5760+
context = Py_None;
5761+
Py_INCREF(context);
5762+
}
57595763
PyErr_WriteUnraisable(context);
57605764
Py_DECREF(context);
57615765
}

Objects/funcobject.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,18 @@ notify_func_watchers(PyInterpreterState *interp, PyFunction_WatchEvent event,
2525
if (cb(event, func, new_value) < 0) {
2626
// Don't risk resurrecting the func if an unraisablehook keeps a
2727
// reference; pass a string as context.
28+
PyObject *context = NULL;
2829
PyObject *repr = func_repr(func);
29-
PyObject *context = PyUnicode_FromFormat("watcher callback for %U", repr);
30+
if (repr != NULL) {
31+
context = PyUnicode_FromFormat("watcher callback for %U", repr);
32+
Py_DECREF(repr);
33+
}
34+
if (context == NULL) {
35+
context = Py_None;
36+
Py_INCREF(context);
37+
}
3038
PyErr_WriteUnraisable(context);
3139
Py_DECREF(context);
32-
Py_DECREF(repr);
3340
}
3441
}
3542
i++;

0 commit comments

Comments
 (0)