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: io close() swallowing exceptions
Type: behavior Stage: resolved
Components: IO Versions: Python 3.1, Python 3.2, Python 2.7, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ajaksu2, amaury.forgeotdarc, giampaolo.rodola, pakal, pitrou, vstinner
Priority: normal Keywords: patch

Created on 2010-02-06 12:27 by pakal, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_error_close.py pakal, 2010-02-06 12:27
no_swallow_on_close3.patch pakal, 2010-04-29 19:50
Messages (16)
msg98940 - (view) Author: Pascal Chambon (pakal) * Date: 2010-02-06 12:27
The current semantic of io streams is to swallow IOErrors on close(), eg. in _pyio from the trunk, we have each time constructs like:
try:
     self.flush()
except IOError:
     pass  # If flush() fails, just give up

and in C files :
        /* If flush() fails, just give up */
        if (PyErr_ExceptionMatches(PyExc_IOError))
            PyErr_Clear();

I'd rather advocate letting exceptions flow, as users have the right to know if their io operations lost bytes.
Below is a test case for these swallowed exceptions.


PS : issues like http://bugs.python.org/issue7640 are actually much more crucial, so we shall let them higher priority
msg102673 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2010-04-09 03:13
Wouldn't this break code that currently works?
msg102710 - (view) Author: Pascal Chambon (pakal) * Date: 2010-04-09 12:00
Well, it would break code which currently ignores that it fails, so it's more a benefit than a loss for programmers imo.

I doubt the impact will be important though, because the io module is still quite recent, and furthermore errors on the last flush are quite unlikely to happen if previous ones succeeded (except "disk full", I don't see any reason for this to happen).
msg102715 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-04-09 12:37
You're right that silencing IO errors is bad.  Patch welcome.
msg103970 - (view) Author: Pascal Chambon (pakal) * Date: 2010-04-22 14:07
Patch and test to stop swallowing exceptions on stream close(), for python SVN trunk.
msg104121 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-04-24 19:19
The tests should probably check all three types of I/O (raw, buffered, text).
msg104143 - (view) Author: Pascal Chambon (pakal) * Date: 2010-04-25 07:20
SHould be better this way then B-)
msg104351 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-04-27 19:55
I just tried the patch. One problem is that you are supposed to be able to call close() several times without having it fail:

>>> f = open("LICENSE")
>>> f.close()
>>> f.close()
>>> f = io.open("LICENSE")
>>> f.close()
>>> f.close()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file.

This means your patch should be a little smarter.
msg104415 - (view) Author: Pascal Chambon (pakal) * Date: 2010-04-28 06:49
I'm quite surprised it wasn't already covered by the test suite :S

Anyway I'm quite confused about the semantic which is expected from IO operations...

