Skip to content

Commit 84eb42e

Browse files
bpo-38110: Use fdwalk for os.closerange() when available. (GH-15224)
Use fdwalk() on platforms that support it to implement os.closerange(). (cherry picked from commit e20134f) Co-authored-by: Jakub Kulík <Kulikjak@gmail.com>
1 parent db0d8a5 commit 84eb42e

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The os.closewalk() implementation now uses the libc fdwalk() API on
2+
platforms where it is available.

Modules/posixmodule.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8419,6 +8419,21 @@ os_close_impl(PyObject *module, int fd)
84198419
}
84208420

84218421

8422+
#ifdef HAVE_FDWALK
8423+
static int
8424+
_fdwalk_close_func(void *lohi, int fd)
8425+
{
8426+
int lo = ((int *)lohi)[0];
8427+
int hi = ((int *)lohi)[1];
8428+
8429+
if (fd >= hi)
8430+
return 1;
8431+
else if (fd >= lo)
8432+
close(fd);
8433+
return 0;
8434+
}
8435+
#endif /* HAVE_FDWALK */
8436+
84228437
/*[clinic input]
84238438
os.closerange
84248439
@@ -8433,11 +8448,21 @@ static PyObject *
84338448
os_closerange_impl(PyObject *module, int fd_low, int fd_high)
84348449
/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
84358450
{
8451+
#ifdef HAVE_FDWALK
8452+
int lohi[2];
8453+
#else
84368454
int i;
8455+
#endif
84378456
Py_BEGIN_ALLOW_THREADS
84388457
_Py_BEGIN_SUPPRESS_IPH
8458+
#ifdef HAVE_FDWALK
8459+
lohi[0] = Py_MAX(fd_low, 0);
8460+
lohi[1] = fd_high;
8461+
fdwalk(_fdwalk_close_func, lohi);
8462+
#else
84398463
for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
84408464
close(i);
8465+
#endif
84418466
_Py_END_SUPPRESS_IPH
84428467
Py_END_ALLOW_THREADS
84438468
Py_RETURN_NONE;

configure

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11500,7 +11500,7 @@ fi
1150011500
for ac_func in alarm accept4 setitimer getitimer bind_textdomain_codeset chown \
1150111501
clock confstr copy_file_range ctermid dup3 execv explicit_bzero explicit_memset \
1150211502
faccessat fchmod fchmodat fchown fchownat \
11503-
fexecve fdopendir fork fpathconf fstatat ftime ftruncate futimesat \
11503+
fdwalk fexecve fdopendir fork fpathconf fstatat ftime ftruncate futimesat \
1150411504
futimens futimes gai_strerror getentropy \
1150511505
getgrgid_r getgrnam_r \
1150611506
getgrouplist getgroups getlogin getloadavg getpeername getpgid getpid \

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3538,7 +3538,7 @@ fi
35383538
AC_CHECK_FUNCS(alarm accept4 setitimer getitimer bind_textdomain_codeset chown \
35393539
clock confstr copy_file_range ctermid dup3 execv explicit_bzero explicit_memset \
35403540
faccessat fchmod fchmodat fchown fchownat \
3541-
fexecve fdopendir fork fpathconf fstatat ftime ftruncate futimesat \
3541+
fdwalk fexecve fdopendir fork fpathconf fstatat ftime ftruncate futimesat \
35423542
futimens futimes gai_strerror getentropy \
35433543
getgrgid_r getgrnam_r \
35443544
getgrouplist getgroups getlogin getloadavg getpeername getpgid getpid \

pyconfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@
341341
/* Define to 1 if you have the `fdopendir' function. */
342342
#undef HAVE_FDOPENDIR
343343

344+
/* Define to 1 if you have the `fdwalk' function. */
345+
#undef HAVE_FDWALK
346+
344347
/* Define to 1 if you have the `fexecve' function. */
345348
#undef HAVE_FEXECVE
346349

0 commit comments

Comments
 (0)