Skip to content

Commit e20134f

Browse files
kulikjakgpshead
authored andcommitted
bpo-38110: Use fdwalk for os.closerange() when available. (GH-15224)
Use fdwalk() on platforms that support it to implement os.closerange().
1 parent af636f4 commit e20134f

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
@@ -8422,6 +8422,21 @@ os_close_impl(PyObject *module, int fd)
84228422
}
84238423

84248424

8425+
#ifdef HAVE_FDWALK
8426+
static int
8427+
_fdwalk_close_func(void *lohi, int fd)
8428+
{
8429+
int lo = ((int *)lohi)[0];
8430+
int hi = ((int *)lohi)[1];
8431+
8432+
if (fd >= hi)
8433+
return 1;
8434+
else if (fd >= lo)
8435+
close(fd);
8436+
return 0;
8437+
}
8438+
#endif /* HAVE_FDWALK */
8439+
84258440
/*[clinic input]
84268441
os.closerange
84278442
@@ -8436,11 +8451,21 @@ static PyObject *
84368451
os_closerange_impl(PyObject *module, int fd_low, int fd_high)
84378452
/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
84388453
{
8454+
#ifdef HAVE_FDWALK
8455+
int lohi[2];
8456+
#else
84398457
int i;
8458+
#endif
84408459
Py_BEGIN_ALLOW_THREADS
84418460
_Py_BEGIN_SUPPRESS_IPH
8461+
#ifdef HAVE_FDWALK
8462+
lohi[0] = Py_MAX(fd_low, 0);
8463+
lohi[1] = fd_high;
8464+
fdwalk(_fdwalk_close_func, lohi);
8465+
#else
84428466
for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
84438467
close(i);
8468+
#endif
84448469
_Py_END_SUPPRESS_IPH
84458470
Py_END_ALLOW_THREADS
84468471
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
@@ -337,6 +337,9 @@
337337
/* Define to 1 if you have the `fdopendir' function. */
338338
#undef HAVE_FDOPENDIR
339339

340+
/* Define to 1 if you have the `fdwalk' function. */
341+
#undef HAVE_FDWALK
342+
340343
/* Define to 1 if you have the `fexecve' function. */
341344
#undef HAVE_FEXECVE
342345

0 commit comments

Comments
 (0)