Skip to content

Commit

Permalink
gh-126004: Fix positions handling in codecs.replace_errors (#127674)
Browse files Browse the repository at this point in the history
This fixes how `PyCodec_ReplaceErrors` handles the `start` and `end` attributes
of `UnicodeError` objects via the `_PyUnicodeError_GetParams` helper.
  • Loading branch information
picnixz authored Jan 23, 2025
1 parent 70dcc84 commit 225296c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
3 changes: 2 additions & 1 deletion Lib/test/test_capi/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,8 @@ def test_codec_ignore_errors_handler(self):

def test_codec_replace_errors_handler(self):
handler = _testcapi.codec_replace_errors
self.do_test_codec_errors_handler(handler, self.all_unicode_errors)
self.do_test_codec_errors_handler(handler, self.all_unicode_errors,
safe=True)

def test_codec_xmlcharrefreplace_errors_handler(self):
handler = _testcapi.codec_xmlcharrefreplace_errors
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix handling of :attr:`UnicodeError.start` and :attr:`UnicodeError.end`
values in the :func:`codecs.replace_errors` error handler. Patch by Bénédikt
Tran.
46 changes: 22 additions & 24 deletions Python/codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,48 +702,46 @@ PyObject *PyCodec_IgnoreErrors(PyObject *exc)

PyObject *PyCodec_ReplaceErrors(PyObject *exc)
{
Py_ssize_t start, end, i, len;
Py_ssize_t start, end, slen;

if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
PyObject *res;
Py_UCS1 *outp;
if (PyUnicodeEncodeError_GetStart(exc, &start))
if (_PyUnicodeError_GetParams(exc, NULL, NULL,
&start, &end, &slen, false) < 0) {
return NULL;
if (PyUnicodeEncodeError_GetEnd(exc, &end))
return NULL;
len = end - start;
res = PyUnicode_New(len, '?');
if (res == NULL)
}
PyObject *res = PyUnicode_New(slen, '?');
if (res == NULL) {
return NULL;
}
assert(PyUnicode_KIND(res) == PyUnicode_1BYTE_KIND);
outp = PyUnicode_1BYTE_DATA(res);
for (i = 0; i < len; ++i)
outp[i] = '?';
Py_UCS1 *outp = PyUnicode_1BYTE_DATA(res);
memset(outp, '?', sizeof(Py_UCS1) * slen);
assert(_PyUnicode_CheckConsistency(res, 1));
return Py_BuildValue("(Nn)", res, end);
}
else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
if (PyUnicodeDecodeError_GetEnd(exc, &end))
if (_PyUnicodeError_GetParams(exc, NULL, NULL,
NULL, &end, NULL, true) < 0) {
return NULL;
}
return Py_BuildValue("(Cn)",
(int)Py_UNICODE_REPLACEMENT_CHARACTER,
end);
}
else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeTranslateError)) {
PyObject *res;
Py_UCS2 *outp;
if (PyUnicodeTranslateError_GetStart(exc, &start))
return NULL;
if (PyUnicodeTranslateError_GetEnd(exc, &end))
if (_PyUnicodeError_GetParams(exc, NULL, NULL,
&start, &end, &slen, false) < 0) {
return NULL;
len = end - start;
res = PyUnicode_New(len, Py_UNICODE_REPLACEMENT_CHARACTER);
if (res == NULL)
}
PyObject *res = PyUnicode_New(slen, Py_UNICODE_REPLACEMENT_CHARACTER);
if (res == NULL) {
return NULL;
assert(PyUnicode_KIND(res) == PyUnicode_2BYTE_KIND);
outp = PyUnicode_2BYTE_DATA(res);
for (i = 0; i < len; i++)
}
assert(slen == 0 || PyUnicode_KIND(res) == PyUnicode_2BYTE_KIND);
Py_UCS2 *outp = PyUnicode_2BYTE_DATA(res);
for (Py_ssize_t i = 0; i < slen; ++i) {
outp[i] = Py_UNICODE_REPLACEMENT_CHARACTER;
}
assert(_PyUnicode_CheckConsistency(res, 1));
return Py_BuildValue("(Nn)", res, end);
}
Expand Down

0 comments on commit 225296c

Please sign in to comment.