FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

C API: Detect invalid UCS4 characters in debug mode · Issue #158445 · python/cpython · GitHub

Repository navigation

C API: Detect invalid UCS4 characters in debug mode #158445

Description

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:

$ 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

Activity

  1. vstinner commented on Sep 29, 2026

    MemberAuthor

    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.

  2. added 2 commits that reference this issue on Sep 29, 2026
  3. vstinner commented on Sep 29, 2026

    MemberAuthor

    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.

  4. added 3 commits that reference this issue on Sep 30, 2026
  5. 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
  6. vstinner commented on Sep 30, 2026

    MemberAuthor

    Since checking for invalid characters at runtime was rejected, I repurpose the issue to "Detect invalid UCS4 characters in debug mode".

  7. encukou commented on Oct 2, 2026

    Member

    This broke several buildbots.

  8. added a commit that references this issue on Oct 2, 2026
  9. vstinner commented on Oct 2, 2026

    MemberAuthor

    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.

  10. added a commit that references this issue on Oct 2, 2026
  11. vstinner commented on Oct 2, 2026

    MemberAuthor

    My latest change should fix the 2 buildbots.

  12. encukou commented on Oct 2, 2026

    Member

    Thank you!

  13. added a commit that references this issue on Oct 3, 2026
  14. vstinner commented on Oct 3, 2026

    MemberAuthor

    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

    Both workers are back to green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL