mirror of
https://github.com/python/cpython.git
synced 2025-07-14 23:05:17 +00:00
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
This commit is contained in:
parent
59799a8399
commit
8b097b4ed7
7 changed files with 414 additions and 46 deletions
|
@ -3054,8 +3054,10 @@ PyUnicode_Decode(const char *s,
|
|||
goto onError;
|
||||
if (!PyUnicode_Check(unicode)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"decoder did not return a str object (type=%.400s)",
|
||||
Py_TYPE(unicode)->tp_name);
|
||||
"'%.400s' decoder returned '%.400s' instead of 'str'; "
|
||||
"use codecs.decode() to decode to arbitrary types",
|
||||
encoding,
|
||||
Py_TYPE(unicode)->tp_name, Py_TYPE(unicode)->tp_name);
|
||||
Py_DECREF(unicode);
|
||||
goto onError;
|
||||
}
|
||||
|
@ -3113,8 +3115,10 @@ PyUnicode_AsDecodedUnicode(PyObject *unicode,
|
|||
goto onError;
|
||||
if (!PyUnicode_Check(v)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"decoder did not return a str object (type=%.400s)",
|
||||
Py_TYPE(v)->tp_name);
|
||||
"'%.400s' decoder returned '%.400s' instead of 'str'; "
|
||||
"use codecs.decode() to decode to arbitrary types",
|
||||
encoding,
|
||||
Py_TYPE(unicode)->tp_name, Py_TYPE(unicode)->tp_name);
|
||||
Py_DECREF(v);
|
||||
goto onError;
|
||||
}
|
||||
|
@ -3425,7 +3429,8 @@ PyUnicode_AsEncodedString(PyObject *unicode,
|
|||
PyObject *b;
|
||||
|
||||
error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
|
||||
"encoder %s returned bytearray instead of bytes",
|
||||
"encoder %s returned bytearray instead of bytes; "
|
||||
"use codecs.encode() to encode to arbitrary types",
|
||||
encoding);
|
||||
if (error) {
|
||||
Py_DECREF(v);
|
||||
|
@ -3438,8 +3443,10 @@ PyUnicode_AsEncodedString(PyObject *unicode,
|
|||
}
|
||||
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"encoder did not return a bytes object (type=%.400s)",
|
||||
Py_TYPE(v)->tp_name);
|
||||
"'%.400s' encoder returned '%.400s' instead of 'bytes'; "
|
||||
"use codecs.encode() to encode to arbitrary types",
|
||||
encoding,
|
||||
Py_TYPE(v)->tp_name, Py_TYPE(v)->tp_name);
|
||||
Py_DECREF(v);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -3465,8 +3472,10 @@ PyUnicode_AsEncodedUnicode(PyObject *unicode,
|
|||
goto onError;
|
||||
if (!PyUnicode_Check(v)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"encoder did not return an str object (type=%.400s)",
|
||||
Py_TYPE(v)->tp_name);
|
||||
"'%.400s' encoder returned '%.400s' instead of 'str'; "
|
||||
"use codecs.encode() to encode to arbitrary types",
|
||||
encoding,
|
||||
Py_TYPE(v)->tp_name, Py_TYPE(v)->tp_name);
|
||||
Py_DECREF(v);
|
||||
goto onError;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue