File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff 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 ;
You can’t perform that action at this time.
0 commit comments