Repository navigation
C API: Detect invalid UCS4 characters in debug mode #158445
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Sep 29, 2026 With 32-bit
wchar_t(ex: Unix),PyUnicode_FromWideChar()andPyUnicodeWriter_WriteWideChar()already raise a ValueError if a character is outside the [U+0000; U+10ffff] range.Other Python and C functions raising an exception if the code pointer is negative or is greater than 0x10ffff:
- Python
chr()raisesValueError. PyUnicode_FromOrdinal(),PyUnicode_Fill()andPyUnicodeWriter_WriteChar()raiseValueError.PyUnicode_FromFormat("%c")raises OverflowError.PyUnicode_New()raisesSystemError.
PyUnicode_WriteChar()raises ValueError if the character is greater than the string maximum character.- Python
- 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, 2026 Since checking for invalid characters at runtime was rejected, I repurpose the issue to "Detect invalid UCS4 characters in debug mode".
- addedinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
on Oct 1, 2026 This broke several buildbots.
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- ARM64 Raspbian 3.x: https://buildbot.python.org/#/builders/1678/builds/3888
- x86 Debian Non-Debug with X 3.x: https://buildbot.python.org/#/builders/1245/builds/9676
I wrote a first PR #158603 to fix
support.built_with_c_assertions()(merged). Previously,-UNDEBUGcompiler 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, 2026 My latest change should fix the 2 buildbots.
Thank you!
Reacted by Victor StinnerARM64 Raspbian 3.x: https://buildbot.python.org/#/builders/1678/builds/3888
x86 Debian Non-Debug with X 3.x: https://buildbot.python.org/#/builders/1245/builds/9676Both workers are back to green.
Currently, 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:
Linked PRs