Skip to content

Commit 8ff13de

Browse files
pythongh-99612: Fix PyUnicode_DecodeUTF8Stateful() for ASCII-only data
Previously *consumed was not set in this case.
1 parent b0e1f9c commit 8ff13de

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

Lib/test/test_capi/test_unicode.py

+47
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,53 @@
1111

1212
class CAPITest(unittest.TestCase):
1313

14+
@support.cpython_only
15+
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
16+
def test_decodeutf8(self):
17+
"""Test PyUnicode_DecodeUTF8()"""
18+
from _testcapi import unicode_decodeutf8 as decodeutf8
19+
20+
for s in ['abc', '\xa1\xa2', '\u4f60\u597d', 'a\U0001f600']:
21+
b = s.encode('utf-8')
22+
self.assertEqual(decodeutf8(b), s)
23+
self.assertEqual(decodeutf8(b, 'strict'), s)
24+
25+
self.assertRaises(UnicodeDecodeError, decodeutf8, b'\x80')
26+
self.assertRaises(UnicodeDecodeError, decodeutf8, b'\xc0')
27+
self.assertRaises(UnicodeDecodeError, decodeutf8, b'\xff')
28+
self.assertRaises(UnicodeDecodeError, decodeutf8, b'a\xf0\x9f')
29+
self.assertEqual(decodeutf8(b'a\xf0\x9f', 'replace'), 'a\ufffd')
30+
self.assertEqual(decodeutf8(b'a\xf0\x9fb', 'replace'), 'a\ufffdb')
31+
32+
self.assertRaises(LookupError, decodeutf8, b'a\x80', 'foo')
33+
# TODO: Test PyUnicode_DecodeUTF8() with NULL as data and
34+
# negative size.
35+
36+
@support.cpython_only
37+
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
38+
def test_decodeutf8stateful(self):
39+
"""Test PyUnicode_DecodeUTF8Stateful()"""
40+
from _testcapi import unicode_decodeutf8stateful as decodeutf8stateful
41+
42+
for s in ['abc', '\xa1\xa2', '\u4f60\u597d', 'a\U0001f600']:
43+
b = s.encode('utf-8')
44+
self.assertEqual(decodeutf8stateful(b), (s, len(b)))
45+
self.assertEqual(decodeutf8stateful(b, 'strict'), (s, len(b)))
46+
47+
self.assertRaises(UnicodeDecodeError, decodeutf8stateful, b'\x80')
48+
self.assertRaises(UnicodeDecodeError, decodeutf8stateful, b'\xc0')
49+
self.assertRaises(UnicodeDecodeError, decodeutf8stateful, b'\xff')
50+
self.assertEqual(decodeutf8stateful(b'a\xf0\x9f'), ('a', 1))
51+
self.assertEqual(decodeutf8stateful(b'a\xf0\x9f', 'replace'), ('a', 1))
52+
self.assertRaises(UnicodeDecodeError, decodeutf8stateful, b'a\xf0\x9fb')
53+
self.assertEqual(decodeutf8stateful(b'a\xf0\x9fb', 'replace'), ('a\ufffdb', 4))
54+
55+
self.assertRaises(LookupError, decodeutf8stateful, b'a\x80', 'foo')
56+
# TODO: Test PyUnicode_DecodeUTF8Stateful() with NULL as data and
57+
# negative size.
58+
# TODO: Test PyUnicode_DecodeUTF8Stateful() with NULL as the address of
59+
# "consumed".
60+
1461
# Test PyUnicode_FromFormat()
1562
def test_from_format(self):
1663
import_helper.import_module('ctypes')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :c:func:`PyUnicode_DecodeUTF8Stateful` for ASCII-only data:
2+
``*consumed`` was not set.

Modules/_testcapi/unicode.c

+37
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#define PY_SSIZE_T_CLEAN
12
#include "parts.h"
23

34
static struct PyModuleDef *_testcapimodule = NULL; // set at initialization
@@ -223,6 +224,40 @@ unicode_asutf8andsize(PyObject *self, PyObject *args)
223224
return Py_BuildValue("(Nn)", result, utf8_len);
224225
}
225226

227+
/* Test PyUnicode_DecodeUTF8() */
228+
static PyObject *
229+
unicode_decodeutf8(PyObject *self, PyObject *args)
230+
{
231+
const char *data;
232+
Py_ssize_t size;
233+
const char *errors = NULL;
234+
235+
if (!PyArg_ParseTuple(args, "y#|z", &data, &size, &errors))
236+
return NULL;
237+
238+
return PyUnicode_DecodeUTF8(data, size, errors);
239+
}
240+
241+
/* Test PyUnicode_DecodeUTF8Stateful() */
242+
static PyObject *
243+
unicode_decodeutf8stateful(PyObject *self, PyObject *args)
244+
{
245+
const char *data;
246+
Py_ssize_t size;
247+
const char *errors = NULL;
248+
Py_ssize_t consumed;
249+
PyObject *result;
250+
251+
if (!PyArg_ParseTuple(args, "y#|z", &data, &size, &errors))
252+
return NULL;
253+
254+
result = PyUnicode_DecodeUTF8Stateful(data, size, errors, &consumed);
255+
if (!result) {
256+
return NULL;
257+
}
258+
return Py_BuildValue("(Nn)", result, consumed);
259+
}
260+
226261
static PyObject *
227262
unicode_count(PyObject *self, PyObject *args)
228263
{
@@ -716,6 +751,8 @@ static PyMethodDef TestMethods[] = {
716751
{"unicode_asucs4", unicode_asucs4, METH_VARARGS},
717752
{"unicode_asutf8", unicode_asutf8, METH_VARARGS},
718753
{"unicode_asutf8andsize", unicode_asutf8andsize, METH_VARARGS},
754+
{"unicode_decodeutf8", unicode_decodeutf8, METH_VARARGS},
755+
{"unicode_decodeutf8stateful",unicode_decodeutf8stateful, METH_VARARGS},
719756
{"unicode_count", unicode_count, METH_VARARGS},
720757
{"unicode_findchar", unicode_findchar, METH_VARARGS},
721758
{"unicode_copycharacters", unicode_copycharacters, METH_VARARGS},

Objects/unicodeobject.c

+3
Original file line numberDiff line numberDiff line change
@@ -4530,6 +4530,9 @@ unicode_decode_utf8(const char *s, Py_ssize_t size,
45304530
}
45314531
s += ascii_decode(s, end, PyUnicode_1BYTE_DATA(u));
45324532
if (s == end) {
4533+
if (consumed) {
4534+
*consumed = size;
4535+
}
45334536
return u;
45344537
}
45354538

0 commit comments

Comments
 (0)