[issue7578] Behavio of operations on a closed file object is not documented correctly

Baptiste Lepilleur report at bugs.python.org
Sat Dec 26 21:48:36 CET 2009


New submission from Baptiste Lepilleur <blep at users.sourceforge.net>:

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

----------
assignee: georg.brandl
components: Documentation
messages: 96892
nosy: blep, georg.brandl
severity: normal
status: open
title: Behavio of operations on a closed file object is not documented correctly
versions: Python 2.6, Python 3.1

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7578>
_______________________________________


More information about the Python-bugs-list mailing list