Skip to content

Commit 4333933

Browse files
committed
apply offline comments from Guido
1 parent 827a48c commit 4333933

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

Include/cpython/pyerrors.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ PyAPI_FUNC(PyObject *) _PyErr_FormatFromCause(
112112

113113
/* In exceptions.c */
114114

115-
PyAPI_FUNC(PyObject*) _PyException_AddNote(
116-
PyBaseExceptionObject *exc,
115+
PyAPI_FUNC(int) _PyException_AddNote(
116+
PyObject *exc,
117117
PyObject *note);
118118

119119
/* Helper that attempts to replace the current exception with one of the

Lib/test/test_capi/test_exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class Broken(Exception):
174174
def __init__(self, *arg):
175175
raise ValueError("Broken __init__")
176176

177-
exc = _testcapi.exc_set_object_fetch(Broken, ('abcd'))
177+
exc = _testcapi.exc_set_object_fetch(Broken, 'abcd')
178178
self.assertIsInstance(exc, ValueError)
179179
self.assertEqual(exc.__notes__[0],
180180
"Normalization failed: type=Broken args='abcd'")
@@ -183,7 +183,7 @@ class BadArg:
183183
def __repr__(self):
184184
raise TypeError('Broken arg type')
185185

186-
exc = _testcapi.exc_set_object_fetch(Broken, (BadArg()))
186+
exc = _testcapi.exc_set_object_fetch(Broken, BadArg())
187187
self.assertIsInstance(exc, ValueError)
188188
self.assertEqual(exc.__notes__[0],
189189
'Normalization failed: type=Broken args=<unknown>')

Objects/exceptions.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3749,10 +3749,19 @@ _PyExc_Fini(PyInterpreterState *interp)
37493749
_PyExc_FiniTypes(interp);
37503750
}
37513751

3752-
PyObject *
3753-
_PyException_AddNote(PyBaseExceptionObject *exc, PyObject *note)
3752+
int
3753+
_PyException_AddNote(PyObject *exc, PyObject *note)
37543754
{
3755-
return BaseException_add_note((PyObject *)exc, note);
3755+
if (!PyExceptionInstance_Check(exc)) {
3756+
PyErr_Format(PyExc_TypeError,
3757+
"exc must be an exception, not '%s'",
3758+
Py_TYPE(exc)->tp_name);
3759+
return -1;
3760+
}
3761+
PyObject *r = BaseException_add_note(exc, note);
3762+
int res = r == NULL ? -1 : 0;
3763+
Py_XDECREF(r);
3764+
return res;
37563765
}
37573766

37583767
/* Helper to do the equivalent of "raise X from Y" in C, but always using

Python/errors.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,28 +182,27 @@ _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value)
182182
Py_XINCREF(value);
183183
if (!is_subclass) {
184184
/* We must normalize the value right now */
185-
PyObject *fixed_value;
186185

187186
/* Issue #23571: functions must not be called with an
188187
exception set */
189188
_PyErr_Clear(tstate);
190189

191-
fixed_value = _PyErr_CreateException(exception, value);
192-
Py_XDECREF(value);
190+
PyObject *fixed_value = _PyErr_CreateException(exception, value);
193191
if (fixed_value == NULL) {
194192
PyObject *exc = _PyErr_GetRaisedException(tstate);
195193
assert(PyExceptionInstance_Check(exc));
196194

197195
PyObject *note = get_normalization_failure_note(tstate, exception, value);
196+
Py_XDECREF(value);
198197
if (note != NULL) {
199-
PyObject *res = _PyException_AddNote((PyBaseExceptionObject*)exc, note);
198+
/* ignore errors in _PyException_AddNote - they will be overwritten below */
199+
_PyException_AddNote(exc, note);
200200
Py_DECREF(note);
201-
Py_XDECREF(res);
202201
}
203202
_PyErr_SetRaisedException(tstate, exc);
204203
return;
205204
}
206-
205+
Py_XDECREF(value);
207206
value = fixed_value;
208207
}
209208

0 commit comments

Comments
 (0)