]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40422: Move _Py_closerange to fileutils.c (GH-22680)
authorKyle Evans <kevans91@users.noreply.github.com>
Tue, 13 Oct 2020 20:04:44 +0000 (15:04 -0500)
committerGitHub <noreply@github.com>
Tue, 13 Oct 2020 20:04:44 +0000 (22:04 +0200)
This API is relatively lightweight and organizationally, given that it's
used by multiple modules, it makes sense to move it to fileutils.

Requires making sure that _posixsubprocess is compiled with the appropriate
Py_BUIILD_CORE_BUILTIN macro.

Include/internal/pycore_fileutils.h
Modules/Setup
Modules/_posixsubprocess.c
Modules/posixmodule.c
Modules/posixmodule.h
Python/fileutils.c
setup.py

index bbee58617fd05e45ddc9adf146cf3ebb903e4551..9cb5fc66ee2e03cfc4acd201aca873767ee5981c 100644 (file)
@@ -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
index 470bf6bc2efbf5b4debecffe07c40e714a1341b4..87f3a7cb43a0257bcba8ffc15dc70aca7458736c 100644 (file)
@@ -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!!!
index ed046fc5c1ba9f39575d64c45423e1820e52ca91..d08c47980e9c6a926d8989dd46de620ee4abb3ac 100644 (file)
@@ -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
index de81db8b84fe12080127bd59f5112c06f607c4a6..6ce0bcb9fe8ca487dbd34fb60017db8ab1222314 100644 (file)
@@ -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:
 
@@ -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
 
index 749833f71cd4de5ab420ebdbc60eeaf4594d65d0..1e00562abc3370eb44631b6c065f47e2d995fdd7 100644 (file)
@@ -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
index 50ef3c174acc8441cb796e8107e975519c02b34c..b79067f2b5d38b8047168cab51f4d066b9086b7f 100644 (file)
@@ -2106,3 +2106,79 @@ done:
     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
+}
index 476f8c414978eadffd5559f9e297be1a4dfb91eb..d3fd7bca6438afd327cb85a956771b141ad8139d 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -950,7 +950,8 @@ class PyBuildExt(build_ext):
         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