https://github.com/python/cpython/commit/226ca06ce8474d19fc26ff2568268a3c99c84d1c
commit: 226ca06ce8474d19fc26ff2568268a3c99c84d1c
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-10-22T01:05:04-07:00
summary:

On FreeBSD, use /dev/fd/ in fd_count() if it's mounted (#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.

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 a7baa7abc4ac60..c46c01f50fa13b 100644
--- a/Lib/test/support/os_helper.py
+++ b/Lib/test/support/os_helper.py
@@ -688,6 +688,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:
@@ -735,6 +753,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