|
22 | 22 | #define PY_SSIZE_T_CLEAN
|
23 | 23 |
|
24 | 24 | #include "Python.h"
|
| 25 | +#include "pycore_fileutils.h" |
25 | 26 | #ifdef MS_WINDOWS
|
26 | 27 | /* include <windows.h> early to avoid conflict with pycore_condvar.h:
|
27 | 28 |
|
@@ -8740,82 +8741,6 @@ os_close_impl(PyObject *module, int fd)
|
8740 | 8741 | Py_RETURN_NONE;
|
8741 | 8742 | }
|
8742 | 8743 |
|
8743 |
| -/* Our selection logic for which function to use is as follows: |
8744 |
| - * 1. If close_range(2) is available, always prefer that; it's better for |
8745 |
| - * contiguous ranges like this than fdwalk(3) which entails iterating over |
8746 |
| - * the entire fd space and simply doing nothing for those outside the range. |
8747 |
| - * 2. If closefrom(2) is available, we'll attempt to use that next if we're |
8748 |
| - * closing up to sysconf(_SC_OPEN_MAX). |
8749 |
| - * 2a. Fallback to fdwalk(3) if we're not closing up to sysconf(_SC_OPEN_MAX), |
8750 |
| - * as that will be more performant if the range happens to have any chunk of |
8751 |
| - * non-opened fd in the middle. |
8752 |
| - * 2b. If fdwalk(3) isn't available, just do a plain close(2) loop. |
8753 |
| - */ |
8754 |
| -#ifdef __FreeBSD__ |
8755 |
| -#define USE_CLOSEFROM |
8756 |
| -#endif /* __FreeBSD__ */ |
8757 |
| - |
8758 |
| -#ifdef HAVE_FDWALK |
8759 |
| -#define USE_FDWALK |
8760 |
| -#endif /* HAVE_FDWALK */ |
8761 |
| - |
8762 |
| -#ifdef USE_FDWALK |
8763 |
| -static int |
8764 |
| -_fdwalk_close_func(void *lohi, int fd) |
8765 |
| -{ |
8766 |
| - int lo = ((int *)lohi)[0]; |
8767 |
| - int hi = ((int *)lohi)[1]; |
8768 |
| - |
8769 |
| - if (fd >= hi) { |
8770 |
| - return 1; |
8771 |
| - } |
8772 |
| - else if (fd >= lo) { |
8773 |
| - /* Ignore errors */ |
8774 |
| - (void)close(fd); |
8775 |
| - } |
8776 |
| - return 0; |
8777 |
| -} |
8778 |
| -#endif /* USE_FDWALK */ |
8779 |
| - |
8780 |
| -/* Closes all file descriptors in [first, last], ignoring errors. */ |
8781 |
| -void |
8782 |
| -_Py_closerange(int first, int last) |
8783 |
| -{ |
8784 |
| - first = Py_MAX(first, 0); |
8785 |
| - _Py_BEGIN_SUPPRESS_IPH |
8786 |
| -#ifdef HAVE_CLOSE_RANGE |
8787 |
| - if (close_range(first, last, 0) == 0 || errno != ENOSYS) { |
8788 |
| - /* Any errors encountered while closing file descriptors are ignored; |
8789 |
| - * ENOSYS means no kernel support, though, |
8790 |
| - * so we'll fallback to the other methods. */ |
8791 |
| - } |
8792 |
| - else |
8793 |
| -#endif /* HAVE_CLOSE_RANGE */ |
8794 |
| -#ifdef USE_CLOSEFROM |
8795 |
| - if (last >= sysconf(_SC_OPEN_MAX)) { |
8796 |
| - /* Any errors encountered while closing file descriptors are ignored */ |
8797 |
| - closefrom(first); |
8798 |
| - } |
8799 |
| - else |
8800 |
| -#endif /* USE_CLOSEFROM */ |
8801 |
| -#ifdef USE_FDWALK |
8802 |
| - { |
8803 |
| - int lohi[2]; |
8804 |
| - lohi[0] = first; |
8805 |
| - lohi[1] = last + 1; |
8806 |
| - fdwalk(_fdwalk_close_func, lohi); |
8807 |
| - } |
8808 |
| -#else |
8809 |
| - { |
8810 |
| - for (int i = first; i <= last; i++) { |
8811 |
| - /* Ignore errors */ |
8812 |
| - (void)close(i); |
8813 |
| - } |
8814 |
| - } |
8815 |
| -#endif /* USE_FDWALK */ |
8816 |
| - _Py_END_SUPPRESS_IPH |
8817 |
| -} |
8818 |
| - |
8819 | 8744 | /*[clinic input]
|
8820 | 8745 | os.closerange
|
8821 | 8746 |
|
|
0 commit comments