Skip to content

Commit 0068e9c

Browse files
committed
refactor common code into a new _PyErr_FormatNote function
1 parent beaf88a commit 0068e9c

File tree

4 files changed

+33
-40
lines changed

4 files changed

+33
-40
lines changed

Include/internal/pycore_pyerrors.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ extern PyObject* _Py_Offer_Suggestions(PyObject* exception);
109109
PyAPI_FUNC(Py_ssize_t) _Py_UTF8_Edit_Cost(PyObject *str_a, PyObject *str_b,
110110
Py_ssize_t max_cost);
111111

112+
void _PyErr_FormatNote(const char *format, ...);
113+
112114
#ifdef __cplusplus
113115
}
114116
#endif

Objects/typeobject.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9137,24 +9137,10 @@ type_new_set_names(PyTypeObject *type)
91379137
Py_DECREF(set_name);
91389138

91399139
if (res == NULL) {
9140-
/* Add note to the exception */
9141-
PyObject *exc = PyErr_GetRaisedException();
9142-
assert(exc != NULL);
9143-
PyObject *note = PyUnicode_FromFormat(
9140+
_PyErr_FormatNote(
91449141
"Error calling __set_name__ on '%.100s' instance %R "
91459142
"in '%.100s'",
91469143
Py_TYPE(value)->tp_name, key, type->tp_name);
9147-
9148-
if (note) {
9149-
int res = _PyException_AddNote(exc, note);
9150-
Py_DECREF(note);
9151-
if (res == 0) {
9152-
PyErr_SetRaisedException(exc);
9153-
goto error;
9154-
}
9155-
}
9156-
_PyErr_ChainExceptions1(exc);
9157-
goto error;
91589144
}
91599145
else {
91609146
Py_DECREF(res);

Python/codecs.c

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Copyright (c) Corporation for National Research Initiatives.
1111
#include "Python.h"
1212
#include "pycore_call.h" // _PyObject_CallNoArgs()
1313
#include "pycore_interp.h" // PyInterpreterState.codec_search_path
14+
#include "pycore_pyerrors.h" // _PyErr_FormatNote()
1415
#include "pycore_pystate.h" // _PyInterpreterState_GET()
1516
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI
1617
#include <ctype.h>
@@ -382,29 +383,6 @@ PyObject *PyCodec_StreamWriter(const char *encoding,
382383
return codec_getstreamcodec(encoding, stream, errors, 3);
383384
}
384385

385-
static void
386-
add_note_to_codec_error(const char *operation,
387-
const char *encoding)
388-
{
389-
PyObject *exc = PyErr_GetRaisedException();
390-
if (exc == NULL) {
391-
return;
392-
}
393-
PyObject *note = PyUnicode_FromFormat("%s with '%s' codec failed",
394-
operation, encoding);
395-
if (note == NULL) {
396-
_PyErr_ChainExceptions1(exc);
397-
return;
398-
}
399-
int res = _PyException_AddNote(exc, note);
400-
Py_DECREF(note);
401-
if (res < 0) {
402-
_PyErr_ChainExceptions1(exc);
403-
return;
404-
}
405-
PyErr_SetRaisedException(exc);
406-
}
407-
408386
/* Encode an object (e.g. a Unicode object) using the given encoding
409387
and return the resulting encoded object (usually a Python string).
410388
@@ -425,7 +403,7 @@ _PyCodec_EncodeInternal(PyObject *object,
425403

426404
result = PyObject_Call(encoder, args, NULL);
427405
if (result == NULL) {
428-
add_note_to_codec_error("encoding", encoding);
406+
_PyErr_FormatNote("%s with '%s' codec failed", "encoding", encoding);
429407
goto onError;
430408
}
431409

@@ -470,7 +448,7 @@ _PyCodec_DecodeInternal(PyObject *object,
470448

471449
result = PyObject_Call(decoder, args, NULL);
472450
if (result == NULL) {
473-
add_note_to_codec_error("decoding", encoding);
451+
_PyErr_FormatNote("%s with '%s' codec failed", "decoding", encoding);
474452
goto onError;
475453
}
476454
if (!PyTuple_Check(result) ||

Python/errors.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,33 @@ PyErr_Format(PyObject *exception, const char *format, ...)
12001200
}
12011201

12021202

1203+
/* Adds a note to the current exception (if any) */
1204+
void
1205+
_PyErr_FormatNote(const char *format, ...)
1206+
{
1207+
PyObject *exc = PyErr_GetRaisedException();
1208+
if (exc == NULL) {
1209+
return;
1210+
}
1211+
va_list vargs;
1212+
va_start(vargs, format);
1213+
PyObject *note = PyUnicode_FromFormatV(format, vargs);
1214+
va_end(vargs);
1215+
if (note == NULL) {
1216+
goto error;
1217+
}
1218+
int res = _PyException_AddNote(exc, note);
1219+
Py_DECREF(note);
1220+
if (res < 0) {
1221+
goto error;
1222+
}
1223+
PyErr_SetRaisedException(exc);
1224+
return;
1225+
error:
1226+
_PyErr_ChainExceptions1(exc);
1227+
}
1228+
1229+
12031230
PyObject *
12041231
PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
12051232
{

0 commit comments

Comments
 (0)