https://github.com/python/cpython/commit/b3e6eb1931a3e587f0d0c7ad0be4989f56feeb40
commit: b3e6eb1931a3e587f0d0c7ad0be4989f56feeb40
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: vstinner <[email protected]>
date: 2026-10-22T08:38:09Z
summary:

[3.15] On FreeBSD, use /dev/fd/ in fd_count() if it's mounted (GH-160210) 
(#160220)

On FreeBSD, use /dev/fd/ in fd_count() if it's mounted (GH-160210)

fd_count() can now use /dev/fd/ fdescfs file system if it's mounted,
which is not the case by default. Otherwise, call os.dup(fd) in a
loop which is slow since os.sysconf("SC_OPEN_MAX") is larger than
200,000 by default.
(cherry picked from commit 226ca06ce8474d19fc26ff2568268a3c99c84d1c)

Co-authored-by: Victor Stinner <[email protected]>

files:
M Lib/test/support/os_helper.py

diff --git a/Lib/test/support/os_helper.py b/Lib/test/support/os_helper.py
index 12d34dedfe1bbaa..183a8681271e9cf 100644
--- a/Lib/test/support/os_helper.py
+++ b/Lib/test/support/os_helper.py
@@ -686,6 +686,24 @@ def fd_count():
         except FileNotFoundError:
             pass
 
+    if sys.platform == "freebsd" and fd_count._dev_fd_mounted:
+        # Check if /dev/fd/ fdescfs file system is mounted. By default, it's
+        # not mounted and /dev/fd/ always contains 3 files (0, 1, 2).
+        try:
+            dirfd = os.open("/dev/fd", os.O_RDONLY)
+            try:
+                if os.path.exists(f"/dev/fd/{dirfd}"):
+                    names = list(os.scandir(dirfd))
+                    # Don't count dirfd
+                    return len(names) - 1
+                else:
+                    # fdescfs is not mounted
+                    fd_count._dev_fd_mounted = False
+            finally:
+                os.close(dirfd)
+        except FileNotFoundError:
+            pass
+
     MAXFD = 256
     if hasattr(os, 'sysconf'):
         try:
@@ -733,6 +751,9 @@ def fd_count():
 
     return count
 
+# On FreeBSD, flag set to False if /dev/fd/ is not mounted as fdescfs
+fd_count._dev_fd_mounted = True
+
 
 if hasattr(os, "umask"):
     @contextlib.contextmanager

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to