Skip to content

Commit da4f375

Browse files
[3.14] gh-69011: clarify & deduplicate ctypes.create_*_buffer docs (GH-132858) (GH-134881)
This adds a warning about the possibly-missing NUL terminator, but in a way that doesn't make it sound like a bug/wart. (cherry picked from commit b783e17) Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
1 parent 7dc4496 commit da4f375

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

Doc/library/ctypes.rst

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,35 +2031,55 @@ Utility functions
20312031
pointer.
20322032

20332033

2034-
.. function:: create_string_buffer(init_or_size, size=None)
2034+
.. function:: create_string_buffer(init, size=None)
2035+
create_string_buffer(size)
20352036

20362037
This function creates a mutable character buffer. The returned object is a
20372038
ctypes array of :class:`c_char`.
20382039

2039-
*init_or_size* must be an integer which specifies the size of the array, or a
2040-
bytes object which will be used to initialize the array items.
2040+
If *size* is given (and not ``None``), it must be an :class:`int`.
2041+
It specifies the size of the returned array.
20412042

2042-
If a bytes object is specified as first argument, the buffer is made one item
2043-
larger than its length so that the last element in the array is a NUL
2044-
termination character. An integer can be passed as second argument which allows
2045-
specifying the size of the array if the length of the bytes should not be used.
2043+
If the *init* argument is given, it must be :class:`bytes`. It is used
2044+
to initialize the array items. Bytes not initialized this way are
2045+
set to zero (NUL).
2046+
2047+
If *size* is not given (or if it is ``None``), the buffer is made one element
2048+
larger than *init*, effectively adding a NUL terminator.
2049+
2050+
If both arguments are given, *size* must not be less than ``len(init)``.
2051+
2052+
.. warning::
2053+
2054+
If *size* is equal to ``len(init)``, a NUL terminator is
2055+
not added. Do not treat such a buffer as a C string.
2056+
2057+
For example::
2058+
2059+
>>> bytes(create_string_buffer(2))
2060+
b'\x00\x00'
2061+
>>> bytes(create_string_buffer(b'ab'))
2062+
b'ab\x00'
2063+
>>> bytes(create_string_buffer(b'ab', 2))
2064+
b'ab'
2065+
>>> bytes(create_string_buffer(b'ab', 4))
2066+
b'ab\x00\x00'
2067+
>>> bytes(create_string_buffer(b'abcdef', 2))
2068+
Traceback (most recent call last):
2069+
...
2070+
ValueError: byte string too long
20462071

20472072
.. audit-event:: ctypes.create_string_buffer init,size ctypes.create_string_buffer
20482073

20492074

2050-
.. function:: create_unicode_buffer(init_or_size, size=None)
2075+
.. function:: create_unicode_buffer(init, size=None)
2076+
create_unicode_buffer(size)
20512077

20522078
This function creates a mutable unicode character buffer. The returned object is
20532079
a ctypes array of :class:`c_wchar`.
20542080

2055-
*init_or_size* must be an integer which specifies the size of the array, or a
2056-
string which will be used to initialize the array items.
2057-
2058-
If a string is specified as first argument, the buffer is made one item
2059-
larger than the length of the string so that the last element in the array is a
2060-
NUL termination character. An integer can be passed as second argument which
2061-
allows specifying the size of the array if the length of the string should not
2062-
be used.
2081+
The function takes the same arguments as :func:`~create_string_buffer` except
2082+
*init* must be a string and *size* counts :class:`c_wchar`.
20632083

20642084
.. audit-event:: ctypes.create_unicode_buffer init,size ctypes.create_unicode_buffer
20652085

0 commit comments

Comments
 (0)