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>
This commit is contained in:
Jelle Zijlstra 2023-05-04 08:23:40 -07:00 committed by GitHub
parent 04f6733275
commit b7a0a52196
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 95 additions and 1 deletions

View File

@ -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__``
============================== ====================== ======================= ====================================================
@ -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
--------------------

View File

@ -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

View File

@ -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

View File

@ -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
==================================
@ -179,7 +192,6 @@ See :pep:`692` for more details.
(PEP written by Franek Magiera)
Other Language Changes
======================