[Python-Dev] When to use EOFError?

Steven D'Aprano steve at pearwood.info
Mon Jun 27 20:25:42 EDT 2016

On Mon, Jun 27, 2016 at 03:47:31PM -0700, Ethan Furman wrote:
> On 06/27/2016 03:20 PM, Guido van Rossum wrote:
> >The point is that it's not an error. In Andre Malo's use case, at
> >least, EOFError is used as a control flow exception, not as an error.
> Like StopIteration then: only an error if it escapes.

Well, not quite -- if you're expected four pickles in a file, and get
EOFError after pickle #2, then it's an actual error. But that's up to 
the caller to decide.

EOFError just means there's nothing more to read in a situation where 
returning an empty (byte) string isn't an option. The meaning you give 
to that depends on your expectations.

I think Greg had the right idea: raise a pickle error if you hit EOF in 
the middle of a pickle, because that absolutely means your data is 
corrupt; raise EOFError when you hit EOF at the very beginning of the 
file, or after a complete pickle.


More information about the Python-Dev mailing list