[New-bugs-announce] [issue15840] Ambiguity with regard to the effect of accessing a closed IOBase instance

Alexander Konovalenko report at bugs.python.org
Sat Sep 1 20:33:44 CEST 2012

New submission from Alexander Konovalenko:

Two pieces of the documentation for io.IOBase seem to contradict each other:

At http://docs.python.org/library/io.html#io.IOBase:
"Note that calling any method (even inquiries) on a closed stream is undefined. Implementations may raise IOError in this case."

At http://docs.python.org/library/io.html#io.IOBase.close:
"Once the file is closed, any operation on the file (e.g. reading or writing) will raise a ValueError."

Perhaps the confusion arises in part because it is not clear when a requirement for all IOBase implementations is documented and when the docs merely describe the default behavior of IOBase that implementations are free to override.

Those passages are inconsistent on two points:

1) IOError and ValueError are not subclasses one of another. So what should we expect an IOBase implementation to raise?

2) Undefined behavior means literally anything can happen. Technically, that means calling read() or readable() on a closed file (or other kind of stream) could wreak havoc in a totally unrelated part of the system or segfault the Python interpreter. That is definitely a thing to carefully avoid in any production systems.

On the other hand, raising an exception, even if we don't know in advance whether it will be ValueError or IOError, is pretty much defined behavior. An exception is easy to deal with and to contain inside a module.

So please clarify how dangerous it actually is to access an IOBase stream after it has been closed.

assignee: docs at python
components: Documentation
messages: 169660
nosy: alexkon, docs at python
priority: normal
severity: normal
status: open
title: Ambiguity with regard to the effect of accessing a closed IOBase instance
versions: Python 2.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list