@@ -685,17 +685,30 @@ as internal buffering of data.
685685
686686.. function :: dup(fd)
687687
688- Return a duplicate of file descriptor *fd *.
688+ Return a duplicate of file descriptor *fd *. The new file descriptor is
689+ :ref: `non-inheritable <fd_inheritance >`.
690+
691+ On Windows, when duplicating a standard stream (0: stdin, 1: stdout,
692+ 2: stderr), the new file descriptor is :ref: `inheritable
693+ <fd_inheritance>`.
689694
690695 Availability: Unix, Windows.
691696
697+ .. versionchanged :: 3.4
698+ The new file descriptor is now non-inheritable.
699+
692700
693- .. function :: dup2(fd, fd2)
701+ .. function :: dup2(fd, fd2, inheritable=True )
694702
695703 Duplicate file descriptor *fd * to *fd2 *, closing the latter first if necessary.
704+ The file descriptor *fd2 * is :ref: `inheritable <fd_inheritance >` by default,
705+ or non-inheritable if *inheritable * is ``False ``.
696706
697707 Availability: Unix, Windows.
698708
709+ .. versionchanged :: 3.4
710+ Add the optional *inheritable * parameter.
711+
699712
700713.. function :: fchmod(fd, mode)
701714
@@ -848,6 +861,7 @@ as internal buffering of data.
848861 Open the file *file * and set various flags according to *flags * and possibly
849862 its mode according to *mode *. When computing *mode *, the current umask value
850863 is first masked out. Return the file descriptor for the newly opened file.
864+ The new file descriptor is :ref: `non-inheritable <fd_inheritance >`.
851865
852866 For a description of the flag and mode values, see the C run-time documentation;
853867 flag constants (like :const: `O_RDONLY ` and :const: `O_WRONLY `) are defined in
@@ -859,6 +873,9 @@ as internal buffering of data.
859873
860874 Availability: Unix, Windows.
861875
876+ .. versionchanged :: 3.4
877+ The new file descriptor is now non-inheritable.
878+
862879 .. note ::
863880
864881 This function is intended for low-level I/O. For normal usage, use the
@@ -933,20 +950,28 @@ or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Window
933950
934951 .. index :: module: pty
935952
936- Open a new pseudo-terminal pair. Return a pair of file descriptors ``(master,
937- slave) `` for the pty and the tty, respectively. For a (slightly) more portable
938- approach, use the :mod: `pty ` module.
953+ Open a new pseudo-terminal pair. Return a pair of file descriptors
954+ ``(master, slave) `` for the pty and the tty, respectively. The new file
955+ descriptors are :ref: `non-inheritable <fd_inheritance >`. For a (slightly) more
956+ portable approach, use the :mod: `pty ` module.
939957
940958 Availability: some flavors of Unix.
941959
960+ .. versionchanged :: 3.4
961+ The new file descriptors are now non-inheritable.
962+
942963
943964.. function :: pipe()
944965
945- Create a pipe. Return a pair of file descriptors ``(r, w) `` usable for reading
946- and writing, respectively.
966+ Create a pipe. Return a pair of file descriptors ``(r, w) `` usable for
967+ reading and writing, respectively. The new file descriptor are
968+ :ref: `non-inheritable <fd_inheritance >`.
947969
948970 Availability: Unix, Windows.
949971
972+ .. versionchanged :: 3.4
973+ The new file descriptors are now non-inheritable.
974+
950975
951976.. function :: pipe2(flags)
952977
@@ -1178,6 +1203,50 @@ Querying the size of a terminal
11781203 Height of the terminal window in characters.
11791204
11801205
1206+ .. _fd_inheritance :
1207+
1208+ Inheritance of File Descriptors
1209+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1210+
1211+ A file descriptor has a inheritable flag which indicates if the file descriptor
1212+ can be inherited or not in child processes. Since Python 3.4, file descriptors
1213+ created by Python are non-inheritable by default.
1214+
1215+ On UNIX, non-inheritable file descriptors are closed in child processes at the
1216+ execution of a new program, other file descriptors are inherited.
1217+
1218+ On Windows, non-inheritable handles and file descriptors are closed in child
1219+ processes, except standard streams (file descriptors 0, 1 and 2: stdin, stdout
1220+ and stderr) which are always inherited. Using :func: `os.spawn* ` functions,
1221+ all inheritable handles and all inheritable file descriptors are inherited.
1222+ Using the :mod: `subprocess ` module, all file descriptors except standard
1223+ streams are closed, inheritable handles are only inherited if the *close_fds *
1224+ parameter is ``False ``.
1225+
1226+ .. versionadded :: 3.4
1227+
1228+ .. function :: get_inheritable(fd)
1229+
1230+ Get the `inheritable flag <fd_inheritance >`_ of the specified file
1231+ descriptor. Return a :class: `bool `.
1232+
1233+ .. function :: set_inheritable(fd, inheritable)
1234+
1235+ Set the `inheritable flag <fd_inheritance >`_ of the specified file descriptor.
1236+
1237+ .. function :: get_handle_inheritable(handle)
1238+
1239+ Get the `inheritable flag <fd_inheritance >`_ of the specified handle. Return a :class: `bool `.
1240+
1241+ Availability: Windows.
1242+
1243+ .. function :: set_handle_inheritable(handle, inheritable)
1244+
1245+ Set the `inheritable flag <fd_inheritance >`_ of the specified handle.
1246+
1247+ Availability: Windows.
1248+
1249+
11811250.. _os-file-dir :
11821251
11831252Files and Directories
0 commit comments