@@ -34,39 +34,24 @@ These formats allow accessing an object as a contiguous chunk of memory.
34
34
You don't have to provide raw storage for the returned unicode or bytes
35
35
area.
36
36
37
- Unless otherwise stated, buffers are not NUL-terminated.
38
-
39
- There are three ways strings and buffers can be converted to C:
40
-
41
- * Formats such as ``y* `` and ``s* `` fill a :c:type: `Py_buffer ` structure.
42
- This locks the underlying buffer so that the caller can subsequently use
43
- the buffer even inside a :c:type: `Py_BEGIN_ALLOW_THREADS `
44
- block without the risk of mutable data being resized or destroyed.
45
- As a result, **you have to call ** :c:func: `PyBuffer_Release ` after you have
46
- finished processing the data (or in any early abort case).
47
-
48
- * The ``es ``, ``es# ``, ``et `` and ``et# `` formats allocate the result buffer.
49
- **You have to call ** :c:func: `PyMem_Free ` after you have finished
50
- processing the data (or in any early abort case).
37
+ In general, when a format sets a pointer to a buffer, the buffer is
38
+ managed by the corresponding Python object, and the buffer shares
39
+ the lifetime of this object. You won't have to release any memory yourself.
40
+ The only exceptions are ``es ``, ``es# ``, ``et `` and ``et# ``.
41
+
42
+ However, when a :c:type: `Py_buffer ` structure gets filled, the underlying
43
+ buffer is locked so that the caller can subsequently use the buffer even
44
+ inside a :c:type: `Py_BEGIN_ALLOW_THREADS ` block without the risk of mutable data
45
+ being resized or destroyed. As a result, **you have to call **
46
+ :c:func: `PyBuffer_Release ` after you have finished processing the data (or
47
+ in any early abort case).
51
48
52
- * .. _c-arg-borrowed-buffer:
53
-
54
- Other formats take a :class: `str ` or a read-only :term: `bytes-like object `,
55
- such as :class: `bytes `, and provide a ``const char * `` pointer to
56
- its buffer.
57
- In this case the buffer is "borrowed": it is managed by the corresponding
58
- Python object, and shares the lifetime of this object.
59
- You won't have to release any memory yourself.
60
-
61
- To ensure that the underlying buffer may be safely borrowed, the object's
62
- :c:member: `PyBufferProcs.bf_releasebuffer ` field must be ``NULL ``.
63
- This disallows common mutable objects such as :class: `bytearray `,
64
- but also some read-only objects such as :class: `memoryview ` of
65
- :class: `bytes `.
49
+ Unless otherwise stated, buffers are not NUL-terminated.
66
50
67
- Besides this ``bf_releasebuffer `` requirement, there is no check to verify
68
- whether the input object is immutable (e.g. whether it would honor a request
69
- for a writable buffer, or whether another thread can mutate the data).
51
+ Some formats require a read-only :term: `bytes-like object `, and set a
52
+ pointer instead of a buffer structure. They work by checking that
53
+ the object's :c:member: `PyBufferProcs.bf_releasebuffer ` field is ``NULL ``,
54
+ which disallows mutable objects such as :class: `bytearray `.
70
55
71
56
.. note ::
72
57
@@ -104,7 +89,7 @@ There are three ways strings and buffers can be converted to C:
104
89
Unicode objects are converted to C strings using ``'utf-8' `` encoding.
105
90
106
91
``s# `` (:class: `str `, read-only :term: `bytes-like object `) [const char \* , :c:type: `Py_ssize_t `]
107
- Like ``s* ``, except that it provides a :ref: ` borrowed buffer < c-arg-borrowed-buffer >` .
92
+ Like ``s* ``, except that it doesn't accept mutable objects .
108
93
The result is stored into two C variables,
109
94
the first one a pointer to a C string, the second one its length.
110
95
The string may contain embedded null bytes. Unicode objects are converted
@@ -123,9 +108,8 @@ There are three ways strings and buffers can be converted to C:
123
108
pointer is set to ``NULL ``.
124
109
125
110
``y `` (read-only :term: `bytes-like object `) [const char \* ]
126
- This format converts a bytes-like object to a C pointer to a
127
- :ref: `borrowed <c-arg-borrowed-buffer >` character string;
128
- it does not accept Unicode objects. The bytes buffer must not
111
+ This format converts a bytes-like object to a C pointer to a character
112
+ string; it does not accept Unicode objects. The bytes buffer must not
129
113
contain embedded null bytes; if it does, a :exc: `ValueError `
130
114
exception is raised.
131
115
0 commit comments