Skip to content

Commit 823f781

Browse files
[3.14] gh-156939: Document that PyUnicodeObject ends with null character (GH-157708) (#159530)
gh-156939: Document that PyUnicodeObject ends with null character (GH-157708) Document also that PyUnicode_READ() and PyUnicode_READ_CHAR() accept reading the trailing null character, and that PyUnicode_GetLength() doesn't count the trailing null character. (cherry picked from commit 4d3b405) Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 1de946c commit 823f781

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

‎Doc/c-api/unicode.rst‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ points must be below 1114112 (which is the full Unicode range).
1919

2020
UTF-8 representation is created on demand and cached in the Unicode object.
2121

22+
.. impl-detail::
23+
24+
The internal buffer always includes an extra trailing null character for
25+
compatibility with null terminated C strings. This extra character is not
26+
counted in :c:func:`PyUnicode_GetLength` nor in the various *size* arguments
27+
of the functions below.
28+
2229
.. note::
2330
The :c:type:`Py_UNICODE` representation has been removed since Python 3.12
2431
with deprecated APIs.
@@ -167,11 +174,15 @@ access to internal read-only data of Unicode objects:
167174
.. versionadded:: 3.3
168175
169176
170-
.. c:function:: Py_UCS4 PyUnicode_READ(int kind, void *data, \
171-
Py_ssize_t index)
177+
.. c:function:: Py_UCS4 PyUnicode_READ(int kind, void *data, Py_ssize_t index)
172178
173179
Read a code point from a canonical representation *data* (as obtained with
174-
:c:func:`PyUnicode_DATA`). No checks or ready calls are performed.
180+
:c:func:`PyUnicode_DATA`). No checks are performed.
181+
182+
.. impl-detail::
183+
184+
Accept reading the trailing null character at index
185+
:c:func:`PyUnicode_GetLength`.
175186
176187
.. versionadded:: 3.3
177188
@@ -182,6 +193,11 @@ access to internal read-only data of Unicode objects:
182193
representation. This is less efficient than :c:func:`PyUnicode_READ` if you
183194
do multiple consecutive reads.
184195
196+
.. impl-detail::
197+
198+
Accept reading the trailing null character at index
199+
:c:func:`PyUnicode_GetLength`.
200+
185201
.. versionadded:: 3.3
186202
187203
@@ -703,6 +719,10 @@ APIs:
703719
704720
On error, set an exception and return ``-1``.
705721
722+
.. impl-detail::
723+
724+
The length does not count the trailing null character.
725+
706726
.. versionadded:: 3.3
707727
708728
@@ -781,6 +801,11 @@ APIs:
781801
782802
Return character on success, ``-1`` on error with an exception set.
783803
804+
.. impl-detail::
805+
806+
Do not accept reading the trailing null character at index
807+
:c:func:`PyUnicode_GetLength`.
808+
784809
.. versionadded:: 3.3
785810
786811

0 commit comments

Comments
 (0)