#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.
This commit is contained in:
Georg Brandl 2008-01-19 20:22:13 +00:00
parent 15ce880cc8
commit 309501a617
5 changed files with 44 additions and 7 deletions

View File

@ -481,6 +481,20 @@ by file descriptors.
:func:`fdopen`, use its :meth:`close` method. :func:`fdopen`, use its :meth:`close` method.
.. function:: closerange(fd_low, fd_high)
Close all file descriptors from *fd_low* (inclusive) to *fd_high* (exclusive),
ignoring errors. Availability: Macintosh, Unix, Windows. Equivalent to::
for fd in xrange(fd_low, fd_high):
try:
os.close(fd)
except OSError:
pass
.. versionadded:: 2.6
.. function:: dup(fd) .. function:: dup(fd)
Return a duplicate of file descriptor *fd*. Availability: Macintosh, Unix, Return a duplicate of file descriptor *fd*. Availability: Macintosh, Unix,

View File

@ -965,13 +965,8 @@ class Popen(object):
def _close_fds(self, but): def _close_fds(self, but):
for i in xrange(3, MAXFD): os.closerange(3, but)
if i == but: os.closerange(but + 1, MAXFD)
continue
try:
os.close(i)
except:
pass
def _execute_child(self, args, executable, preexec_fn, close_fds, def _execute_child(self, args, executable, preexec_fn, close_fds,

View File

@ -23,6 +23,12 @@ class FileTests(unittest.TestCase):
os.close(f) os.close(f)
self.assert_(os.access(test_support.TESTFN, os.W_OK)) 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): class TemporaryFileTests(unittest.TestCase):
def setUp(self): def setUp(self):

View File

@ -1019,6 +1019,9 @@ Library
Extension Modules Extension Modules
----------------- -----------------
- Patch #1663329: added ``os.closerange()`` function to quickly close a range
of file descriptors without considering errors.
- Patch 976880: ``mmap`` objects now have an ``rfind`` method that - Patch 976880: ``mmap`` objects now have an ``rfind`` method that
works as expected. ``mmap.find`` also takes an optional ``end`` works as expected. ``mmap.find`` also takes an optional ``end``
parameter. parameter.

View File

@ -6156,6 +6156,24 @@ posix_close(PyObject *self, PyObject *args)
} }
PyDoc_STRVAR(posix_closerange__doc__,
"closerange(fd_low, fd_high)\n\n\
Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
static PyObject *
posix_closerange(PyObject *self, PyObject *args)
{
int fd_from, fd_to, i;
if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
return NULL;
Py_BEGIN_ALLOW_THREADS
for (i = fd_from; i < fd_to; i++)
close(i);
Py_END_ALLOW_THREADS
Py_RETURN_NONE;
}
PyDoc_STRVAR(posix_dup__doc__, PyDoc_STRVAR(posix_dup__doc__,
"dup(fd) -> fd2\n\n\ "dup(fd) -> fd2\n\n\
Return a duplicate of a file descriptor."); Return a duplicate of a file descriptor.");
@ -8451,6 +8469,7 @@ static PyMethodDef posix_methods[] = {
#endif /* HAVE_TCSETPGRP */ #endif /* HAVE_TCSETPGRP */
{"open", posix_open, METH_VARARGS, posix_open__doc__}, {"open", posix_open, METH_VARARGS, posix_open__doc__},
{"close", posix_close, METH_VARARGS, posix_close__doc__}, {"close", posix_close, METH_VARARGS, posix_close__doc__},
{"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
{"dup", posix_dup, METH_VARARGS, posix_dup__doc__}, {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
{"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__}, {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
{"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__}, {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},