[Python-ideas] struct.unpack should support open files

Nathaniel Smith njs at pobox.com
Wed Dec 26 06:10:05 EST 2018


On Wed, Dec 26, 2018, 02:19 Andrew Svetlov <andrew.svetlov at gmail.com wrote:

>
> Also I'm thinking about type annotations in typeshed.
> Now the type is Union[array[int], bytes, bytearray, memoryview]
> Should it be Union[io.BinaryIO, array[int], bytes, bytearray, memoryview] ?
>

Yeah, trying to support both buffers and file-like objects in the same
function seems like a clearly bad idea. If we do this at all it should be
by adding new convenience functions/methods that take file-like objects
exclusively, like the ones several people posted on the thread.

I don't really have an opinion on whether this is worth doing at all. I
guess I can think of some arguments against: Packing/unpacking multiple
structs to the same file-like object may be less efficient than using a
single buffer + a single call to read/write. And it's unfortunate that the
obvious pack_into/unpack_from names are already taken. And it's only 2
lines of code to write your own helpers. But none of these are particularly
strong arguments either, and clearly some people would find them handy.

-n
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20181226/e4ddd96b/attachment.html>


More information about the Python-ideas mailing list