Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b7a0a52

Browse filesBrowse files
JelleZijlstrahugovkhauntsaninja
authored
gh-102500: Document PEP 688 (#102571)
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com> Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
1 parent 04f6733 commit b7a0a52
Copy full SHA for b7a0a52

File tree

4 files changed

+95
-1
lines changed
Filter options

4 files changed

+95
-1
lines changed

‎Doc/library/collections.abc.rst

Copy file name to clipboardExpand all lines: Doc/library/collections.abc.rst
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ ABC Inherits from Abstract Methods Mi
177177
:class:`AsyncIterable` [1]_ ``__aiter__``
178178
:class:`AsyncIterator` [1]_ :class:`AsyncIterable` ``__anext__`` ``__aiter__``
179179
:class:`AsyncGenerator` [1]_ :class:`AsyncIterator` ``asend``, ``athrow`` ``aclose``, ``__aiter__``, ``__anext__``
180+
:class:`Buffer` [1]_ ``__buffer__``
180181
============================== ====================== ======================= ====================================================
181182

182183

@@ -346,6 +347,13 @@ Collections Abstract Base Classes -- Detailed Descriptions
346347

347348
.. versionadded:: 3.6
348349

350+
.. class:: Buffer
351+
352+
ABC for classes that provide the :meth:`~object.__buffer__` method,
353+
implementing the :ref:`buffer protocol <bufferobjects>`. See :pep:`688`.
354+
355+
.. versionadded:: 3.12
356+
349357
Examples and Recipes
350358
--------------------
351359

‎Doc/library/inspect.rst

Copy file name to clipboardExpand all lines: Doc/library/inspect.rst
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,39 @@ the following flags:
16031603
for any introspection needs.
16041604

16051605

1606+
Buffer flags
1607+
------------
1608+
1609+
.. class:: BufferFlags
1610+
1611+
This is an :class:`enum.IntFlag` that represents the flags that
1612+
can be passed to the :meth:`~object.__buffer__` method of objects
1613+
implementing the :ref:`buffer protocol <bufferobjects>`.
1614+
1615+
The meaning of the flags is explained at :ref:`buffer-request-types`.
1616+
1617+
.. attribute:: BufferFlags.SIMPLE
1618+
.. attribute:: BufferFlags.WRITABLE
1619+
.. attribute:: BufferFlags.FORMAT
1620+
.. attribute:: BufferFlags.ND
1621+
.. attribute:: BufferFlags.STRIDES
1622+
.. attribute:: BufferFlags.C_CONTIGUOUS
1623+
.. attribute:: BufferFlags.F_CONTIGUOUS
1624+
.. attribute:: BufferFlags.ANY_CONTIGUOUS
1625+
.. attribute:: BufferFlags.INDIRECT
1626+
.. attribute:: BufferFlags.CONTIG
1627+
.. attribute:: BufferFlags.CONTIG_RO
1628+
.. attribute:: BufferFlags.STRIDED
1629+
.. attribute:: BufferFlags.STRIDED_RO
1630+
.. attribute:: BufferFlags.RECORDS
1631+
.. attribute:: BufferFlags.RECORDS_RO
1632+
.. attribute:: BufferFlags.FULL
1633+
.. attribute:: BufferFlags.FULL_RO
1634+
.. attribute:: BufferFlags.READ
1635+
.. attribute:: BufferFlags.WRITE
1636+
1637+
.. versionadded:: 3.12
1638+
16061639
.. _inspect-module-cli:
16071640

16081641
Command Line Interface

‎Doc/reference/datamodel.rst

Copy file name to clipboardExpand all lines: Doc/reference/datamodel.rst
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2865,6 +2865,47 @@ a :exc:`TypeError`.
28652865
The specification for the Python ``match`` statement.
28662866

28672867

2868+
.. _python-buffer-protocol:
2869+
2870+
Emulating buffer types
2871+
----------------------
2872+
2873+
The :ref:`buffer protocol <bufferobjects>` provides a way for Python
2874+
objects to expose efficient access to a low-level memory array. This protocol
2875+
is implemented by builtin types such as :class:`bytes` and :class:`memoryview`,
2876+
and third-party libraries may define additional buffer types.
2877+
2878+
While buffer types are usually implemented in C, it is also possible to
2879+
implement the protocol in Python.
2880+
2881+
.. method:: object.__buffer__(self, flags)
2882+
2883+
Called when a buffer is requested from *self* (for example, by the
2884+
:class:`memoryview` constructor). The *flags* argument is an integer
2885+
representing the kind of buffer requested, affecting for example whether
2886+
the returned buffer is read-only or writable. :class:`inspect.BufferFlags`
2887+
provides a convenient way to interpret the flags. The method must return
2888+
a :class:`memoryview` object.
2889+
2890+
.. method:: object.__release_buffer__(self, buffer)
2891+
2892+
Called when a buffer is no longer needed. The *buffer* argument is a
2893+
:class:`memoryview` object that was previously returned by
2894+
:meth:`~object.__buffer__`. The method must release any resources associated
2895+
with the buffer. This method should return ``None``.
2896+
Buffer objects that do not need to perform any cleanup are not required
2897+
to implement this method.
2898+
2899+
.. versionadded:: 3.12
2900+
2901+
.. seealso::
2902+
2903+
:pep:`688` - Making the buffer protocol accessible in Python
2904+
Introduces the Python ``__buffer__`` and ``__release_buffer__`` methods.
2905+
2906+
:class:`collections.abc.Buffer`
2907+
ABC for buffer types.
2908+
28682909
.. _special-lookup:
28692910

28702911
Special method lookup

‎Doc/whatsnew/3.12.rst

Copy file name to clipboardExpand all lines: Doc/whatsnew/3.12.rst
+13-1Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,19 @@ New Features
149149
In Python 3.14, the default will switch to ``'data'``.
150150
(Contributed by Petr Viktorin in :pep:`706`.)
151151

152+
PEP 688: Making the buffer protocol accessible in Python
153+
--------------------------------------------------------
154+
155+
:pep:`688` introduces a way to use the :ref:`buffer protocol <bufferobjects>`
156+
from Python code. Classes that implement the :meth:`~object.__buffer__` method
157+
are now usable as buffer types.
158+
159+
The new :class:`collections.abc.Buffer` ABC provides a standard
160+
way to represent buffer objects, for example in type annotations.
161+
The new :class:`inspect.BufferFlags` enum represents the flags that
162+
can be used to customize buffer creation.
163+
(Contributed by Jelle Zijlstra in :gh:`102500`.)
164+
152165
New Features Related to Type Hints
153166
==================================
154167

@@ -179,7 +192,6 @@ See :pep:`692` for more details.
179192

180193
(PEP written by Franek Magiera)
181194

182-
183195
Other Language Changes
184196
======================
185197

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.