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

Nick Coghlan ncoghlan at gmail.com
Thu Jul 14 04:52:19 EDT 2016

On 14 July 2016 at 07:09, Terry Reedy <tjreedy at udel.edu> wrote:
> Assuming that the descriptor check is not just an unintened holdover from
> 2.x, it seems that for C-coded functions used as methods, type-checking the
> first arg was conceptually factored out and replaced by a generic check in
> the descriptor mechanism.

It's intentional - the default C level descriptors typecheck their
first argument, since getting that wrong may cause a segfault in most

> If the descriptor check cannot be selectively loosened, a possible solution
> might be a base class for all bytes-like buffer protocol classes that would
> have all method functions that work with all bytes-like objects.

A custom wrapper descriptor that checks for "supports the buffer
protocol" rather than "is a bytes-like object" is certainly possible,
so I believe Serhiy's question here is more a design question around
"Should they?" than it is a technical question around "Can they?".

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


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list