Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 933430a

Browse files
committed
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.
1 parent d20ec1b commit 933430a

5 files changed

Lines changed: 21 additions & 12 deletions

File tree

Doc/library/functions.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -995,8 +995,8 @@ are always available. They are listed here in alphabetical order.
995995

996996
If *closefd* is ``False`` and a file descriptor rather than a filename was
997997
given, the underlying file descriptor will be kept open when the file is
998-
closed. If a filename is given *closefd* has no effect and must be ``True``
999-
(the default).
998+
closed. If a filename is given *closefd* must be ``True`` (the default)
999+
otherwise an error will be raised.
10001000

10011001
A custom opener can be used by passing a callable as *opener*. The underlying
10021002
file descriptor for the file object is then obtained by calling *opener* with

Doc/library/io.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,12 @@ Raw File I/O
519519
The *name* can be one of two things:
520520

521521
* a character string or :class:`bytes` object representing the path to the
522-
file which will be opened;
522+
file which will be opened. In this case closefd must be True (the default)
523+
otherwise an error will be raised.
523524
* an integer representing the number of an existing OS-level file descriptor
524-
to which the resulting :class:`FileIO` object will give access.
525+
to which the resulting :class:`FileIO` object will give access. When the
526+
FileIO object is closed this fd will be closed as well, unless *closefd*
527+
is set to ``False``.
525528

526529
The *mode* can be ``'r'``, ``'w'``, ``'x'`` or ``'a'`` for reading
527530
(default), writing, exclusive creation or appending. The file will be

Lib/test/test_fileio.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,13 @@ def test_reject(self):
112112
self.assertRaises(TypeError, self.f.write, "Hello!")
113113

114114
def testRepr(self):
115-
self.assertEqual(repr(self.f), "<_io.FileIO name=%r mode=%r>"
116-
% (self.f.name, self.f.mode))
115+
self.assertEqual(
116+
repr(self.f), "<_io.FileIO name=%r mode=%r closefd='%d'>"
117+
% (self.f.name, self.f.mode, self.f.closefd))
117118
del self.f.name
118-
self.assertEqual(repr(self.f), "<_io.FileIO fd=%r mode=%r>"
119-
% (self.f.fileno(), self.f.mode))
119+
self.assertEqual(
120+
repr(self.f), "<_io.FileIO fd=%r mode=%r closefd='%d'>"
121+
% (self.f.fileno(), self.f.mode, self.f.closefd))
120122
self.f.close()
121123
self.assertEqual(repr(self.f), "<_io.FileIO [closed]>")
122124

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ Library
196196
- Issue #22641: In asyncio, the default SSL context for client connections
197197
is now created using ssl.create_default_context(), for stronger security.
198198

199+
- Issue #17401: Include closefd in io.FileIO repr.
200+
199201
- Issue #21338: Add silent mode for compileall. quiet parameters of
200202
compile_{dir, file, path} functions now have a multilevel value. Also,
201203
-q option of the CLI now have a multilevel value. Patch by Thomas Kluyver.

Modules/_io/fileio.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,12 +1054,14 @@ fileio_repr(fileio *self)
10541054
PyErr_Clear();
10551055
else
10561056
return NULL;
1057-
res = PyUnicode_FromFormat("<_io.FileIO fd=%d mode='%s'>",
1058-
self->fd, mode_string(self));
1057+
res = PyUnicode_FromFormat(
1058+
"<_io.FileIO fd=%d mode='%s' closefd='%d'>",
1059+
self->fd, mode_string(self), self->closefd);
10591060
}
10601061
else {
1061-
res = PyUnicode_FromFormat("<_io.FileIO name=%R mode='%s'>",
1062-
nameobj, mode_string(self));
1062+
res = PyUnicode_FromFormat(
1063+
"<_io.FileIO name=%R mode='%s' closefd='%d'>",
1064+
nameobj, mode_string(self), self->closefd);
10631065
Py_DECREF(nameobj);
10641066
}
10651067
return res;

0 commit comments

Comments
 (0)