Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-129173: Use _PyUnicodeError_GetParams in PyCodec_IgnoreErrors #129174

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 43 additions & 19 deletions Python/codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -659,44 +659,64 @@ PyObject *PyCodec_LookupError(const char *name)
return handler;
}

static void wrong_exception_type(PyObject *exc)

static inline void
wrong_exception_type(PyObject *exc)
{
PyErr_Format(PyExc_TypeError,
"don't know how to handle %.200s in error callback",
Py_TYPE(exc)->tp_name);
"don't know how to handle %T in error callback", exc);
}


#define _PyIsUnicodeEncodeError(EXC) \
PyObject_TypeCheck(EXC, (PyTypeObject *)PyExc_UnicodeEncodeError)
#define _PyIsUnicodeDecodeError(EXC) \
PyObject_TypeCheck(EXC, (PyTypeObject *)PyExc_UnicodeDecodeError)
#define _PyIsUnicodeTranslateError(EXC) \
PyObject_TypeCheck(EXC, (PyTypeObject *)PyExc_UnicodeTranslateError)


// --- handler: 'strict' ------------------------------------------------------

PyObject *PyCodec_StrictErrors(PyObject *exc)
{
if (PyExceptionInstance_Check(exc))
if (PyExceptionInstance_Check(exc)) {
PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
else
}
else {
PyErr_SetString(PyExc_TypeError, "codec must pass exception instance");
}
return NULL;
}


PyObject *PyCodec_IgnoreErrors(PyObject *exc)
// --- handler: 'ignore' ------------------------------------------------------

static PyObject *
_PyCodec_IgnoreError(PyObject *exc, int as_bytes)
{
Py_ssize_t end;

if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
if (PyUnicodeEncodeError_GetEnd(exc, &end))
return NULL;
if (_PyUnicodeError_GetParams(exc, NULL, NULL, NULL,
&end, NULL, as_bytes) < 0)
{
return NULL;
}
else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
if (PyUnicodeDecodeError_GetEnd(exc, &end))
return NULL;
return Py_BuildValue("(Nn)", Py_GetConstant(Py_CONSTANT_EMPTY_STR), end);
}


PyObject *PyCodec_IgnoreErrors(PyObject *exc)
{
if (_PyIsUnicodeEncodeError(exc) || _PyIsUnicodeTranslateError(exc)) {
return _PyCodec_IgnoreError(exc, false);
}
else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeTranslateError)) {
if (PyUnicodeTranslateError_GetEnd(exc, &end))
return NULL;
else if (_PyIsUnicodeDecodeError(exc)) {
return _PyCodec_IgnoreError(exc, true);
}
else {
wrong_exception_type(exc);
return NULL;
}
return Py_BuildValue("(Nn)", Py_GetConstant(Py_CONSTANT_EMPTY_STR), end);
}


Expand Down Expand Up @@ -1368,13 +1388,17 @@ PyCodec_SurrogateEscapeErrors(PyObject *exc)
}


static PyObject *strict_errors(PyObject *self, PyObject *exc)
// --- Codecs registry handlers -----------------------------------------------

static inline PyObject *
strict_errors(PyObject *Py_UNUSED(self), PyObject *exc)
{
return PyCodec_StrictErrors(exc);
}


static PyObject *ignore_errors(PyObject *self, PyObject *exc)
static inline PyObject *
ignore_errors(PyObject *Py_UNUSED(self), PyObject *exc)
{
return PyCodec_IgnoreErrors(exc);
}
Expand Down
Loading