@@ -1940,35 +1940,55 @@ Utility functions
1940
1940
pointer.
1941
1941
1942
1942
1943
- .. function :: create_string_buffer(init_or_size, size=None)
1943
+ .. function :: create_string_buffer(init, size=None)
1944
+ create_string_buffer(size)
1944
1945
1945
1946
This function creates a mutable character buffer. The returned object is a
1946
1947
ctypes array of :class: `c_char `.
1947
1948
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 .
1950
1951
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
1955
1980
1956
1981
.. audit-event :: ctypes.create_string_buffer init,size ctypes.create_string_buffer
1957
1982
1958
1983
1959
- .. function :: create_unicode_buffer(init_or_size, size=None)
1984
+ .. function :: create_unicode_buffer(init, size=None)
1985
+ create_unicode_buffer(size)
1960
1986
1961
1987
This function creates a mutable unicode character buffer. The returned object is
1962
1988
a ctypes array of :class: `c_wchar `.
1963
1989
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 `.
1972
1992
1973
1993
.. audit-event :: ctypes.create_unicode_buffer init,size ctypes.create_unicode_buffer
1974
1994
0 commit comments