Skip to content

Commit

Permalink
Let ctypes._CData.from_buffer[_copy] accept any buffer object (#2610)
Browse files Browse the repository at this point in the history
Fixes the issue discussed here:
#1906 (comment)
  • Loading branch information
dgelessus authored and JelleZijlstra committed Nov 20, 2018
1 parent cd75801 commit 45d4e88
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions stdlib/2and3/ctypes/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Stubs for ctypes

from array import array
from typing import (
Any, Callable, ClassVar, Iterator, Iterable, List, Mapping, Optional, Sequence, Sized, Text,
Tuple, Type, Generic, TypeVar, overload,
Expand Down Expand Up @@ -45,6 +46,13 @@ if sys.platform == 'win32':
pydll: LibraryLoader[PyDLL] = ...
pythonapi: PyDLL = ...

# Anything that implements the read-write buffer interface.
# The buffer interface is defined purely on the C level, so we cannot define a normal Protocol
# for it. Instead we have to list the most common stdlib buffer classes in a Union.
_WritableBuffer = _UnionT[bytearray, memoryview, array, _CData]
# Same as _WritableBuffer, but also includes read-only buffer types (like bytes).
_ReadOnlyBuffer = _UnionT[_WritableBuffer, bytes]

class _CDataMeta(type):
# By default mypy complains about the following two methods, because strictly speaking cls
# might not be a Type[_CT]. However this can never actually happen, because the only class that
Expand All @@ -56,9 +64,9 @@ class _CData(metaclass=_CDataMeta):
_b_needsfree_: bool = ...
_objects: Optional[Mapping[Any, int]] = ...
@classmethod
def from_buffer(cls: Type[_CT], source: bytearray, offset: int = ...) -> _CT: ...
def from_buffer(cls: Type[_CT], source: _WritableBuffer, offset: int = ...) -> _CT: ...
@classmethod
def from_buffer_copy(cls: Type[_CT], source: bytearray, offset: int = ...) -> _CT: ...
def from_buffer_copy(cls: Type[_CT], source: _ReadOnlyBuffer, offset: int = ...) -> _CT: ...
@classmethod
def from_address(cls: Type[_CT], address: int) -> _CT: ...
@classmethod
Expand Down

0 comments on commit 45d4e88

Please sign in to comment.