classification
Title: Behavior of operations on a closed file object is not documented correctly
Type: Stage: resolved
Components: Documentation Versions: Python 3.1, Python 2.6
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: pitrou Nosy List: amaury.forgeotdarc, blep, georg.brandl, kirubakaran, ncoghlan, pitrou
Priority: normal Keywords:

Created on 2009-12-26 20:48 by blep, last changed 2012-07-23 09:42 by pitrou. This issue is now closed.

Messages (8)
msg96892 - (view) Author: Baptiste Lepilleur (blep) Date: 2009-12-26 20:48
The io.IOBase class doc says:
"""Note that calling any method (even inquiries) on a closed stream is
undefined. Implementations may raise IOError in this case."""

But the io.IOBase.close() method document says:
"""Once the file is closed, any operation on the file (e.g. reading or
writing) will raise an IOError."""
which unlike the class doc is not conditional about the behavior...

Experimentation (see below) show that I get a ValueError in practice
(python 3.1, but also true for 2.6) with io.BufferedWriter and
io.StringIO objects.

>>> with open( 'dummy', 'wb') as f:
...     pass
...
>>> f.write( b'' )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: write to closed file
>>> f.writable()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file

>>> import io
>>> s = io.StringIO()
>>> s.close()
>>> s.read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file
msg96926 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-12-27 21:57
Should all these ValueErrors be turned into IOErrors?
msg96927 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-12-27 22:00
I think we raise ValueError because that's already what 2.x does with
plain file objects. Also, it's true that it's a programming error (using
a closed file) and not really an "IO error".

Simplest would be to fix the docs for the io module, IMHO.
msg96942 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-12-28 08:14
I agree; though I would wish for a bit finer-grained I/O related
exceptions...
msg96972 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-12-28 22:11
> I agree; though I would wish for a bit finer-grained I/O related
> exceptions...

I agree with that. It probably needs someone to shepherd the idea to
python-dev so as to get it accepted.
msg97123 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2010-01-01 23:28
Note that one of the reasons for the slightly wishy-washy phrasing in
the docs is to give other implementations a bit more freedom in the way
way they handle these error cases.

Agreed that the main reason is the one Antoine gave though - the
ValueError is looking at things from the point of view that the program
passed in a closed file object when an open one was needed.
msg166203 - (view) Author: Kirubakaran Athmanathan (kirubakaran) Date: 2012-07-23 03:16
Looks like this was fixed in 3.3 in the commit 72890:a22d94547570 Should this issue be closed?
msg166216 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-07-23 09:42
Indeed.
History
Date User Action Args
2012-07-23 09:42:09pitrousetstatus: open -> closed
resolution: out of date
messages: + msg166216

stage: resolved
2012-07-23 03:16:43kirubakaransetnosy: + kirubakaran
messages: + msg166203
2010-01-01 23:28:07ncoghlansetnosy: + ncoghlan
messages: + msg97123
2009-12-28 22:11:46pitrousetmessages: + msg96972
2009-12-28 08:14:24georg.brandlsetassignee: georg.brandl -> pitrou
2009-12-28 08:14:13georg.brandlsetmessages: + msg96942
2009-12-27 22:00:29pitrousetmessages: + msg96927
2009-12-27 21:57:04amaury.forgeotdarcsetnosy: + amaury.forgeotdarc, pitrou
messages: + msg96926
2009-12-26 20:49:17blepsettitle: Behavio of operations on a closed file object is not documented correctly -> Behavior of operations on a closed file object is not documented correctly
2009-12-26 20:48:34blepcreate