Skip to content

Commit fa0ff6d

Browse files
committed
Add test and comments
1 parent 076985d commit fa0ff6d

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

Lib/test/test_capi/test_unicode.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,10 @@ def test_unicode_export(self):
17091709
('ucs4:\U0010ffff'.encode(ucs4_enc),
17101710
PyUnicode_FORMAT_UCS4))
17111711

1712+
# export ASCII as UCS1
1713+
self.assertEqual(unicode_export("abc", PyUnicode_FORMAT_UCS1),
1714+
(b'abc', PyUnicode_FORMAT_UCS1))
1715+
17121716
# always export to UCS4
17131717
self.assertEqual(unicode_export("abc", PyUnicode_FORMAT_UCS4),
17141718
('abc'.encode(ucs4_enc), PyUnicode_FORMAT_UCS4))

Objects/unicodeobject.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,6 +2142,7 @@ PyUnicode_Export(PyObject *unicode, unsigned int supported_formats,
21422142
}
21432143

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

21542155
if (supported_formats & PyUnicode_FORMAT_UTF8) {
2156+
// Encode UCS1, UCS2 or UCS4 to UTF-8
21552157
const char *utf8 = PyUnicode_AsUTF8AndSize(unicode, size);
21562158
if (utf8 == NULL) {
21572159
goto error;

0 commit comments

Comments
 (0)