summaryrefslogtreecommitdiffstats
path: root/Doc/reference/datamodel.rst
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2023-05-04 15:23:40 (GMT)
committerGitHub <noreply@github.com>2023-05-04 15:23:40 (GMT)
commitb7a0a521960a6e9ea46b79b51cbcc3c4ffcc7057 (patch)
treeb77c5170d60ab85bf3cba18b6dbdf09b38c77425 /Doc/reference/datamodel.rst
parent04f673327530f47f002e784459037231de478412 (diff)
downloadcpython-b7a0a521960a6e9ea46b79b51cbcc3c4ffcc7057.zip
cpython-b7a0a521960a6e9ea46b79b51cbcc3c4ffcc7057.tar.gz
cpython-b7a0a521960a6e9ea46b79b51cbcc3c4ffcc7057.tar.bz2
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>
Diffstat (limited to 'Doc/reference/datamodel.rst')
-rw-r--r--Doc/reference/datamodel.rst41
1 files changed, 41 insertions, 0 deletions
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
index 1294d68..0a9cabc 100644
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -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