Should a flush on a closed stream fail (at the moment sometimes it does, sometimes doesn't) ? Why is sometimes ValueError used when I'd rather expect an IOError ?
msg104426 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-04-28 11:22
> I'm quite surprised it wasn't already covered by the test suite :S

Probably an oversight. Do you want to add some tests?

> Should a flush on a closed stream fail (at the moment sometimes it
> does, sometimes doesn't) ?

It probably should, yes.

> Why is sometimes ValueError used when I'd rather expect an IOError ?

Because it's not an IO error at all. No I/O occurs. You are just using
the file wrongly (or the wrong file), hence the ValueError.
msg104441 - (view) Author: Pascal Chambon (pakal) * Date: 2010-04-28 17:23
>Probably an oversight. Do you want to add some tests?

That's WIP

> Because it's not an IO error at all. No I/O occurs. You are just using
the file wrongly (or the wrong file), hence the ValueError.

Then when you try to wrap a non-readable stream into a readable buffered stream (like BufferedRWPair), it should raise a value error as well, but currently it's rather:
"""if not reader.readable(): raise IOError('"reader" argument must be readable.')"""
msg104460 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-04-28 20:08
For what it's worth, I documented the possibility to call close() several times in r80592.

> Then when you try to wrap a non-readable stream into a readable 
> buffered stream (like BufferedRWPair), it should raise a value error as 
> well,

Good point. Unfortunately, it's now a bit late to change this.
(in any case, it's a programming error to do such things and therefore the user will have to fix his/her code, rather than trying to catch the exception)
msg104577 - (view) Author: Pascal Chambon (pakal) * Date: 2010-04-29 19:50
Here is a code/test patch which *should* fix the multiple close() case, and ensure flush() raise an error on closed file. 

All IO test suites pass on my win32 (except test_largefile that I skip due to lack of hdd space).


I've noticed that the detach() semantic was quite different between _pyio and _io, by the way: C code raises valueerror when we try to access the stream after detaching it, whereas python lets attribute errors be raised. But maybe this is not so important, as these are programming errors anyway.

One thing I'm still wondering : why couldn't we obtain these C extension by cythonizing _pyio ? Are there features that cython lacks, or optimization considerations I'm not aware of ? Cython-generated extensions seem soooo easier to maintain...
msg104579 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-04-29 20:16
> But maybe this is not so important, as these are programming errors
> anyway.

Agreed :)

> One thing I'm still wondering : why couldn't we obtain these C
> extension by cythonizing _pyio ? Are there features that cython lacks, 
> or optimization considerations I'm not aware of ? Cython-generated
> extensions seem soooo easier to maintain...

Several reasons:
- we don't want to depend on an external tool such as cython; the interpreter and its most critical modules just need a C compiler and a reasonably standard C library
- the language cython implements is not Python: it is both a superset and (more annoyingly) a subset of Python
- cython will not allow us, I think, to do as many optimizations as we do in the C version of the io library; the algorithms used are not the same as in the Python version, since raw C allows some much more efficient constructs (especially for handling memory buffers)
msg104854 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-05-03 16:59
Thanks for the patch! It was committed in r80720 (trunk), r80721 (2.6), r80722 (py3k), r80723 (3.1).
msg105165 - (view) Author: Pascal Chambon (pakal) * Date: 2010-05-06 21:07
Cool, thanks a lot B-)
History
Date User Action Args
2022-04-11 14:56:57adminsetgithub: 52113
2010-05-06 21:07:11pakalsetmessages: + msg105165
2010-05-03 16:59:08pitrousetstatus: open -> closed
resolution: fixed
messages: + msg104854

stage: patch review -> resolved
2010-04-29 20:16:15pitrousetmessages: + msg104579
2010-04-29 19:50:33pakalsetfiles: + no_swallow_on_close3.patch

messages: + msg104577
2010-04-29 19:34:20pakalsetfiles: - no_swallow_on_close2.patch
2010-04-28 20:37:40giampaolo.rodolasetnosy: + giampaolo.rodola
2010-04-28 20:08:15pitrousetmessages: + msg104460
2010-04-28 17:23:21pakalsetmessages: + msg104441
2010-04-28 11:22:18pitrousetmessages: + msg104426
2010-04-28 06:49:52pakalsetmessages: + msg104415
2010-04-27 19:55:43pitrousetmessages: + msg104351
2010-04-25 07:20:15pakalsetfiles: + no_swallow_on_close2.patch

messages: + msg104143
2010-04-25 07:18:48pakalsetfiles: - release_io_close_exceptions.patch
2010-04-24 19:19:28pitrousetmessages: + msg104121
stage: needs patch -> patch review
2010-04-22 14:11:43vstinnersetnosy: + vstinner
2010-04-22 14:07:33pakalsetfiles: + release_io_close_exceptions.patch
keywords: + patch
messages: + msg103970
2010-04-09 12:37:31pitrousetpriority: low -> normal
nosy: + amaury.forgeotdarc, pitrou
messages: + msg102715

2010-04-09 12:00:45pakalsetmessages: + msg102710
2010-04-09 03:13:48ajaksu2setpriority: low

type: behavior
versions: - Python 2.5
nosy: + ajaksu2

messages: + msg102673
stage: needs patch
2010-02-06 12:27:06pakalcreate