#1663329: add os.closerange() to close a range of fds,
ignoring errors, and use this in subprocess to speed up
subprocess creation in close_fds mode. Patch by Mike Klaas.
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index ca9489e..29c25bc 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -965,13 +965,8 @@
def _close_fds(self, but):
- for i in xrange(3, MAXFD):
- if i == but:
- continue
- try:
- os.close(i)
- except:
- pass
+ os.closerange(3, but)
+ os.closerange(but + 1, MAXFD)
def _execute_child(self, args, executable, preexec_fn, close_fds,
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 52fdd8a..a6fe40e 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -23,6 +23,12 @@
os.close(f)
self.assert_(os.access(test_support.TESTFN, os.W_OK))
+ def test_closerange(self):
+ f = os.open(test_support.TESTFN, os.O_CREAT|os.O_RDWR)
+ # close a fd that is open, and one that isn't
+ os.closerange(f, f+2)
+ self.assertRaises(OSError, os.write, f, "a")
+
class TemporaryFileTests(unittest.TestCase):
def setUp(self):