diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2013-11-13 13:49:21 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2013-11-13 13:49:21 (GMT) |
commit | 8b097b4ed726b8282fce582cb2c20ab9c986fc21 (patch) | |
tree | ca9b18d186c9132f62378e1bde87e766beb2b379 /Python | |
parent | 59799a83995f135bdb1b1a0994052c1f24c68e83 (diff) | |
download | cpython-8b097b4ed726b8282fce582cb2c20ab9c986fc21.zip cpython-8b097b4ed726b8282fce582cb2c20ab9c986fc21.tar.gz cpython-8b097b4ed726b8282fce582cb2c20ab9c986fc21.tar.bz2 |
Close #17828: better handling of codec errors
- output type errors now redirect users to the type-neutral
convenience functions in the codecs module
- stateless errors that occur during encoding and decoding
will now be automatically wrapped in exceptions that give
the name of the codec involved
Diffstat (limited to 'Python')
-rw-r--r-- | Python/codecs.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/Python/codecs.c b/Python/codecs.c index c541ba0..e2edc26 100644 --- a/Python/codecs.c +++ b/Python/codecs.c @@ -332,6 +332,22 @@ 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) +{ + /* 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); +} + /* Encode an object (e.g. an Unicode object) using the given encoding and return the resulting encoded object (usually a Python string). @@ -376,6 +392,7 @@ PyObject *PyCodec_Encode(PyObject *object, Py_XDECREF(result); Py_XDECREF(args); Py_XDECREF(encoder); + wrap_codec_error("encoding", encoding); return NULL; } @@ -422,6 +439,7 @@ PyObject *PyCodec_Decode(PyObject *object, Py_XDECREF(args); Py_XDECREF(decoder); Py_XDECREF(result); + wrap_codec_error("decoding", encoding); return NULL; } |