@@ -2031,35 +2031,55 @@ Utility functions
2031
2031
pointer.
2032
2032
2033
2033
2034
- .. function :: create_string_buffer(init_or_size, size=None)
2034
+ .. function :: create_string_buffer(init, size=None)
2035
+ create_string_buffer(size)
2035
2036
2036
2037
This function creates a mutable character buffer. The returned object is a
2037
2038
ctypes array of :class: `c_char `.
2038
2039
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 .
2041
2042
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
2046
2071
2047
2072
.. audit-event :: ctypes.create_string_buffer init,size ctypes.create_string_buffer
2048
2073
2049
2074
2050
- .. function :: create_unicode_buffer(init_or_size, size=None)
2075
+ .. function :: create_unicode_buffer(init, size=None)
2076
+ create_unicode_buffer(size)
2051
2077
2052
2078
This function creates a mutable unicode character buffer. The returned object is
2053
2079
a ctypes array of :class: `c_wchar `.
2054
2080
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 `.
2063
2083
2064
2084
.. audit-event :: ctypes.create_unicode_buffer init,size ctypes.create_unicode_buffer
2065
2085
0 commit comments