GeezerVison is showing github.com — C API: Detect invalid UCS4 characters in debug mode · Issue #158445 · python/cpython
C API: Detect invalid UCS4 characters in debug mode #158445Labels interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error DescriptionCurrently, PyUnicode_FromKindAndData() and PyUnicodeWriter_WriteUCS4() create an invalid string if a character is not in the [U+0000; U+10ffff] range. Instead, I propose to raise an exception in this case.
Example creating an invalid string:
$ python3.15
>>> import _testcapi, sys
>>> invalid_ucs4 = (0x11_0000).to_bytes(4, byteorder=sys.byteorder)
>>> s = _testcapi.unicode_fromkindanddata(4, invalid_ucs4*5)
>>> list(s)
SystemError: invalid maximum character passed to PyUnicode_New
>>> s[0]
SystemError: invalid maximum character passed to PyUnicode_New
Linked PRs
Activityvstinner commented on Sep 29, 2026 With 32-bit wchar_t (ex: Unix), PyUnicode_FromWideChar() and PyUnicodeWriter_WriteWideChar() already raise a ValueError if a character is outside the [U+0000; U+10ffff] range. vstinner commented on Sep 29, 2026 Other Python and C functions raising an exception if the code pointer is negative or is greater than 0x10ffff:
- Python
chr() raises ValueError.
PyUnicode_FromOrdinal(), PyUnicode_Fill() and PyUnicodeWriter_WriteChar() raise ValueError.
PyUnicode_FromFormat("%c") raises OverflowError.
PyUnicode_New() raises SystemError.
PyUnicode_WriteChar() raises ValueError if the character is greater than the string maximum character.
changed the title [-]C API: Reject invalid UCS-4 string in PyUnicode_FromKindAndData[/-] [+]C API: Detect invalid UCS4 characters in debug mode[/+] on Sep 30, 2026vstinner commented on Sep 30, 2026 Since checking for invalid characters at runtime was rejected, I repurpose the issue to "Detect invalid UCS4 characters in debug mode". encukou commented on Oct 2, 2026 This broke several buildbots. vstinner commented on Oct 2, 2026
This broke several buildbots.
Oh, I didn't get buildbot failures report on my PR, so I didn't notice. Thanks for the notification.
So test_capi fails on 2 buildbots: Python is built in release mode with assertion using ./configure (...) CFLAGS=-UNDEBUG
I wrote a first PR #158603 to fix support.built_with_c_assertions() (merged). Previously, -UNDEBUG compiler flag was not handled properly.
I wrote a second PR #158604 to fix test_capi.test_unicode. added a commit that references this issue on Oct 2, 2026gh-158445: Skip invalid UCS-4 tests if Python is built with assertions ( vstinner commented on Oct 2, 2026 My latest change should fix the 2 buildbots. encukou commented on Oct 2, 2026 Reacted by Victor Stinner vstinner commented on Oct 3, 2026
MetadataMetadatainterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error DevelopmentNo branches or pull requestsIssue actions
|