summaryrefslogtreecommitdiffstats
path: root/Lib/test/support
diff options
context:
space:
mode:
authorRonald Oussoren <ronaldoussoren@mac.com>2023-12-07 09:30:15 (GMT)
committerGitHub <noreply@github.com>2023-12-07 09:30:15 (GMT)
commit953ee622b3901d3467e65e3484dcfa75ba6fcddf (patch)
treea5dcb5ddab732674a05a13a8f6fce6d0951a998d /Lib/test/support
parent16448cab44e23d350824e9ac75e699f5bcc48a14 (diff)
downloadcpython-953ee622b3901d3467e65e3484dcfa75ba6fcddf.zip
cpython-953ee622b3901d3467e65e3484dcfa75ba6fcddf.tar.gz
cpython-953ee622b3901d3467e65e3484dcfa75ba6fcddf.tar.bz2
gh-109981: Fix support.fd_count() on macOS 14 (#112797)
Use scanning "/dev/fd/" on macOS in support.fd_count(). That's both more efficient than scanning all possible file descriptors, and avoids crashing the interpreter when there are open "guarded" file descriptors. "Guarded" file descriptors are a macOS feature where file descriptors used by system libraries are marked and cause hard crashes when used by "user" code. Co-authored-by: Victor Stinner <vstinner@python.org>
Diffstat (limited to 'Lib/test/support')
-rw-r--r--Lib/test/support/os_helper.py11
1 files changed, 9 insertions, 2 deletions
diff --git a/Lib/test/support/os_helper.py b/Lib/test/support/os_helper.py
index 46ae53a..7a67d87 100644
--- a/Lib/test/support/os_helper.py
+++ b/Lib/test/support/os_helper.py
@@ -592,10 +592,17 @@ def fd_count():
"""Count the number of open file descriptors.
"""
if sys.platform.startswith(('linux', 'freebsd', 'emscripten')):
+ fd_path = "/proc/self/fd"
+ elif sys.platform == "darwin":
+ fd_path = "/dev/fd"
+ else:
+ fd_path = None
+
+ if fd_path is not None:
try:
- names = os.listdir("/proc/self/fd")
+ names = os.listdir(fd_path)
# Subtract one because listdir() internally opens a file
- # descriptor to list the content of the /proc/self/fd/ directory.
+ # descriptor to list the content of the directory.
return len(names) - 1
except FileNotFoundError:
pass