Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.8] bpo-38110: Use fdwalk for os.closerange() when available. (GH-15224) #15978

Merged
merged 1 commit into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The os.closewalk() implementation now uses the libc fdwalk() API on
platforms where it is available.
25 changes: 25 additions & 0 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -8419,6 +8419,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

Expand All @@ -8433,11 +8448,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;
Expand Down
2 changes: 1 addition & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
3 changes: 3 additions & 0 deletions pyconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,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

Expand Down