[Python-Dev] When to use EOFError?
random832 at fastmail.com
Wed Jun 22 10:31:27 EDT 2016
On Tue, Jun 21, 2016, at 16:48, 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?
I think this is the most appropriate. If the calling code needs to know
the original reason it can find it in __cause__.
My instinct, though, (and I'm aware that others may not agree, but I
thought it was worth bringing up) is that loads should actually always
raise a ValueError, i.e. my mental model of loads is like:
f = BytesIO(s)
except UnpicklingError as e:
raise ValueError from e
More information about the Python-Dev