christian.heimes
2008-11-05 19:30:32 UTC
Author: christian.heimes
Date: Wed Nov 5 20:30:32 2008
New Revision: 67106
Log:
Fixed issue #4233.
Changed semantic of _fileio.FileIO's close() method on file objects with closefd=False. The file descriptor is still kept open but the file object behaves like a closed file. The FileIO object also got a new readonly attribute closefd.
Approved by Barry
Modified:
python/branches/py3k/Doc/library/io.rst
python/branches/py3k/Lib/test/test_io.py
python/branches/py3k/Misc/NEWS
python/branches/py3k/Modules/_fileio.c
Modified: python/branches/py3k/Doc/library/io.rst
==============================================================================
--- python/branches/py3k/Doc/library/io.rst (original)
+++ python/branches/py3k/Doc/library/io.rst Wed Nov 5 20:30:32 2008
@@ -213,8 +213,10 @@
.. method:: close()
- Flush and close this stream. This method has no effect if the file is
- already closed.
+ Flush and close this stream. This method has no effect if the file is
+ already closed. Once the file is closed, any operation on the file
+ (e.g. reading or writing) will raise an :exc:`IOError`. The internal
+ file descriptor isn't closed if *closefd* was False.
.. attribute:: closed
Modified: python/branches/py3k/Lib/test/test_io.py
==============================================================================
--- python/branches/py3k/Lib/test/test_io.py (original)
+++ python/branches/py3k/Lib/test/test_io.py Wed Nov 5 20:30:32 2008
@@ -272,6 +272,29 @@
self.assertRaises(ValueError, io.open, support.TESTFN, 'w',
closefd=False)
+ def testReadClosed(self):
+ with io.open(support.TESTFN, "w") as f:
+ f.write("egg\n")
+ with io.open(support.TESTFN, "r") as f:
+ file = io.open(f.fileno(), "r", closefd=False)
+ self.assertEqual(file.read(), "egg\n")
+ file.seek(0)
+ file.close()
+ self.assertRaises(ValueError, file.read)
+
+ def test_no_closefd_with_filename(self):
+ # can't use closefd in combination with a file name
+ self.assertRaises(ValueError, io.open, support.TESTFN, "r", closefd=False)
+
+ def test_closefd_attr(self):
+ with io.open(support.TESTFN, "wb") as f:
+ f.write(b"egg\n")
+ with io.open(support.TESTFN, "r") as f:
+ self.assertEqual(f.buffer.raw.closefd, True)
+ file = io.open(f.fileno(), "r", closefd=False)
+ self.assertEqual(file.buffer.raw.closefd, False)
+
+
class MemorySeekTestMixin:
def testInit(self):
@@ -1237,15 +1260,6 @@
else:
self.assert_(issubclass(obj, io.IOBase))
- def test_fileio_warnings(self):
- with support.check_warnings() as w:
- self.assertEqual(w.warnings, [])
- self.assertRaises(TypeError, io.FileIO, [])
- self.assertEqual(w.warnings, [])
- self.assertRaises(ValueError, io.FileIO, "/some/invalid/name", "rt")
- self.assertEqual(w.warnings, [])
-
-
def test_main():
support.run_unittest(IOTest, BytesIOTest, StringIOTest,
BufferedReaderTest, BufferedWriterTest,
Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS (original)
+++ python/branches/py3k/Misc/NEWS Wed Nov 5 20:30:32 2008
@@ -15,6 +15,11 @@
Core and Builtins
-----------------
+- Issue #4233: Changed semantic of ``_fileio.FileIO``'s ``close()``
+ method on file objects with closefd=False. The file descriptor is still
+ kept open but the file object behaves like a closed file. The ``FileIO``
+ object also got a new readonly attribute ``closefd``.
+
- Issue #3626: On cygwin, starting python with a non-existent script name
would not display anything if the file name is only 1 character long.
Modified: python/branches/py3k/Modules/_fileio.c
==============================================================================
--- python/branches/py3k/Modules/_fileio.c (original)
+++ python/branches/py3k/Modules/_fileio.c Wed Nov 5 20:30:32 2008
@@ -61,10 +61,7 @@
fileio_close(PyFileIOObject *self)
{
if (!self->closefd) {
- if (PyErr_WarnEx(PyExc_RuntimeWarning,
- "Trying to close unclosable fd!", 3) < 0) {
- return NULL;
- }
+ self->fd = -1;
Py_RETURN_NONE;
}
errno = internal_close(self);
@@ -821,6 +818,12 @@
}
static PyObject *
+get_closefd(PyFileIOObject *self, void *closure)
+{
+ return PyBool_FromLong((long)(self->closefd));
+}
+
+static PyObject *
get_mode(PyFileIOObject *self, void *closure)
{
return PyUnicode_FromString(mode_string(self));
@@ -828,6 +831,8 @@
static PyGetSetDef fileio_getsetlist[] = {
{"closed", (getter)get_closed, NULL, "True if the file is closed"},
+ {"closefd", (getter)get_closefd, NULL,
+ "True if the file descriptor will be closed"},
{"mode", (getter)get_mode, NULL, "String giving the file mode"},
{0},
};
Date: Wed Nov 5 20:30:32 2008
New Revision: 67106
Log:
Fixed issue #4233.
Changed semantic of _fileio.FileIO's close() method on file objects with closefd=False. The file descriptor is still kept open but the file object behaves like a closed file. The FileIO object also got a new readonly attribute closefd.
Approved by Barry
Modified:
python/branches/py3k/Doc/library/io.rst
python/branches/py3k/Lib/test/test_io.py
python/branches/py3k/Misc/NEWS
python/branches/py3k/Modules/_fileio.c
Modified: python/branches/py3k/Doc/library/io.rst
==============================================================================
--- python/branches/py3k/Doc/library/io.rst (original)
+++ python/branches/py3k/Doc/library/io.rst Wed Nov 5 20:30:32 2008
@@ -213,8 +213,10 @@
.. method:: close()
- Flush and close this stream. This method has no effect if the file is
- already closed.
+ Flush and close this stream. This method has no effect if the file is
+ already closed. Once the file is closed, any operation on the file
+ (e.g. reading or writing) will raise an :exc:`IOError`. The internal
+ file descriptor isn't closed if *closefd* was False.
.. attribute:: closed
Modified: python/branches/py3k/Lib/test/test_io.py
==============================================================================
--- python/branches/py3k/Lib/test/test_io.py (original)
+++ python/branches/py3k/Lib/test/test_io.py Wed Nov 5 20:30:32 2008
@@ -272,6 +272,29 @@
self.assertRaises(ValueError, io.open, support.TESTFN, 'w',
closefd=False)
+ def testReadClosed(self):
+ with io.open(support.TESTFN, "w") as f:
+ f.write("egg\n")
+ with io.open(support.TESTFN, "r") as f:
+ file = io.open(f.fileno(), "r", closefd=False)
+ self.assertEqual(file.read(), "egg\n")
+ file.seek(0)
+ file.close()
+ self.assertRaises(ValueError, file.read)
+
+ def test_no_closefd_with_filename(self):
+ # can't use closefd in combination with a file name
+ self.assertRaises(ValueError, io.open, support.TESTFN, "r", closefd=False)
+
+ def test_closefd_attr(self):
+ with io.open(support.TESTFN, "wb") as f:
+ f.write(b"egg\n")
+ with io.open(support.TESTFN, "r") as f:
+ self.assertEqual(f.buffer.raw.closefd, True)
+ file = io.open(f.fileno(), "r", closefd=False)
+ self.assertEqual(file.buffer.raw.closefd, False)
+
+
class MemorySeekTestMixin:
def testInit(self):
@@ -1237,15 +1260,6 @@
else:
self.assert_(issubclass(obj, io.IOBase))
- def test_fileio_warnings(self):
- with support.check_warnings() as w:
- self.assertEqual(w.warnings, [])
- self.assertRaises(TypeError, io.FileIO, [])
- self.assertEqual(w.warnings, [])
- self.assertRaises(ValueError, io.FileIO, "/some/invalid/name", "rt")
- self.assertEqual(w.warnings, [])
-
-
def test_main():
support.run_unittest(IOTest, BytesIOTest, StringIOTest,
BufferedReaderTest, BufferedWriterTest,
Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS (original)
+++ python/branches/py3k/Misc/NEWS Wed Nov 5 20:30:32 2008
@@ -15,6 +15,11 @@
Core and Builtins
-----------------
+- Issue #4233: Changed semantic of ``_fileio.FileIO``'s ``close()``
+ method on file objects with closefd=False. The file descriptor is still
+ kept open but the file object behaves like a closed file. The ``FileIO``
+ object also got a new readonly attribute ``closefd``.
+
- Issue #3626: On cygwin, starting python with a non-existent script name
would not display anything if the file name is only 1 character long.
Modified: python/branches/py3k/Modules/_fileio.c
==============================================================================
--- python/branches/py3k/Modules/_fileio.c (original)
+++ python/branches/py3k/Modules/_fileio.c Wed Nov 5 20:30:32 2008
@@ -61,10 +61,7 @@
fileio_close(PyFileIOObject *self)
{
if (!self->closefd) {
- if (PyErr_WarnEx(PyExc_RuntimeWarning,
- "Trying to close unclosable fd!", 3) < 0) {
- return NULL;
- }
+ self->fd = -1;
Py_RETURN_NONE;
}
errno = internal_close(self);
@@ -821,6 +818,12 @@
}
static PyObject *
+get_closefd(PyFileIOObject *self, void *closure)
+{
+ return PyBool_FromLong((long)(self->closefd));
+}
+
+static PyObject *
get_mode(PyFileIOObject *self, void *closure)
{
return PyUnicode_FromString(mode_string(self));
@@ -828,6 +831,8 @@
static PyGetSetDef fileio_getsetlist[] = {
{"closed", (getter)get_closed, NULL, "True if the file is closed"},
+ {"closefd", (getter)get_closefd, NULL,
+ "True if the file descriptor will be closed"},
{"mode", (getter)get_mode, NULL, "String giving the file mode"},
{0},
};