[Python-ideas] struct.unpack should support open files
Steven D'Aprano
steve at pearwood.info
Mon Dec 24 08:33:14 EST 2018
On Mon, Dec 24, 2018 at 03:01:07PM +0200, Andrew Svetlov wrote:
> Handling files overcomplicates both implementation and mental space for API
> saving.
Perhaps. Although the implementation doesn't seem that complicated, and
the mental space for the API not that much more difficult:
unpack from bytes, or read from a file;
versus
unpack from bytes, which you might read from a file
Seems about the same to me, except that with the proposal you don't have
to calculate the size of the struct before reading.
I haven't thought about this very deeply, but at first glance, I like
Drew's idea of being able to just pass an open file to unpack and have
it read from the file.
> Files can be opened in text mode, what to do in this case? What
> exception should be raised?
That is easy to answer: the same exception you get if you pass text to
unpack() when it is expecting bytes:
py> struct.unpack(fmt, "a")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
There should be no difference whether the text comes from a literal, a
variable, or is read from a file.
> How to handle OS errors?
unpack() shouldn't try to handle them. If an OS error occurs, raise an
exception, exactly the same way file.read() would raise an exception.
--
Steve
More information about the Python-ideas
mailing list