This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: close() seems to have limited effect
Type: behavior Stage: resolved
Components: Extension Modules, Library (Lib) Versions: Python 3.0
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, christian.heimes, hdima, pitrou, skip.montanaro
Priority: release blocker Keywords: patch

Created on 2008-12-09 00:11 by skip.montanaro, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
iobug.diff skip.montanaro, 2008-12-09 21:00
io.patch skip.montanaro, 2008-12-09 21:10
io_fixes.patch hdima, 2008-12-26 13:16 io module fixes with test
Messages (9)
msg77376 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2008-12-09 00:11
Seems like file.close() in 3.0 isn't much of a barrier to further reading:

% python3.0
Python 3.0rc3+ (py3k:67338M, Nov 22 2008, 06:47:23) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> fp = open("/etc/services")
>>> ct = fp.read(1048)
>>> print (ct[-80:], end='') ; fp.close() ; ct = fp.read(17) ; print (ct)
compressnet       2/udp     # Management Utility
compressnet       2/tcp     # Management Utility

The second read() should raise an exception.  Same code, 2.6:

% python2.6
Python 2.6+ (release26-maint:66821:66833, Oct 30 2008, 22:16:1) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import print_function
>>> fp = open("/etc/services")
>>> ct = fp.read(1048)
>>> print (ct[-80:], end='') ; fp.close() ; ct = fp.read(17) ; print (ct)
compressnet       2/udp     # Management Utility
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file
compressnet       2/tcp     # M>>> 

Culled this from a posting to comp.lang.python.
msg77440 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2008-12-09 18:45
Indeed, it seems to happen if you first call read() before calling close().
msg77446 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2008-12-09 20:55
Here's a test case (diff against Lib/test/test_io.py).  This fails for
me on OS X 10.5.5 with the tip of the py3k branch.
msg77450 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2008-12-09 21:00
fixed patch.  I'm still a bit clumsy with the assertRaises stuff.
msg77455 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2008-12-09 21:10
Here's a minimal patch to BufferedReader.read() which causes the test
to pass.  I will leave it for smarter people to decided whether or not
all the other read() methods need the same test.
msg77475 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-12-10 00:04
- The patch corrects a specific case, but not others: for example if the 
file is opened in "r" (text) mode.
There are also other methods to test: peek(), read1()...

- It should use
   self._checkClosed()
which already raises the same exception with the same message.
msg77476 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2008-12-10 01:31
Amaury> - It should use
    Amaury>    self._checkClosed()
    Amaury> which already raises the same exception with the same message.

I think some other places will need this change then.

Note that I don't know the io code at all.  I was just trying to provide a
simple test case and a simple fix.  Feel free to generalize it as necessary.

Skip
msg78295 - (view) Author: Dmitry Vasiliev (hdima) Date: 2008-12-26 13:16
Attached more generic version of the patch.
msg79498 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-01-09 19:59
Expanded the test a bit and committed the patch in r68454. Thanks!
History
Date User Action Args
2022-04-11 14:56:42adminsetgithub: 48854
2009-01-09 20:08:25pitrousetstatus: pending -> closed
resolution: accepted -> fixed
2009-01-09 19:59:10pitrousetstatus: open -> pending
messages: + msg79498
versions: - Python 3.1
resolution: accepted
stage: patch review -> resolved
2008-12-26 13:17:04hdimasetfiles: + io_fixes.patch
nosy: + hdima
messages: + msg78295
2008-12-20 02:41:10loewissetpriority: deferred blocker -> release blocker
2008-12-10 08:25:32loewissetpriority: release blocker -> deferred blocker
2008-12-10 01:31:51skip.montanarosetmessages: + msg77476
2008-12-10 00:04:00amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg77475
2008-12-09 21:12:36skip.montanarosetstage: needs patch -> patch review
2008-12-09 21:10:22skip.montanarosetfiles: - iobug.diff
2008-12-09 21:10:16skip.montanarosetfiles: + io.patch
messages: + msg77455
2008-12-09 21:00:32skip.montanarosetfiles: + iobug.diff
messages: + msg77450
2008-12-09 20:55:13skip.montanarosetstage: test needed -> needs patch
2008-12-09 20:55:03skip.montanarosetfiles: + iobug.diff
keywords: + patch
messages: + msg77446
2008-12-09 18:45:39pitrousetnosy: + pitrou
messages: + msg77440
2008-12-09 01:55:47christian.heimessetpriority: release blocker
nosy: + christian.heimes
stage: test needed
components: + Extension Modules, Library (Lib), - Interpreter Core
versions: + Python 3.1
2008-12-09 00:11:20skip.montanarocreate