Mercurial > cpython-lru_cache-weakref
diff Python/codecs.c @ 87084:854a2cea31b9
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
| author | Nick Coghlan <ncoghlan@gmail.com> |
|---|---|
| date | Wed, 13 Nov 2013 23:49:21 +1000 |
| parents | 3055e61f1e66 |
| children | 4ea622c085ca |
line wrap: on
line diff
--- a/Python/codecs.c Wed Nov 13 14:17:30 2013 +0100 +++ b/Python/codecs.c Wed Nov 13 23:49:21 2013 +1000 @@ -332,6 +332,22 @@ 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 @@ Py_XDECREF(result); Py_XDECREF(args); Py_XDECREF(encoder); + wrap_codec_error("encoding", encoding); return NULL; } @@ -422,6 +439,7 @@ Py_XDECREF(args); Py_XDECREF(decoder); Py_XDECREF(result); + wrap_codec_error("decoding", encoding); return NULL; }
