Skip to content

Commit

Permalink
Add test and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Jun 11, 2024
1 parent 076985d commit fa0ff6d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Lib/test/test_capi/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1709,6 +1709,10 @@ def test_unicode_export(self):
('ucs4:\U0010ffff'.encode(ucs4_enc),
PyUnicode_FORMAT_UCS4))

# export ASCII as UCS1
self.assertEqual(unicode_export("abc", PyUnicode_FORMAT_UCS1),
(b'abc', PyUnicode_FORMAT_UCS1))

# always export to UCS4
self.assertEqual(unicode_export("abc", PyUnicode_FORMAT_UCS4),
('abc'.encode(ucs4_enc), PyUnicode_FORMAT_UCS4))
Expand Down
2 changes: 2 additions & 0 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2142,6 +2142,7 @@ PyUnicode_Export(PyObject *unicode, unsigned int supported_formats,
}

if (supported_formats & PyUnicode_FORMAT_UCS4) {
// Convert UCS1 or UCS2 to UCS4
Py_UCS4 *ucs4 = PyUnicode_AsUCS4Copy(unicode);
if (ucs4 == NULL) {
goto error;
Expand All @@ -2152,6 +2153,7 @@ PyUnicode_Export(PyObject *unicode, unsigned int supported_formats,
}

if (supported_formats & PyUnicode_FORMAT_UTF8) {
// Encode UCS1, UCS2 or UCS4 to UTF-8
const char *utf8 = PyUnicode_AsUTF8AndSize(unicode, size);
if (utf8 == NULL) {
goto error;
Expand Down

0 comments on commit fa0ff6d

Please sign in to comment.