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
Backport of r67106 from the py3k branch
diff --git a/Modules/_fileio.c b/Modules/_fileio.c
index 49bc29c..b9310f3 100644
--- a/Modules/_fileio.c
+++ b/Modules/_fileio.c
@@ -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 PyString_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},
};