Issue 4233: open(0, closefd=False) prints 3 warnings (original) (raw)

Issue4233

Created on 2008-10-30 00:08 by amaury.forgeotdarc, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
fileio_closefd-2.patch vstinner,2008-11-01 01:28
fileio_closefd3.patch christian.heimes,2008-11-01 01:44
fileio_closefd4.patch christian.heimes,2008-11-05 01:23
Messages (13)
msg75337 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-10-30 00:07
This happens with a recent py3k build: >>> x = open(0, closefd=False) >>> del x C:\dev\python\py3k\lib\io.py:1461: RuntimeWarning: Trying to close unclosable fd! self.buffer.close() C:\dev\python\py3k\lib\io.py:389: RuntimeWarning: Trying to close unclosable fd! self.close() __main__:1: RuntimeWarning: Trying to close unclosable fd! Also, there are no unit test for closefd.
msg75431 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-10-31 21:37
I don't see a good 'n easy way to fix the issue. close() is called in too many places and I don't wanna add more checks in Python code. This patch reduces the mass of warnings to one, but it also changes the semantic of close() a bit. However it seems logical to set the fd attribute of the file object to -1 (meaning closed). Index: Modules/_fileio.c =================================================================== --- Modules/_fileio.c (Revision 67068) +++ Modules/_fileio.c (Arbeitskopie) @@ -60,7 +60,8 @@ static PyObject * fileio_close(PyFileIOObject *self) { - if (!self->closefd) { + if (!self->closefd && self->fd >= 0) { + self->fd = -1; if (PyErr_WarnEx(PyExc_RuntimeWarning, "Trying to close unclosable fd!", 3) < 0) { return NULL;
msg75435 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-11-01 01:12
@christian: Your patch doesn't work: if close() if called twice, the file is really closed :-/ Here is a new patch: - _FileIO.close() sets self->fd to -1 but don't show a warning anymore because the operation is valid: it does really close the _FileIO object - add two tests for two problems: read is now blocked if the file is closed, and the warning displayed by FileIO.__del__
msg75436 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-11-01 01:28
christian just told me that my svn repos was old. Here is an updated version of my patch. I now use io.open() instead of io.BufferedReader() in the tests.
msg75437 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-11-01 01:44
Another patch based on Victor's patch. It adds some additional tests and exposes the internal attribute closefd.
msg75514 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-11-05 01:23
Here is a new patch with doc and NEWS updates.
msg75524 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2008-11-05 19:28
Small typo, conveyed to Crys_ in irc.
msg75526 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-11-05 19:32
Applied in r67106 (py3k) The patch must be backported to 2.6 and 2.7.
msg75803 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-11-12 22:19
Can this change be backported to 2.6 and 2.7? r67106 says that it "Changed semantic of close() on file objects with closefd=False".
msg75807 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-11-12 22:56
Yes, it should. I've lefted the bug open for this very reason.
msg76150 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-11-20 23:39
Backported to trunk in r67307. But -- do we really want to backport to 2.6? This changes the semantics of closefd, adds a new closefd attribute... Did the rules change for 2.6.1?
msg77019 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-12-05 14:34
2.6.1 is released. Should this issue be apply to 2.6.x or not?
msg80788 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2009-01-29 22:38
This issue has been fixed in py3k, release30-maint and trunk. I think that 2.6.x doesn't need this API change, so I prefer to close this issue. Reopen the issue if I'm wrong.
History
Date User Action Args
2022-04-11 14:56:40 admin set github: 48483
2009-01-29 22:38:45 vstinner set status: open -> closedmessages: +
2008-12-05 14:34:16 vstinner set messages: +
2008-11-20 23:39:39 amaury.forgeotdarc set messages: +
2008-11-12 22:56:48 christian.heimes set messages: +
2008-11-12 22:19:46 amaury.forgeotdarc set messages: +
2008-11-05 19:33:45 christian.heimes set priority: release blocker -> highversions: + Python 2.6, Python 2.7, - Python 3.0
2008-11-05 19:32:13 christian.heimes set type: behaviorresolution: accepted -> fixedmessages: + assignee: christian.heimes
2008-11-05 19:28:01 barry set resolution: acceptedmessages: + nosy: + barry
2008-11-05 01:23:11 christian.heimes set files: + fileio_closefd4.patchmessages: +
2008-11-01 01:44:58 christian.heimes set files: + fileio_closefd3.patchmessages: +
2008-11-01 01:28:22 vstinner set files: - fileio_closefd.patch
2008-11-01 01:28:12 vstinner set files: + fileio_closefd-2.patchmessages: +
2008-11-01 01:12:22 vstinner set files: + fileio_closefd.patchkeywords: + patchmessages: + nosy: + vstinner
2008-10-31 21:37:41 christian.heimes set priority: critical -> release blockernosy: + christian.heimesmessages: +
2008-10-30 00:08:00 amaury.forgeotdarc create