Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit abd887d

Browse files
author
Stefan Krah
committed
Issue #14181: Improve clarity in the documentation for the multi-purpose
Py_buffer.obj field.
1 parent 2d62798 commit abd887d

2 files changed

Lines changed: 45 additions & 23 deletions

File tree

Doc/c-api/buffer.rst

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,23 @@ allows them to be created and copied very simply. When a generic wrapper
8181
around a buffer is needed, a :ref:`memoryview <memoryview-objects>` object
8282
can be created.
8383

84+
For short instructions how to write an exporting object, see
85+
:ref:`Buffer Object Structures <buffer-structs>`. For obtaining
86+
a buffer, see :c:func:`PyObject_GetBuffer`.
8487

8588
.. c:type:: Py_buffer
8689
8790
.. c:member:: void \*obj
8891
89-
A new reference to the exporting object or *NULL*. The reference is owned
90-
by the consumer and automatically decremented and set to *NULL* by
91-
:c:func:`PyBuffer_Release`.
92+
A new reference to the exporting object. The reference is owned by
93+
the consumer and automatically decremented and set to *NULL* by
94+
:c:func:`PyBuffer_Release`. The field is the equivalent of the return
95+
value of any standard C-API function.
9296

93-
For temporary buffers that are wrapped by :c:func:`PyMemoryView_FromBuffer`
94-
this field must be *NULL*.
97+
As a special case, for *temporary* buffers that are wrapped by
98+
:c:func:`PyMemoryView_FromBuffer` or :c:func:`PyBuffer_FillInfo`
99+
this field is *NULL*. In general, exporting objects MUST NOT
100+
use this scheme.
95101

96102
.. c:member:: void \*buf
97103
@@ -423,7 +429,9 @@ Buffer-related functions
423429
return -1.
424430
425431
On success, fill in *view*, set :c:member:`view->obj` to a new reference
426-
to *exporter* and return 0.
432+
to *exporter* and return 0. In the case of chained buffer providers
433+
that redirect requests to a single object, :c:member:`view->obj` MAY
434+
refer to this object instead of *exporter* (See :ref:`Buffer Object Structures <buffer-structs>`).
427435
428436
Successful calls to :c:func:`PyObject_GetBuffer` must be paired with calls
429437
to :c:func:`PyBuffer_Release`, similar to :c:func:`malloc` and :c:func:`free`.

Doc/c-api/typeobj.rst

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,18 +1213,29 @@ Buffer Object Structures
12131213
int (PyObject *exporter, Py_buffer *view, int flags);
12141214

12151215
Handle a request to *exporter* to fill in *view* as specified by *flags*.
1216-
A standard implementation of this function will take these steps:
1216+
Except for point (3), an implementation of this function MUST take these
1217+
steps:
12171218

1218-
- Check if the request can be met. If not, raise :c:data:`PyExc_BufferError`,
1219-
set :c:data:`view->obj` to *NULL* and return -1.
1219+
(1) Check if the request can be met. If not, raise :c:data:`PyExc_BufferError`,
1220+
set :c:data:`view->obj` to *NULL* and return -1.
12201221

1221-
- Fill in the requested fields.
1222+
(2) Fill in the requested fields.
12221223

1223-
- Increment an internal counter for the number of exports.
1224+
(3) Increment an internal counter for the number of exports.
12241225

1225-
- Set :c:data:`view->obj` to *exporter* and increment :c:data:`view->obj`.
1226+
(4) Set :c:data:`view->obj` to *exporter* and increment :c:data:`view->obj`.
12261227

1227-
- Return 0.
1228+
(5) Return 0.
1229+
1230+
If *exporter* is part of a chain or tree of buffer providers, two main
1231+
schemes can be used:
1232+
1233+
* Re-export: Each member of the tree acts as the exporting object and
1234+
sets :c:data:`view->obj` to a new reference to itself.
1235+
1236+
* Redirect: The buffer request is redirected to the root object of the
1237+
tree. Here, :c:data:`view->obj` will be a new reference to the root
1238+
object.
12281239

12291240
The individual fields of *view* are described in section
12301241
:ref:`Buffer structure <buffer-structure>`, the rules how an exporter
@@ -1233,8 +1244,9 @@ Buffer Object Structures
12331244

12341245
All memory pointed to in the :c:type:`Py_buffer` structure belongs to
12351246
the exporter and must remain valid until there are no consumers left.
1236-
:c:member:`~Py_buffer.shape`, :c:member:`~Py_buffer.strides`,
1237-
:c:member:`~Py_buffer.suboffsets` and :c:member:`~Py_buffer.internal`
1247+
:c:member:`~Py_buffer.format`, :c:member:`~Py_buffer.shape`,
1248+
:c:member:`~Py_buffer.strides`, :c:member:`~Py_buffer.suboffsets`
1249+
and :c:member:`~Py_buffer.internal`
12381250
are read-only for the consumer.
12391251

12401252
:c:func:`PyBuffer_FillInfo` provides an easy way of exposing a simple
@@ -1250,21 +1262,23 @@ Buffer Object Structures
12501262
void (PyObject *exporter, Py_buffer *view);
12511263

12521264
Handle a request to release the resources of the buffer. If no resources
1253-
need to be released, this field may be *NULL*. A standard implementation
1254-
of this function will take these steps:
1265+
need to be released, :c:member:`PyBufferProcs.bf_releasebuffer` may be
1266+
*NULL*. Otherwise, a standard implementation of this function will take
1267+
these optional steps:
12551268

1256-
- Decrement an internal counter for the number of exports.
1269+
(1) Decrement an internal counter for the number of exports.
12571270

1258-
- If the counter is 0, free all memory associated with *view*.
1271+
(2) If the counter is 0, free all memory associated with *view*.
12591272

12601273
The exporter MUST use the :c:member:`~Py_buffer.internal` field to keep
1261-
track of buffer-specific resources (if present). This field is guaranteed
1262-
to remain constant, while a consumer MAY pass a copy of the original buffer
1263-
as the *view* argument.
1274+
track of buffer-specific resources. This field is guaranteed to remain
1275+
constant, while a consumer MAY pass a copy of the original buffer as the
1276+
*view* argument.
12641277

12651278

12661279
This function MUST NOT decrement :c:data:`view->obj`, since that is
1267-
done automatically in :c:func:`PyBuffer_Release`.
1280+
done automatically in :c:func:`PyBuffer_Release` (this scheme is
1281+
useful for breaking reference cycles).
12681282

12691283

12701284
:c:func:`PyBuffer_Release` is the interface for the consumer that

0 commit comments

Comments
 (0)