[Python-ideas] Use unbound bytes methods with objects supporting the buffer protocol

eryk sun eryksun at gmail.com
Thu Jul 14 12:31:40 EDT 2016


On Thu, Jul 14, 2016 at 8:52 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Given the way this would behave if "bytes" was implemented in Python
> rather than C (i.e. unbound methods would rely on ducktyping, even for
> the first argument), +1 from me for making the unbound methods for
> bytes compatible with arbitrary objects supporting the buffer
> protocol.

The buffer protocol is a bit generic for duck typing. Instead the
bytes methods could check for a memoryview with a format that's "B" or
"b".

    >>> a = np.array([1,2,3,4], dtype='int16')
    >>> b = np.array([1,2,3,4], dtype='uint8')
    >>> memoryview(a).format
    'h'
    >>> memoryview(b).format
    'B'

It's possible to cast if necessary, e.g. memoryview(a).cast('B'). No
copy of the data is made, so it's still reasonably efficient. This
preserves raising a TypeError for operations that are generally
nonsensical, such as attempting to split() an array of short integers
as if it's just bytes.


More information about the Python-ideas mailing list