summaryrefslogtreecommitdiff
path: root/Lib/test/test_subprocess.py
diff options
context:
space:
mode:
authorCharles-François Natali <neologix@free.fr>2011-08-25 21:20:54 +0200
committerCharles-François Natali <neologix@free.fr>2011-08-25 21:20:54 +0200
commit2a34eb31040ad3eb2f09e22a0237afa98755390d (patch)
treee11a9f4b24a1db8835769acbb560906f899bde47 /Lib/test/test_subprocess.py
parenteacada8656981db1c90b747a2262cbc8b9eb9815 (diff)
downloadcpython-git-2a34eb31040ad3eb2f09e22a0237afa98755390d.tar.gz
Issue #12786: Set communication pipes used by subprocess.Popen CLOEXEC to avoid
them being inherited by other subprocesses.
Diffstat (limited to 'Lib/test/test_subprocess.py')
-rw-r--r--Lib/test/test_subprocess.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py
index 28b679837d..1aa2c0a41f 100644
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -995,6 +995,37 @@ class POSIXProcessTestCase(BaseTestCase):
self.assertRaises(OSError, os.waitpid, pid, 0)
self.assertNotIn(ident, [id(o) for o in subprocess._active])
+ def test_pipe_cloexec(self):
+ # Issue 12786: check that the communication pipes' FDs are set CLOEXEC,
+ # and are not inherited by another child process.
+ p1 = subprocess.Popen([sys.executable, "-c",
+ 'import os;'
+ 'os.read(0, 1)'
+ ],
+ stdin=subprocess.PIPE, stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+
+ p2 = subprocess.Popen([sys.executable, "-c", """if True:
+ import os, errno, sys
+ for fd in %r:
+ try:
+ os.close(fd)
+ except OSError as e:
+ if e.errno != errno.EBADF:
+ raise
+ else:
+ sys.exit(1)
+ sys.exit(0)
+ """ % [f.fileno() for f in (p1.stdin, p1.stdout,
+ p1.stderr)]
+ ],
+ stdin=subprocess.PIPE, stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE, close_fds=False)
+ p1.communicate('foo')
+ _, stderr = p2.communicate()
+
+ self.assertEqual(p2.returncode, 0, "Unexpected error: " + repr(stderr))
+
@unittest.skipUnless(mswindows, "Windows specific tests")
class Win32ProcessTestCase(BaseTestCase):