[issue29178] Adding bytes.frombuffer(byteslike) constructor

Serhiy Storchaka report at bugs.python.org
Fri Jan 6 17:29:08 EST 2017


Serhiy Storchaka added the comment:

> Which virtually no one follows :(

Sad. But adding bytes.frombuffer() wouldn't make it magically used. If you are 
aware of the problem, you can use the above two-liner as well as 
bytes.frombuffer(). You even can use it in the current code with older Python 
releases, unlike to bytes.frombuffer() which would need 3.7.

> Any protocol parsing code has a lot of slicing.

How much code you expect to update with bytes.frombuffer()? And why not use the 
above two-liner instead?

> > There is also a problem with returned type for subclasses (this is always
> > a problem for alternate constructors).
> Good point. How do we usually solve this in CPython?

It is deemed that returning an instance of a subclass is more preferable. 
Otherwise you could use separate function rather of a class method. But it is 
not easy. You need either pass a memoryview or bytes instance to class 
constructor, or (only for mutable arrays) create an empty instance and 
concatenate a buffer to it.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29178>
_______________________________________


More information about the Python-bugs-list mailing list