Skip to content

bpo-40422: move _Py_closerange to core #22680

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

Merged
merged 3 commits into from
Oct 13, 2020
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
2 changes: 2 additions & 0 deletions Include/internal/pycore_fileutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ PyAPI_FUNC(int) _Py_GetLocaleconvNumeric(
PyObject **decimal_point,
PyObject **thousands_sep);

PyAPI_FUNC(void) _Py_closerange(int first, int last);

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion Modules/Setup
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ _symtable symtablemodule.c
#termios termios.c # Steen Lumholt's termios module
#resource resource.c # Jeremy Hylton's rlimit interface

#_posixsubprocess _posixsubprocess.c # POSIX subprocess module helper
#_posixsubprocess -DPy_BUILD_CORE_BUILTIN _posixsubprocess.c # POSIX subprocess module helper

# Multimedia modules -- off by default.
# These don't work for 64-bit platforms!!!
Expand Down
1 change: 1 addition & 0 deletions Modules/_posixsubprocess.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* Authors: Gregory P. Smith & Jeffrey Yasskin */
#include "Python.h"
#include "pycore_fileutils.h"
#if defined(HAVE_PIPE2) && !defined(_GNU_SOURCE)
# define _GNU_SOURCE
#endif
Expand Down
77 changes: 1 addition & 76 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define PY_SSIZE_T_CLEAN

#include "Python.h"
#include "pycore_fileutils.h"
#ifdef MS_WINDOWS
/* include <windows.h> early to avoid conflict with pycore_condvar.h:

Expand Down Expand Up @@ -8740,82 +8741,6 @@ os_close_impl(PyObject *module, int fd)
Py_RETURN_NONE;
}

/* Our selection logic for which function to use is as follows:
* 1. If close_range(2) is available, always prefer that; it's better for
* contiguous ranges like this than fdwalk(3) which entails iterating over
* the entire fd space and simply doing nothing for those outside the range.
* 2. If closefrom(2) is available, we'll attempt to use that next if we're
* closing up to sysconf(_SC_OPEN_MAX).
* 2a. Fallback to fdwalk(3) if we're not closing up to sysconf(_SC_OPEN_MAX),
* as that will be more performant if the range happens to have any chunk of
* non-opened fd in the middle.
* 2b. If fdwalk(3) isn't available, just do a plain close(2) loop.
*/
#ifdef __FreeBSD__
#define USE_CLOSEFROM
#endif /* __FreeBSD__ */

#ifdef HAVE_FDWALK
#define USE_FDWALK
#endif /* HAVE_FDWALK */

#ifdef USE_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) {
/* Ignore errors */
(void)close(fd);
}
return 0;
}
#endif /* USE_FDWALK */

/* Closes all file descriptors in [first, last], ignoring errors. */
void
_Py_closerange(int first, int last)
{
first = Py_MAX(first, 0);
_Py_BEGIN_SUPPRESS_IPH
#ifdef HAVE_CLOSE_RANGE
if (close_range(first, last, 0) == 0 || errno != ENOSYS) {
/* Any errors encountered while closing file descriptors are ignored;
* ENOSYS means no kernel support, though,
* so we'll fallback to the other methods. */
}
else
#endif /* HAVE_CLOSE_RANGE */
#ifdef USE_CLOSEFROM
if (last >= sysconf(_SC_OPEN_MAX)) {
/* Any errors encountered while closing file descriptors are ignored */
closefrom(first);
}
else
#endif /* USE_CLOSEFROM */
#ifdef USE_FDWALK
{
int lohi[2];
lohi[0] = first;
lohi[1] = last + 1;
fdwalk(_fdwalk_close_func, lohi);
}
#else
{
for (int i = first; i <= last; i++) {
/* Ignore errors */
(void)close(i);
}
}
#endif /* USE_FDWALK */
_Py_END_SUPPRESS_IPH
}

/*[clinic input]
os.closerange

Expand Down
2 changes: 0 additions & 2 deletions Modules/posixmodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ PyAPI_FUNC(int) _Py_Sigset_Converter(PyObject *, void *);
#endif /* HAVE_SIGSET_T */
#endif /* Py_LIMITED_API */

PyAPI_FUNC(void) _Py_closerange(int first, int last);

#ifdef __cplusplus
}
#endif
Expand Down
76 changes: 76 additions & 0 deletions Python/fileutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -2106,3 +2106,79 @@ _Py_GetLocaleconvNumeric(struct lconv *lc,
PyMem_Free(oldloc);
return res;
}

/* Our selection logic for which function to use is as follows:
* 1. If close_range(2) is available, always prefer that; it's better for
* contiguous ranges like this than fdwalk(3) which entails iterating over
* the entire fd space and simply doing nothing for those outside the range.
* 2. If closefrom(2) is available, we'll attempt to use that next if we're
* closing up to sysconf(_SC_OPEN_MAX).
* 2a. Fallback to fdwalk(3) if we're not closing up to sysconf(_SC_OPEN_MAX),
* as that will be more performant if the range happens to have any chunk of
* non-opened fd in the middle.
* 2b. If fdwalk(3) isn't available, just do a plain close(2) loop.
*/
#ifdef __FreeBSD__
# define USE_CLOSEFROM
#endif /* __FreeBSD__ */

#ifdef HAVE_FDWALK
# define USE_FDWALK
#endif /* HAVE_FDWALK */

#ifdef USE_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) {
/* Ignore errors */
(void)close(fd);
}
return 0;
}
#endif /* USE_FDWALK */

/* Closes all file descriptors in [first, last], ignoring errors. */
void
_Py_closerange(int first, int last)
{
first = Py_MAX(first, 0);
_Py_BEGIN_SUPPRESS_IPH
#ifdef HAVE_CLOSE_RANGE
if (close_range(first, last, 0) == 0 || errno != ENOSYS) {
/* Any errors encountered while closing file descriptors are ignored;
* ENOSYS means no kernel support, though,
* so we'll fallback to the other methods. */
}
else
#endif /* HAVE_CLOSE_RANGE */
#ifdef USE_CLOSEFROM
if (last >= sysconf(_SC_OPEN_MAX)) {
/* Any errors encountered while closing file descriptors are ignored */
closefrom(first);
}
else
#endif /* USE_CLOSEFROM */
#ifdef USE_FDWALK
{
int lohi[2];
lohi[0] = first;
lohi[1] = last + 1;
fdwalk(_fdwalk_close_func, lohi);
}
#else
{
for (int i = first; i <= last; i++) {
/* Ignore errors */
(void)close(i);
}
}
#endif /* USE_FDWALK */
_Py_END_SUPPRESS_IPH
}
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,8 @@ def detect_simple_extensions(self):
self.add(Extension('_csv', ['_csv.c']))

# POSIX subprocess module helper.
self.add(Extension('_posixsubprocess', ['_posixsubprocess.c']))
self.add(Extension('_posixsubprocess', ['_posixsubprocess.c'],
extra_compile_args=['-DPy_BUILD_CORE_MODULE']))

def detect_test_extensions(self):
# Python C API test module
Expand Down