[Python-Dev] When to use EOFError?
André Malo
nd at perlig.de
Wed Jun 22 12:22:49 EDT 2016
* Serhiy Storchaka wrote:
> There is a design question. If you read file in some format or with some
> protocol, and the data is ended unexpectedly, when to use general
> EOFError exception and when to use format/protocol specific exception?
>
> For example when load truncated pickle data, an unpickler can raise
> EOFError, UnpicklingError, ValueError or AttributeError. It is possible
> to avoid ValueError or AttributeError, but what exception should be
> raised instead, EOFError or UnpicklingError? Maybe convert all EOFError
> to UnpicklingError? Or all UnpicklingError caused by unexpectedly ended
> input to EOFError? Or raise EOFError if the input is ended after
> completed opcode, and UnpicklingError if it contains truncated opcode?
I often concatenate multiple pickles into one file. When reading them, it
works like this:
try:
while True:
yield pickle.load(fp)
except EOFError:
pass
In this case the truncation is not really unexpected. Maybe it should
distinguish between truncated-in-the-middle and truncated-because-empty.
(Same goes for marshal)
Cheers,
--
Real programmers confuse Christmas and Halloween because
DEC 25 = OCT 31. -- Unknown
(found in ssl_engine_mutex.c)
More information about the Python-Dev
mailing list