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.
$ 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
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