diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py index 0d86078..b768cb4 100644 --- a/Lib/ctypes/__init__.py +++ b/Lib/ctypes/__init__.py @@ -139,6 +139,9 @@ from _ctypes import sizeof, byref, addressof, alignment, resize from _ctypes import get_errno, set_errno from _ctypes import _SimpleCData +from typing import Buffer +Buffer.register(_SimpleCData) + def _check_size(typ, typecode=None): # Check if sizeof(ctypes_type) against struct.calcsize. This # should protect somewhat against a misconfigured libffi. diff --git a/Lib/io.py b/Lib/io.py index e03db97..46de8ec 100644 --- a/Lib/io.py +++ b/Lib/io.py @@ -50,6 +50,7 @@ __all__ = ["BlockingIOError", "open", "IOBase", "RawIOBase", "FileIO", import _io import abc +import typing from _io import (DEFAULT_BUFFER_SIZE, BlockingIOError, UnsupportedOperation, open, FileIO, BytesIO, StringIO, BufferedReader, @@ -90,3 +91,5 @@ for klass in (BytesIO, BufferedReader, BufferedWriter, BufferedRandom, for klass in (StringIO, TextIOWrapper): TextIOBase.register(klass) del klass + +typing.Buffer.register(BytesIO) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index a7f8dd5..be28f3e 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -596,6 +596,19 @@ class ProtocolTests(BaseTestCase): with self.assertRaises(TypeError): isinstance(0, typing.SupportsAbs) + def test_buffer(self): + from io import BytesIO + from ctypes import _SimpleCData + from mmap import mmap + from array import array + self.assertIsSubclass(bytes, typing.Buffer) + self.assertIsSubclass(bytearray, typing.Buffer) + self.assertIsSubclass(memoryview, typing.Buffer) + self.assertIsSubclass(BytesIO, typing.Buffer) + self.assertIsSubclass(_SimpleCData, typing.Buffer) + self.assertIsSubclass(mmap.mmap, typing.Buffer) + self.assertIsSubclass(array, typing.Buffer) + class GenericTests(BaseTestCase): diff --git a/Lib/typing.py b/Lib/typing.py index 4cac66c..e4d65e8 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -46,6 +46,7 @@ __all__ = [ 'ValuesView', # Structural checks, a.k.a. protocols. + 'Buffer', 'Reversible', 'SupportsAbs', 'SupportsFloat', @@ -1359,6 +1360,17 @@ else: AsyncIterator = None +class Buffer(metaclass=abc.ABCMeta): + """Objects supporting the PEP-3118 buffer interface""" + __slots__ = () + # Non generic + # No defined python API (buffer object interface is defined at a C level) + +Buffer.register(type(b'')) +Buffer.register(type(bytearray())) +Buffer.register(type(memoryview(b''))) +# TODO: array module, mmap module, + class Iterable(Generic[T_co], extra=collections_abc.Iterable): __slots__ = ()