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_SurrogateEscapeErrors #129175

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
138 changes: 76 additions & 62 deletions Python/codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1310,76 +1310,90 @@ PyCodec_SurrogatePassErrors(PyObject *exc)
}
}


static PyObject *
PyCodec_SurrogateEscapeErrors(PyObject *exc)
_PyCodec_SurrogateEscapeUnicodeEncodeError(PyObject *exc)
{
PyObject *restuple;
PyObject *object;
Py_ssize_t i;
Py_ssize_t start;
Py_ssize_t end;
PyObject *res;
PyObject *obj;
Py_ssize_t start, end, slen;
if (_PyUnicodeError_GetParams(exc,
&obj, NULL,
&start, &end, &slen, false) < 0)
{
return NULL;
}

if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
char *outp;
if (PyUnicodeEncodeError_GetStart(exc, &start))
return NULL;
if (PyUnicodeEncodeError_GetEnd(exc, &end))
return NULL;
if (!(object = PyUnicodeEncodeError_GetObject(exc)))
return NULL;
res = PyBytes_FromStringAndSize(NULL, end-start);
if (!res) {
Py_DECREF(object);
PyObject *res = PyBytes_FromStringAndSize(NULL, slen);
if (res == NULL) {
Py_DECREF(obj);
return NULL;
}

char *outp = PyBytes_AsString(res);
for (Py_ssize_t i = start; i < end; i++) {
/* object is guaranteed to be "ready" */
Py_UCS4 ch = PyUnicode_READ_CHAR(obj, i);
if (ch < 0xdc80 || ch > 0xdcff) {
Py_DECREF(obj);
Py_DECREF(res);
PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
return NULL;
}
outp = PyBytes_AsString(res);
for (i = start; i < end; i++) {
/* object is guaranteed to be "ready" */
Py_UCS4 ch = PyUnicode_READ_CHAR(object, i);
if (ch < 0xdc80 || ch > 0xdcff) {
/* Not a UTF-8b surrogate, fail with original exception */
PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
Py_DECREF(res);
Py_DECREF(object);
return NULL;
}
*outp++ = ch - 0xdc00;
*outp++ = ch - 0xdc00;
}
Py_DECREF(obj);

return Py_BuildValue("(Nn)", res, end);
}


static PyObject *
_PyCodec_SurrogateEscapeUnicodeDecodeError(PyObject *exc)
{
PyObject *obj;
Py_ssize_t start, end, slen;
if (_PyUnicodeError_GetParams(exc,
&obj, NULL,
&start, &end, &slen, true) < 0)
{
return NULL;
}

Py_UCS2 ch[4]; /* decode up to 4 bad bytes. */
int consumed = 0;
const unsigned char *p = (const unsigned char*)PyBytes_AS_STRING(obj);
while (consumed < 4 && consumed < slen) {
/* Refuse to escape ASCII bytes. */
if (p[start + consumed] < 128) {
break;
}
restuple = Py_BuildValue("(On)", res, end);
Py_DECREF(res);
Py_DECREF(object);
return restuple;
ch[consumed] = 0xdc00 + p[start + consumed];
consumed++;
}
Py_DECREF(obj);

if (consumed == 0) {
/* codec complained about ASCII byte. */
PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
return NULL;
}

PyObject *str = PyUnicode_FromKindAndData(PyUnicode_2BYTE_KIND, ch, consumed);
if (str == NULL) {
return NULL;
}
return Py_BuildValue("(Nn)", str, start + consumed);
}


static PyObject *
PyCodec_SurrogateEscapeErrors(PyObject *exc)
{
if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
return _PyCodec_SurrogateEscapeUnicodeEncodeError(exc);
}
else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
PyObject *str;
const unsigned char *p;
Py_UCS2 ch[4]; /* decode up to 4 bad bytes. */
int consumed = 0;
if (PyUnicodeDecodeError_GetStart(exc, &start))
return NULL;
if (PyUnicodeDecodeError_GetEnd(exc, &end))
return NULL;
if (!(object = PyUnicodeDecodeError_GetObject(exc)))
return NULL;
p = (const unsigned char*)PyBytes_AS_STRING(object);
while (consumed < 4 && consumed < end-start) {
/* Refuse to escape ASCII bytes. */
if (p[start+consumed] < 128)
break;
ch[consumed] = 0xdc00 + p[start+consumed];
consumed++;
}
Py_DECREF(object);
if (!consumed) {
/* codec complained about ASCII byte. */
PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
return NULL;
}
str = PyUnicode_FromKindAndData(PyUnicode_2BYTE_KIND, ch, consumed);
if (str == NULL)
return NULL;
return Py_BuildValue("(Nn)", str, start+consumed);
return _PyCodec_SurrogateEscapeUnicodeDecodeError(exc);
}
else {
wrong_exception_type(exc);
Expand Down
Loading