Skip to content

Commit

Permalink
gh-102500: Document PEP 688 (#102571)
Browse files Browse the repository at this point in the history
Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Shantanu <[email protected]>
  • Loading branch information
3 people authored May 4, 2023
1 parent 04f6733 commit b7a0a52
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Doc/library/collections.abc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ ABC Inherits from Abstract Methods Mi
:class:`AsyncIterable` [1]_ ``__aiter__``
:class:`AsyncIterator` [1]_ :class:`AsyncIterable` ``__anext__`` ``__aiter__``
:class:`AsyncGenerator` [1]_ :class:`AsyncIterator` ``asend``, ``athrow`` ``aclose``, ``__aiter__``, ``__anext__``
:class:`Buffer` [1]_ ``__buffer__``
============================== ====================== ======================= ====================================================


Expand Down Expand Up @@ -346,6 +347,13 @@ Collections Abstract Base Classes -- Detailed Descriptions

.. versionadded:: 3.6

.. class:: Buffer

ABC for classes that provide the :meth:`~object.__buffer__` method,
implementing the :ref:`buffer protocol <bufferobjects>`. See :pep:`688`.

.. versionadded:: 3.12

Examples and Recipes
--------------------

Expand Down
33 changes: 33 additions & 0 deletions Doc/library/inspect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,39 @@ the following flags:
for any introspection needs.


Buffer flags
------------

.. class:: BufferFlags

This is an :class:`enum.IntFlag` that represents the flags that
can be passed to the :meth:`~object.__buffer__` method of objects
implementing the :ref:`buffer protocol <bufferobjects>`.

The meaning of the flags is explained at :ref:`buffer-request-types`.

.. attribute:: BufferFlags.SIMPLE
.. attribute:: BufferFlags.WRITABLE
.. attribute:: BufferFlags.FORMAT
.. attribute:: BufferFlags.ND
.. attribute:: BufferFlags.STRIDES
.. attribute:: BufferFlags.C_CONTIGUOUS
.. attribute:: BufferFlags.F_CONTIGUOUS
.. attribute:: BufferFlags.ANY_CONTIGUOUS
.. attribute:: BufferFlags.INDIRECT
.. attribute:: BufferFlags.CONTIG
.. attribute:: BufferFlags.CONTIG_RO
.. attribute:: BufferFlags.STRIDED
.. attribute:: BufferFlags.STRIDED_RO
.. attribute:: BufferFlags.RECORDS
.. attribute:: BufferFlags.RECORDS_RO
.. attribute:: BufferFlags.FULL
.. attribute:: BufferFlags.FULL_RO
.. attribute:: BufferFlags.READ
.. attribute:: BufferFlags.WRITE

.. versionadded:: 3.12

.. _inspect-module-cli:

Command Line Interface
Expand Down
41 changes: 41 additions & 0 deletions Doc/reference/datamodel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2865,6 +2865,47 @@ a :exc:`TypeError`.
The specification for the Python ``match`` statement.


.. _python-buffer-protocol:

Emulating buffer types
----------------------

The :ref:`buffer protocol <bufferobjects>` provides a way for Python
objects to expose efficient access to a low-level memory array. This protocol
is implemented by builtin types such as :class:`bytes` and :class:`memoryview`,
and third-party libraries may define additional buffer types.

While buffer types are usually implemented in C, it is also possible to
implement the protocol in Python.

.. method:: object.__buffer__(self, flags)

Called when a buffer is requested from *self* (for example, by the
:class:`memoryview` constructor). The *flags* argument is an integer
representing the kind of buffer requested, affecting for example whether
the returned buffer is read-only or writable. :class:`inspect.BufferFlags`
provides a convenient way to interpret the flags. The method must return
a :class:`memoryview` object.

.. method:: object.__release_buffer__(self, buffer)

Called when a buffer is no longer needed. The *buffer* argument is a
:class:`memoryview` object that was previously returned by
:meth:`~object.__buffer__`. The method must release any resources associated
with the buffer. This method should return ``None``.
Buffer objects that do not need to perform any cleanup are not required
to implement this method.

.. versionadded:: 3.12

.. seealso::

:pep:`688` - Making the buffer protocol accessible in Python
Introduces the Python ``__buffer__`` and ``__release_buffer__`` methods.

:class:`collections.abc.Buffer`
ABC for buffer types.

.. _special-lookup:

Special method lookup
Expand Down
14 changes: 13 additions & 1 deletion Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,19 @@ New Features
In Python 3.14, the default will switch to ``'data'``.
(Contributed by Petr Viktorin in :pep:`706`.)

PEP 688: Making the buffer protocol accessible in Python
--------------------------------------------------------

:pep:`688` introduces a way to use the :ref:`buffer protocol <bufferobjects>`
from Python code. Classes that implement the :meth:`~object.__buffer__` method
are now usable as buffer types.

The new :class:`collections.abc.Buffer` ABC provides a standard
way to represent buffer objects, for example in type annotations.
The new :class:`inspect.BufferFlags` enum represents the flags that
can be used to customize buffer creation.
(Contributed by Jelle Zijlstra in :gh:`102500`.)

New Features Related to Type Hints
==================================

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

(PEP written by Franek Magiera)


Other Language Changes
======================

Expand Down

0 comments on commit b7a0a52

Please sign in to comment.