components/python/python27/patches/11-closerange.patch
author John Beck <John.Beck@Oracle.COM>
Thu, 19 Jun 2014 13:54:21 -0700
changeset 1954 32663e59626d
parent 1914 00e8dbcb9b1e
child 4698 4e27e549b58d
permissions -rw-r--r--
19004605 update Python 2.7 line to version 2.7.7

This patch uses fdwalk(3c) to close file descriptors; as that function is not
widely implemented, this is unsuitable for upstream.

--- Python-2.7.7/Modules/posixmodule.c.~1~	2014-05-31 11:58:40.000000000 -0700
+++ Python-2.7.7/Modules/posixmodule.c	2014-06-02 10:49:30.052826955 -0700
@@ -6607,16 +6607,34 @@
 "closerange(fd_low, fd_high)\n\n\
 Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
 
+static int
+close_func(void *lohi, int fd)
+{
+    int lo = ((int *)lohi)[0];
+    int hi = ((int *)lohi)[1];
+
+    if (fd >= hi)
+        return (1);
+    else if (fd >= lo)
+        close(fd);
+
+    return (0);
+}
+
 static PyObject *
 posix_closerange(PyObject *self, PyObject *args)
 {
     int fd_from, fd_to, i;
+    int lohi[2];
+
     if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
         return NULL;
     Py_BEGIN_ALLOW_THREADS
-    for (i = fd_from; i < fd_to; i++)
-        if (_PyVerify_fd(i))
-            close(i);
+
+    lohi[0] = fd_from;
+    lohi[1] = fd_to;
+    fdwalk(close_func, lohi);
+
     Py_END_ALLOW_THREADS
     Py_RETURN_NONE;
 }