diff -r ae42c4576438 Lib/test/subprocessdata/fd_status.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/test/subprocessdata/fd_status.py Fri Feb 13 09:29:05 2015 +0100 @@ -0,0 +1,32 @@ +"""When called as a script, print a comma-separated list of the open +file descriptors on stdout. + +Usage: +fd_stats.py: check all file descriptors +fd_status.py fd1 fd2 ...: check only specified file descriptors +""" + +import errno +import os +import stat +import sys + +if __name__ == "__main__": + fds = [] + if len(sys.argv) == 1: + try: + _MAXFD = os.sysconf("SC_OPEN_MAX") + except: + _MAXFD = 256 + test_fds = range(0, _MAXFD) + else: + test_fds = map(int, sys.argv[1:]) + for fd in test_fds: + try: + st = os.fstat(fd) + except OSError as e: + if e.errno == errno.EBADF: + continue + raise + fds.append(fd) + print(','.join(map(str, fds))) diff -r ae42c4576438 Lib/test/test_os.py --- a/Lib/test/test_os.py Mon Jan 26 13:45:04 2015 +0200 +++ b/Lib/test/test_os.py Fri Feb 13 09:29:05 2015 +0100 @@ -568,6 +568,21 @@ class URandomTests (unittest.TestCase): data2 = self.get_urandom_subprocess(16) self.assertNotEqual(data1, data2) + def test_urandom_fd_non_inheritable(self): + fd_status = test_support.findfile("fd_status.py", subdir="subprocessdata") + + # Ensure that the /dev/urandom file descriptor is open + os.urandom(1) + + proc = subprocess.Popen([sys.executable, fd_status], + stdout=subprocess.PIPE, close_fds=False) + output, error = proc.communicate() + print(repr(output)) + open_fds = set(map(int, output.split(','))) + fds = open_fds - set(range(3)) + + self.assertEqual(fds, []) + HAVE_GETENTROPY = (sysconfig.get_config_var('HAVE_GETENTROPY') == 1)