diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2023-03-21 21:36:31 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-21 21:36:31 (GMT) |
commit | 76350e85ebf96df588384f3d9bdc20d11045bef4 (patch) | |
tree | dfc8197d86f5cbb646d932d05e7612435887c5c7 /Python/codecs.c | |
parent | e6ecd3e6b437f3056e0a410a57c52e2639b56353 (diff) | |
download | cpython-76350e85ebf96df588384f3d9bdc20d11045bef4.zip cpython-76350e85ebf96df588384f3d9bdc20d11045bef4.tar.gz cpython-76350e85ebf96df588384f3d9bdc20d11045bef4.tar.bz2 |
gh-102406: replace exception chaining by PEP-678 notes in codecs (#102407)
Diffstat (limited to 'Python/codecs.c')
-rw-r--r-- | Python/codecs.c | 35 |
1 files changed, 21 insertions, 14 deletions
diff --git a/Python/codecs.c b/Python/codecs.c index b2087b4..9d800f9 100644 --- a/Python/codecs.c +++ b/Python/codecs.c @@ -382,20 +382,27 @@ PyObject *PyCodec_StreamWriter(const char *encoding, return codec_getstreamcodec(encoding, stream, errors, 3); } -/* Helper that tries to ensure the reported exception chain indicates the - * codec that was invoked to trigger the failure without changing the type - * of the exception raised. - */ static void -wrap_codec_error(const char *operation, - const char *encoding) +add_note_to_codec_error(const char *operation, + const char *encoding) { - /* TrySetFromCause will replace the active exception with a suitably - * updated clone if it can, otherwise it will leave the original - * exception alone. - */ - _PyErr_TrySetFromCause("%s with '%s' codec failed", - operation, 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 @@ -418,7 +425,7 @@ _PyCodec_EncodeInternal(PyObject *object, result = PyObject_Call(encoder, args, NULL); if (result == NULL) { - wrap_codec_error("encoding", encoding); + add_note_to_codec_error("encoding", encoding); goto onError; } @@ -463,7 +470,7 @@ _PyCodec_DecodeInternal(PyObject *object, result = PyObject_Call(decoder, args, NULL); if (result == NULL) { - wrap_codec_error("decoding", encoding); + add_note_to_codec_error("decoding", encoding); goto onError; } if (!PyTuple_Check(result) || |