summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert Collins <rbtcollins@hp.com>2014-10-18 00:32:43 (GMT)
committerRobert Collins <rbtcollins@hp.com>2014-10-18 00:32:43 (GMT)
commit933430ab69b051aa35130a3cf779528faca9b6f2 (patch)
treedf0df6807ba2f82cc56d034c54226a5f2628abc9
parentd20ec1b92196a01acf9bfb990329d06b4dee5c78 (diff)
downloadcpython-933430ab69b051aa35130a3cf779528faca9b6f2.zip
cpython-933430ab69b051aa35130a3cf779528faca9b6f2.tar.gz
cpython-933430ab69b051aa35130a3cf779528faca9b6f2.tar.bz2
Issue #17401: document closefd in io.FileIO docs and add to repr
closefd was documented in the open docs but not the matching FileIO class documented. Further, closefd, part of the core state for the object was not shown. In review it was noted that the open docs are a little confusing about the interaction between closefd and paths, so tweaked them at the same time.
-rw-r--r--Doc/library/functions.rst4
-rw-r--r--Doc/library/io.rst7
-rw-r--r--Lib/test/test_fileio.py10
-rw-r--r--Misc/NEWS2
-rw-r--r--Modules/_io/fileio.c10
5 files changed, 21 insertions, 12 deletions
diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst
index b4433bf..60fadca 100644
--- a/Doc/library/functions.rst
+++ b/Doc/library/functions.rst
@@ -995,8 +995,8 @@ are always available. They are listed here in alphabetical order.
If *closefd* is ``False`` and a file descriptor rather than a filename was
given, the underlying file descriptor will be kept open when the file is
- closed. If a filename is given *closefd* has no effect and must be ``True``
- (the default).
+ closed. If a filename is given *closefd* must be ``True`` (the default)
+ otherwise an error will be raised.
A custom opener can be used by passing a callable as *opener*. The underlying
file descriptor for the file object is then obtained by calling *opener* with
diff --git a/Doc/library/io.rst b/Doc/library/io.rst
index 533ba9d..0054286 100644
--- a/Doc/library/io.rst
+++ b/Doc/library/io.rst
@@ -519,9 +519,12 @@ Raw File I/O
The *name* can be one of two things:
* a character string or :class:`bytes` object representing the path to the
- file which will be opened;
+ file which will be opened. In this case closefd must be True (the default)
+ otherwise an error will be raised.
* an integer representing the number of an existing OS-level file descriptor
- to which the resulting :class:`FileIO` object will give access.
+ to which the resulting :class:`FileIO` object will give access. When the
+ FileIO object is closed this fd will be closed as well, unless *closefd*
+ is set to ``False``.
The *mode* can be ``'r'``, ``'w'``, ``'x'`` or ``'a'`` for reading
(default), writing, exclusive creation or appending. The file will be
diff --git a/Lib/test/test_fileio.py b/Lib/test/test_fileio.py
index 41be95e..7c1a5ce 100644
--- a/Lib/test/test_fileio.py
+++ b/Lib/test/test_fileio.py
@@ -112,11 +112,13 @@ class AutoFileTests(unittest.TestCase):
self.assertRaises(TypeError, self.f.write, "Hello!")
def testRepr(self):
- self.assertEqual(repr(self.f), "<_io.FileIO name=%r mode=%r>"
- % (self.f.name, self.f.mode))
+ self.assertEqual(
+ repr(self.f), "<_io.FileIO name=%r mode=%r closefd='%d'>"
+ % (self.f.name, self.f.mode, self.f.closefd))
del self.f.name
- self.assertEqual(repr(self.f), "<_io.FileIO fd=%r mode=%r>"
- % (self.f.fileno(), self.f.mode))
+ self.assertEqual(
+ repr(self.f), "<_io.FileIO fd=%r mode=%r closefd='%d'>"
+ % (self.f.fileno(), self.f.mode, self.f.closefd))
self.f.close()
self.assertEqual(repr(self.f), "<_io.FileIO [closed]>")
diff --git a/Misc/NEWS b/Misc/NEWS
index f1045ca..5d1b807 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -196,6 +196,8 @@ Library
- Issue #22641: In asyncio, the default SSL context for client connections
is now created using ssl.create_default_context(), for stronger security.
+- Issue #17401: Include closefd in io.FileIO repr.
+
- Issue #21338: Add silent mode for compileall. quiet parameters of
compile_{dir, file, path} functions now have a multilevel value. Also,
-q option of the CLI now have a multilevel value. Patch by Thomas Kluyver.
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index 2e0fbf9..5c1316e 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -1054,12 +1054,14 @@ fileio_repr(fileio *self)
PyErr_Clear();
else
return NULL;
- res = PyUnicode_FromFormat("<_io.FileIO fd=%d mode='%s'>",
- self->fd, mode_string(self));
+ res = PyUnicode_FromFormat(
+ "<_io.FileIO fd=%d mode='%s' closefd='%d'>",
+ self->fd, mode_string(self), self->closefd);
}
else {
- res = PyUnicode_FromFormat("<_io.FileIO name=%R mode='%s'>",
- nameobj, mode_string(self));
+ res = PyUnicode_FromFormat(
+ "<_io.FileIO name=%R mode='%s' closefd='%d'>",
+ nameobj, mode_string(self), self->closefd);
Py_DECREF(nameobj);
}
return res;