components/python/python27/patches/11-closerange.patch
branchs11u2-sru
changeset 3379 e99da14b537a
equal deleted inserted replaced
3375:3724eda7445e 3379:e99da14b537a
       
     1 This patch uses fdwalk(3c) to close file descriptors; as that function is not
       
     2 widely implemented, this is unsuitable for upstream.
       
     3 
       
     4 --- Python-2.7.7/Modules/posixmodule.c.~1~	2014-05-31 11:58:40.000000000 -0700
       
     5 +++ Python-2.7.7/Modules/posixmodule.c	2014-06-02 10:49:30.052826955 -0700
       
     6 @@ -6607,16 +6607,34 @@
       
     7  "closerange(fd_low, fd_high)\n\n\
       
     8  Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
       
     9  
       
    10 +static int
       
    11 +close_func(void *lohi, int fd)
       
    12 +{
       
    13 +    int lo = ((int *)lohi)[0];
       
    14 +    int hi = ((int *)lohi)[1];
       
    15 +
       
    16 +    if (fd >= hi)
       
    17 +        return (1);
       
    18 +    else if (fd >= lo)
       
    19 +        close(fd);
       
    20 +
       
    21 +    return (0);
       
    22 +}
       
    23 +
       
    24  static PyObject *
       
    25  posix_closerange(PyObject *self, PyObject *args)
       
    26  {
       
    27      int fd_from, fd_to, i;
       
    28 +    int lohi[2];
       
    29 +
       
    30      if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
       
    31          return NULL;
       
    32      Py_BEGIN_ALLOW_THREADS
       
    33 -    for (i = fd_from; i < fd_to; i++)
       
    34 -        if (_PyVerify_fd(i))
       
    35 -            close(i);
       
    36 +
       
    37 +    lohi[0] = fd_from;
       
    38 +    lohi[1] = fd_to;
       
    39 +    fdwalk(close_func, lohi);
       
    40 +
       
    41      Py_END_ALLOW_THREADS
       
    42      Py_RETURN_NONE;
       
    43  }