From e20134f889a0cfcc37a46979f31a1c98b800de07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kul=C3=ADk?= Date: Wed, 11 Sep 2019 17:11:57 +0200 Subject: [PATCH] bpo-38110: Use fdwalk for os.closerange() when available. (GH-15224) Use fdwalk() on platforms that support it to implement os.closerange(). --- .../2019-09-11-14-49-20.bpo-38110.A19Y-q.rst | 2 ++ Modules/posixmodule.c | 25 +++++++++++++++++++ configure | 2 +- configure.ac | 2 +- pyconfig.h.in | 3 +++ 5 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2019-09-11-14-49-20.bpo-38110.A19Y-q.rst diff --git a/Misc/NEWS.d/next/Library/2019-09-11-14-49-20.bpo-38110.A19Y-q.rst b/Misc/NEWS.d/next/Library/2019-09-11-14-49-20.bpo-38110.A19Y-q.rst new file mode 100644 index 00000000000000..7b94a2e90a5038 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-09-11-14-49-20.bpo-38110.A19Y-q.rst @@ -0,0 +1,2 @@ +The os.closewalk() implementation now uses the libc fdwalk() API on +platforms where it is available. diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index a0a2a305119e43..88d3d83239508b 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -8422,6 +8422,21 @@ os_close_impl(PyObject *module, int fd) } +#ifdef HAVE_FDWALK +static int +_fdwalk_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; +} +#endif /* HAVE_FDWALK */ + /*[clinic input] os.closerange @@ -8436,11 +8451,21 @@ static PyObject * os_closerange_impl(PyObject *module, int fd_low, int fd_high) /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ { +#ifdef HAVE_FDWALK + int lohi[2]; +#else int i; +#endif Py_BEGIN_ALLOW_THREADS _Py_BEGIN_SUPPRESS_IPH +#ifdef HAVE_FDWALK + lohi[0] = Py_MAX(fd_low, 0); + lohi[1] = fd_high; + fdwalk(_fdwalk_close_func, lohi); +#else for (i = Py_MAX(fd_low, 0); i < fd_high; i++) close(i); +#endif _Py_END_SUPPRESS_IPH Py_END_ALLOW_THREADS Py_RETURN_NONE; diff --git a/configure b/configure index cb60ce256d433b..25f9de9fbbb80e 100755 --- a/configure +++ b/configure @@ -11500,7 +11500,7 @@ fi for ac_func in alarm accept4 setitimer getitimer bind_textdomain_codeset chown \ clock confstr copy_file_range ctermid dup3 execv explicit_bzero explicit_memset \ faccessat fchmod fchmodat fchown fchownat \ - fexecve fdopendir fork fpathconf fstatat ftime ftruncate futimesat \ + fdwalk fexecve fdopendir fork fpathconf fstatat ftime ftruncate futimesat \ futimens futimes gai_strerror getentropy \ getgrgid_r getgrnam_r \ getgrouplist getgroups getlogin getloadavg getpeername getpgid getpid \ diff --git a/configure.ac b/configure.ac index 9f5cea5ca0c709..5ca775cced097a 100644 --- a/configure.ac +++ b/configure.ac @@ -3538,7 +3538,7 @@ fi AC_CHECK_FUNCS(alarm accept4 setitimer getitimer bind_textdomain_codeset chown \ clock confstr copy_file_range ctermid dup3 execv explicit_bzero explicit_memset \ faccessat fchmod fchmodat fchown fchownat \ - fexecve fdopendir fork fpathconf fstatat ftime ftruncate futimesat \ + fdwalk fexecve fdopendir fork fpathconf fstatat ftime ftruncate futimesat \ futimens futimes gai_strerror getentropy \ getgrgid_r getgrnam_r \ getgrouplist getgroups getlogin getloadavg getpeername getpgid getpid \ diff --git a/pyconfig.h.in b/pyconfig.h.in index 5ff5cc2282f03a..aca9751d4b756e 100644 --- a/pyconfig.h.in +++ b/pyconfig.h.in @@ -337,6 +337,9 @@ /* Define to 1 if you have the `fdopendir' function. */ #undef HAVE_FDOPENDIR +/* Define to 1 if you have the `fdwalk' function. */ +#undef HAVE_FDWALK + /* Define to 1 if you have the `fexecve' function. */ #undef HAVE_FEXECVE