Python 3.14 added PyUnicodeWriter C API which is a safe way to create a Unicode object. IMO it's now time to soft deprecate the legacy way to create a str object using PyUnicode_New(): deprecate functions like PyUnicode_CopyCharacters() and PyUnicode_Resize().
The PyUnicode_New() API is more fragile. It's possible that a str object is "used" before it's fully initialized, which make following functions (modifying the str) fail or crash. It can also be exposed in Python by mistake before it's fully initialized.
PyUnicodeWriter has a different design than PyUnicode_New(): it's designed to append multiple strings, whereas PyUnicode_New() gives a direct access to an array of characters in a specific format (Py_UCS1, Py_UCS2 or Py_UCS4: 1, 2 or 4 bytes per character).
If PyUnicodeWriter doesn't fit well with existing code, it's possible to allocate an array of characters, write into this array, and then call PyUnicode_FromKindAndData() to create a str object.
Another option is to create a UTF-8 encode string and then call PyUnicode_FromString().
Linked PRs
Python 3.14 added
PyUnicodeWriterC API which is a safe way to create a Unicode object. IMO it's now time to soft deprecate the legacy way to create a str object usingPyUnicode_New(): deprecate functions likePyUnicode_CopyCharacters()andPyUnicode_Resize().The
PyUnicode_New()API is more fragile. It's possible that a str object is "used" before it's fully initialized, which make following functions (modifying the str) fail or crash. It can also be exposed in Python by mistake before it's fully initialized.PyUnicodeWriterhas a different design thanPyUnicode_New(): it's designed to append multiple strings, whereasPyUnicode_New()gives a direct access to an array of characters in a specific format (Py_UCS1, Py_UCS2 or Py_UCS4: 1, 2 or 4 bytes per character).If
PyUnicodeWriterdoesn't fit well with existing code, it's possible to allocate an array of characters, write into this array, and then callPyUnicode_FromKindAndData()to create a str object.Another option is to create a UTF-8 encode string and then call
PyUnicode_FromString().Linked PRs