Skip to content

Commit 492d642

Browse files
authored
bpo-33773: Fix test.support.fd_count() on Linux/FreBSD (GH-7421)
Substract one because listdir() opens internally a file descriptor to list the content of the /proc/self/fd/ directory. Add test_support.test_fd_count(). Move also MAXFD code before msvcrt.CrtSetReportMode(), to make sure that the report mode is always restored on failure.
1 parent 45e4efb commit 492d642

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

Lib/test/support/__init__.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,10 +2768,19 @@ def fd_count():
27682768
if sys.platform.startswith(('linux', 'freebsd')):
27692769
try:
27702770
names = os.listdir("/proc/self/fd")
2771-
return len(names)
2771+
# Substract one because listdir() opens internally a file
2772+
# descriptor to list the content of the /proc/self/fd/ directory.
2773+
return len(names) - 1
27722774
except FileNotFoundError:
27732775
pass
27742776

2777+
MAXFD = 256
2778+
if hasattr(os, 'sysconf'):
2779+
try:
2780+
MAXFD = os.sysconf("SC_OPEN_MAX")
2781+
except OSError:
2782+
pass
2783+
27752784
old_modes = None
27762785
if sys.platform == 'win32':
27772786
# bpo-25306, bpo-31009: Call CrtSetReportMode() to not kill the process
@@ -2789,13 +2798,6 @@ def fd_count():
27892798
msvcrt.CRT_ASSERT):
27902799
old_modes[report_type] = msvcrt.CrtSetReportMode(report_type, 0)
27912800

2792-
MAXFD = 256
2793-
if hasattr(os, 'sysconf'):
2794-
try:
2795-
MAXFD = os.sysconf("SC_OPEN_MAX")
2796-
except OSError:
2797-
pass
2798-
27992801
try:
28002802
count = 0
28012803
for fd in range(MAXFD):

Lib/test/test_support.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,17 @@ def id(self):
569569
self.assertTrue(support.match_test(test_access))
570570
self.assertFalse(support.match_test(test_chdir))
571571

572+
def test_fd_count(self):
573+
# We cannot test the absolute value of fd_count(): on old Linux
574+
# kernel or glibc versions, os.urandom() keeps a FD open on
575+
# /dev/urandom device and Python has 4 FD opens instead of 3.
576+
start = support.fd_count()
577+
fd = os.open(__file__, os.O_RDONLY)
578+
try:
579+
more = support.fd_count()
580+
finally:
581+
os.close(fd)
582+
self.assertEqual(more - start, 1)
572583

573584
# XXX -follows a list of untested API
574585
# make_legacy_pyc

0 commit comments

Comments
 (0)