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

Serhiy Storchaka storchaka at gmail.com
Thu Jul 14 14:14:41 EDT 2016


On 14.07.16 19:31, eryk sun wrote:
> 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.

This looks reasonable. But for now bytes methods accept arbitrary buffers.

     >>> a = np.array([1,2,3,4], dtype='int16')
     >>> b = np.array([1,2,3,4], dtype='uint8')
     >>> b'.'.join([a, b])
     b'\x01\x00\x02\x00\x03\x00\x04\x00.\x01\x02\x03\x04'




More information about the Python-ideas mailing list