summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2023-04-11 10:53:06 (GMT)
committerGitHub <noreply@github.com>2023-04-11 10:53:06 (GMT)
commit55c99d97e14618dfce41472dd4446f763b0da13f (patch)
tree457a036aa57a59204105937c6cba43f3ce6ba288 /Python
parente071f00aaefae9eccf787d5c50396c26c8616483 (diff)
downloadcpython-55c99d97e14618dfce41472dd4446f763b0da13f.zip
cpython-55c99d97e14618dfce41472dd4446f763b0da13f.tar.gz
cpython-55c99d97e14618dfce41472dd4446f763b0da13f.tar.bz2
gh-77757: replace exception wrapping by PEP-678 notes in typeobject's __set_name__ (#103402)
Diffstat (limited to 'Python')
-rw-r--r--Python/codecs.c28
-rw-r--r--Python/errors.c27
2 files changed, 30 insertions, 25 deletions
diff --git a/Python/codecs.c b/Python/codecs.c
index 9d800f9..1983f56 100644
--- a/Python/codecs.c
+++ b/Python/codecs.c
@@ -11,6 +11,7 @@ Copyright (c) Corporation for National Research Initiatives.
#include "Python.h"
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_interp.h" // PyInterpreterState.codec_search_path
+#include "pycore_pyerrors.h" // _PyErr_FormatNote()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI
#include <ctype.h>
@@ -382,29 +383,6 @@ PyObject *PyCodec_StreamWriter(const char *encoding,
return codec_getstreamcodec(encoding, stream, errors, 3);
}
-static void
-add_note_to_codec_error(const char *operation,
- const char *encoding)
-{
- PyObject *exc = PyErr_GetRaisedException();
- if (exc == NULL) {
- return;
- }
- PyObject *note = PyUnicode_FromFormat("%s with '%s' codec failed",
- operation, encoding);
- if (note == NULL) {
- _PyErr_ChainExceptions1(exc);
- return;
- }
- int res = _PyException_AddNote(exc, note);
- Py_DECREF(note);
- if (res < 0) {
- _PyErr_ChainExceptions1(exc);
- return;
- }
- PyErr_SetRaisedException(exc);
-}
-
/* Encode an object (e.g. a Unicode object) using the given encoding
and return the resulting encoded object (usually a Python string).
@@ -425,7 +403,7 @@ _PyCodec_EncodeInternal(PyObject *object,
result = PyObject_Call(encoder, args, NULL);
if (result == NULL) {
- add_note_to_codec_error("encoding", encoding);
+ _PyErr_FormatNote("%s with '%s' codec failed", "encoding", encoding);
goto onError;
}
@@ -470,7 +448,7 @@ _PyCodec_DecodeInternal(PyObject *object,
result = PyObject_Call(decoder, args, NULL);
if (result == NULL) {
- add_note_to_codec_error("decoding", encoding);
+ _PyErr_FormatNote("%s with '%s' codec failed", "decoding", encoding);
goto onError;
}
if (!PyTuple_Check(result) ||
diff --git a/Python/errors.c b/Python/errors.c
index ab14770..0ff6a0d 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -1200,6 +1200,33 @@ PyErr_Format(PyObject *exception, const char *format, ...)
}
+/* Adds a note to the current exception (if any) */
+void
+_PyErr_FormatNote(const char *format, ...)
+{
+ PyObject *exc = PyErr_GetRaisedException();
+ if (exc == NULL) {
+ return;
+ }
+ va_list vargs;
+ va_start(vargs, format);
+ PyObject *note = PyUnicode_FromFormatV(format, vargs);
+ va_end(vargs);
+ if (note == NULL) {
+ goto error;
+ }
+ int res = _PyException_AddNote(exc, note);
+ Py_DECREF(note);
+ if (res < 0) {
+ goto error;
+ }
+ PyErr_SetRaisedException(exc);
+ return;
+error:
+ _PyErr_ChainExceptions1(exc);
+}
+
+
PyObject *
PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
{