Skip to content
Closed
Prev Previous commit
Next Next commit
Add test_unicode_export_import_roundtrip()
  • Loading branch information
vstinner committed Jun 21, 2024
commit 1c781036927abd5be60212a45814f06b12fa69d7
34 changes: 34 additions & 0 deletions Lib/test/test_capi/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1808,5 +1808,39 @@ def test_unicode_import(self):
unicode_import(ucs4[:-3], PyUnicode_FORMAT_UCS4)



def test_unicode_export_import_roundtrip(self):
unicode_export = _testlimitedcapi.unicode_export
unicode_import = _testlimitedcapi.unicode_import

ASCII = PyUnicode_FORMAT_ASCII
UCS1 = PyUnicode_FORMAT_UCS1
UCS2 = PyUnicode_FORMAT_UCS2
UCS4 = PyUnicode_FORMAT_UCS4
UTF8 = PyUnicode_FORMAT_UTF8
ALL = (ASCII | UCS1 | UCS2 | UCS4 | UTF8)

for string, allowed_formats in (
('', {ASCII, UCS1, UCS2, UCS4, UTF8}),
('ascii', {ASCII, UCS1, UCS2, UCS4, UTF8}),
('latin1:\xe9', {UCS1, UCS2, UCS4, UTF8}),
('ucs2:\u20ac', {UCS2, UCS4, UTF8}),
('ucs4:\U0001f638', {UCS4, UTF8}),
):
for format in ASCII, UCS1, UCS2, UCS4, UTF8:
with self.subTest(string=string, format=format):
if format not in allowed_formats:
with self.assertRaises(ValueError):
unicode_export(string, format)
else:
buf, buf_fmt = unicode_export(string, format)
restored = unicode_import(buf, buf_fmt)
self.assertEqual(restored, string)

buf, buf_fmt = unicode_export(string, ALL)
restored = unicode_import(buf, buf_fmt)
self.assertEqual(restored, string)


if __name__ == '__main__':
unittest.main()
6 changes: 3 additions & 3 deletions Modules/_testlimitedcapi/unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -1843,14 +1843,14 @@ static PyObject*
unicode_export(PyObject *self, PyObject *args)
{
PyObject *obj;
unsigned int supported_formats;
if (!PyArg_ParseTuple(args, "OI", &obj, &supported_formats)) {
unsigned int requested_formats;
if (!PyArg_ParseTuple(args, "OI", &obj, &requested_formats)) {
return NULL;
}

Py_ssize_t size;
uint32_t format;
const void *data = PyUnicode_Export(obj, supported_formats, &size, &format);
const void *data = PyUnicode_Export(obj, requested_formats, &size, &format);
if (data == NULL) {
return NULL;
}
Expand Down