Nick Coghlan added the comment:
The complexity you're hitting here is the main reason I'm a fan of creating a
dedicated library for dealing with these problems, rather than trying to handle
them directly on the builtins.
Given a bufferlib module, for example, you could have a higher level API like:
def snapshot(source, *, result_type=bytes):
...
That handled any source object that supported the buffer protocol, and any
target type that accepted a memoryview instance as input to the constructor.
# Default bytes snapshot
data = snapshot(original)
# Mutable snapshot without copying and promptly releasing the view
data = snapshot(original, result_type=bytearray)
The start/stop/step or length+offset question could be handled at that level by
allowing both, but also offering lower level APIs with less argument processing
overhead:
def snapshot_slice(source, start, stop, step=1, *, result_type=bytes):
...
def snapshot_at(source, *, offset=0, count=None, result_type=bytes):
...
----------
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue29178>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com