summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles-Fran?ois Natali <cf.natali@gmail.com>2013-08-25 18:28:44 +0200
committerCharles-Fran?ois Natali <cf.natali@gmail.com>2013-08-25 18:28:44 +0200
commit0da8dc3892f2ca0a378ddf22d2ae47b3aa631923 (patch)
tree38ad8999260c2535df6cc6aa521c3b4bc750043a
parent539264c48a4294b16f8e941f82601d90a2193478 (diff)
parent2e9e17f6d90d9c96efe0489608dd39e5f80b2984 (diff)
downloadcpython-0da8dc3892f2ca0a378ddf22d2ae47b3aa631923.tar.gz
Merge.
-rw-r--r--Lib/test/test_subprocess.py17
-rw-r--r--Modules/_posixsubprocess.c23
2 files changed, 29 insertions, 11 deletions
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py
index 470faff3c7..8a3ac4bae7 100644
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -1972,6 +1972,23 @@ class POSIXProcessTestCase(BaseTestCase):
self.assertRaises(OSError, os.waitpid, pid, 0)
self.assertNotIn(ident, [id(o) for o in subprocess._active])
+ def test_close_fds_after_preexec(self):
+ fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
+
+ # this FD is used as dup2() target by preexec_fn, and should be closed
+ # in the child process
+ fd = os.dup(1)
+ self.addCleanup(os.close, fd)
+
+ p = subprocess.Popen([sys.executable, fd_status],
+ stdout=subprocess.PIPE, close_fds=True,
+ preexec_fn=lambda: os.dup2(1, fd))
+ output, ignored = p.communicate()
+
+ remaining_fds = set(map(int, output.split(b',')))
+
+ self.assertNotIn(fd, remaining_fds)
+
@unittest.skipUnless(mswindows, "Windows specific tests")
class Win32ProcessTestCase(BaseTestCase):
diff --git a/Modules/_posixsubprocess.c b/Modules/_posixsubprocess.c
index c4a6b7bcb6..8d70107f37 100644
--- a/Modules/_posixsubprocess.c
+++ b/Modules/_posixsubprocess.c
@@ -412,17 +412,6 @@ child_exec(char *const exec_array[],
POSIX_CALL(close(errwrite));
}
- if (close_fds) {
- int local_max_fd = max_fd;
-#if defined(__NetBSD__)
- local_max_fd = fcntl(0, F_MAXFD);
- if (local_max_fd < 0)
- local_max_fd = max_fd;
-#endif
- /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
- _close_open_fd_range(3, local_max_fd, py_fds_to_keep);
- }
-
if (cwd)
POSIX_CALL(chdir(cwd));
@@ -451,6 +440,18 @@ child_exec(char *const exec_array[],
/* Py_DECREF(result); - We're about to exec so why bother? */
}
+ /* close FDs after executing preexec_fn, which might open FDs */
+ if (close_fds) {
+ int local_max_fd = max_fd;
+#if defined(__NetBSD__)
+ local_max_fd = fcntl(0, F_MAXFD);
+ if (local_max_fd < 0)
+ local_max_fd = max_fd;
+#endif
+ /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
+ _close_open_fd_range(3, local_max_fd, py_fds_to_keep);
+ }
+
/* This loop matches the Lib/os.py _execvpe()'s PATH search when */
/* given the executable_list generated by Lib/subprocess.py. */
saved_errno = 0;