Skip to content

Commit aebb4ea

Browse files
[3.13] gh-69011: : clarify & deduplicate ctypes.create_*_buffer docs (GH-132858) (GH-134882)
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 029ecea commit aebb4ea

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
@@ -1940,35 +1940,55 @@ Utility functions
19401940
pointer.
19411941

19421942

1943-
.. function:: create_string_buffer(init_or_size, size=None)
1943+
.. function:: create_string_buffer(init, size=None)
1944+
create_string_buffer(size)
19441945

19451946
This function creates a mutable character buffer. The returned object is a
19461947
ctypes array of :class:`c_char`.
19471948

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

1951-
If a bytes object is specified as first argument, the buffer is made one item
1952-
larger than its length so that the last element in the array is a NUL
1953-
termination character. An integer can be passed as second argument which allows
1954-
specifying the size of the array if the length of the bytes should not be used.
1952+
If the *init* argument is given, it must be :class:`bytes`. It is used
1953+
to initialize the array items. Bytes not initialized this way are
1954+
set to zero (NUL).
1955+
1956+
If *size* is not given (or if it is ``None``), the buffer is made one element
1957+
larger than *init*, effectively adding a NUL terminator.
1958+
1959+
If both arguments are given, *size* must not be less than ``len(init)``.
1960+
1961+
.. warning::
1962+
1963+
If *size* is equal to ``len(init)``, a NUL terminator is
1964+
not added. Do not treat such a buffer as a C string.
1965+
1966+
For example::
1967+
1968+
>>> bytes(create_string_buffer(2))
1969+
b'\x00\x00'
1970+
>>> bytes(create_string_buffer(b'ab'))
1971+
b'ab\x00'
1972+
>>> bytes(create_string_buffer(b'ab', 2))
1973+
b'ab'
1974+
>>> bytes(create_string_buffer(b'ab', 4))
1975+
b'ab\x00\x00'
1976+
>>> bytes(create_string_buffer(b'abcdef', 2))
1977+
Traceback (most recent call last):
1978+
...
1979+
ValueError: byte string too long
19551980

19561981
.. audit-event:: ctypes.create_string_buffer init,size ctypes.create_string_buffer
19571982

19581983

1959-
.. function:: create_unicode_buffer(init_or_size, size=None)
1984+
.. function:: create_unicode_buffer(init, size=None)
1985+
create_unicode_buffer(size)
19601986

19611987
This function creates a mutable unicode character buffer. The returned object is
19621988
a ctypes array of :class:`c_wchar`.
19631989

1964-
*init_or_size* must be an integer which specifies the size of the array, or a
1965-
string which will be used to initialize the array items.
1966-
1967-
If a string is specified as first argument, the buffer is made one item
1968-
larger than the length of the string so that the last element in the array is a
1969-
NUL termination character. An integer can be passed as second argument which
1970-
allows specifying the size of the array if the length of the string should not
1971-
be used.
1990+
The function takes the same arguments as :func:`~create_string_buffer` except
1991+
*init* must be a string and *size* counts :class:`c_wchar`.
19721992

19731993
.. audit-event:: ctypes.create_unicode_buffer init,size ctypes.create_unicode_buffer
19741994

0 commit comments

Comments
 (0)