summaryrefslogtreecommitdiffstats
path: root/Modules/posixmodule.c
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-09-12 11:19:21 (GMT)
committerGitHub <noreply@github.com>2019-09-12 11:19:21 (GMT)
commit84eb42e3c90c03f13262f19185b14566e10941ac (patch)
tree6d8b2e13f739d8890712fe0a71e798cf2a07ec3c /Modules/posixmodule.c
parentdb0d8a5b2c803d30d9df436e00b6627ec8e09a13 (diff)
downloadcpython-84eb42e3c90c03f13262f19185b14566e10941ac.zip
cpython-84eb42e3c90c03f13262f19185b14566e10941ac.tar.gz
cpython-84eb42e3c90c03f13262f19185b14566e10941ac.tar.bz2
bpo-38110: Use fdwalk for os.closerange() when available. (GH-15224)
Use fdwalk() on platforms that support it to implement os.closerange(). (cherry picked from commit e20134f889a0cfcc37a46979f31a1c98b800de07) Co-authored-by: Jakub KulĂ­k <Kulikjak@gmail.com>
Diffstat (limited to 'Modules/posixmodule.c')
-rw-r--r--Modules/posixmodule.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index cf6c00b..a066eb1 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -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
@@ -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;