Skip to content

Commit 4d3b405

Browse files
authored
gh-156939: Document that PyUnicodeObject ends with null character (#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.
1 parent 3229a4e commit 4d3b405

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
@@ -16,6 +16,13 @@ points must be below 1114112 (which is the full Unicode range).
1616

1717
UTF-8 representation is created on demand and cached in the Unicode object.
1818

19+
.. impl-detail::
20+
21+
The internal buffer always includes an extra trailing null character for
22+
compatibility with null terminated C strings. This extra character is not
23+
counted in :c:func:`PyUnicode_GetLength` nor in the various *size* arguments
24+
of the functions below.
25+
1926
.. note::
2027
The :c:type:`Py_UNICODE` representation has been removed since Python 3.12
2128
with deprecated APIs.
@@ -164,11 +171,15 @@ access to internal read-only data of Unicode objects:
164171
.. versionadded:: 3.3
165172
166173
167-
.. c:function:: Py_UCS4 PyUnicode_READ(int kind, void *data, \
168-
Py_ssize_t index)
174+
.. c:function:: Py_UCS4 PyUnicode_READ(int kind, void *data, Py_ssize_t index)
169175
170176
Read a code point from a canonical representation *data* (as obtained with
171-
:c:func:`PyUnicode_DATA`). No checks or ready calls are performed.
177+
:c:func:`PyUnicode_DATA`). No checks are performed.
178+
179+
.. impl-detail::
180+
181+
Accept reading the trailing null character at index
182+
:c:func:`PyUnicode_GetLength`.
172183
173184
.. versionadded:: 3.3
174185
@@ -179,6 +190,11 @@ access to internal read-only data of Unicode objects:
179190
representation. This is less efficient than :c:func:`PyUnicode_READ` if you
180191
do multiple consecutive reads.
181192
193+
.. impl-detail::
194+
195+
Accept reading the trailing null character at index
196+
:c:func:`PyUnicode_GetLength`.
197+
182198
.. versionadded:: 3.3
183199
184200
@@ -716,6 +732,10 @@ APIs:
716732
717733
On error, set an exception and return ``-1``.
718734
735+
.. impl-detail::
736+
737+
The length does not count the trailing null character.
738+
719739
.. versionadded:: 3.3
720740
721741
@@ -794,6 +814,11 @@ APIs:
794814
795815
Return character on success, ``-1`` on error with an exception set.
796816
817+
.. impl-detail::
818+
819+
Do not accept reading the trailing null character at index
820+
:c:func:`PyUnicode_GetLength`.
821+
797822
.. versionadded:: 3.3
798823
799824

0 commit comments

Comments
 (0)