summaryrefslogtreecommitdiffstats
path: root/Lib/test/support
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2018-06-06 15:23:50 (GMT)
committerGitHub <noreply@github.com>2018-06-06 15:23:50 (GMT)
commit492d6424a7ca907c8ec1df21e51062e8f3d88e32 (patch)
treed88718e085da33d6a105054e201611ee3aca1ddf /Lib/test/support
parent45e4efba7fa2abe61d25e4f8b5bf482e19ff1280 (diff)
downloadcpython-492d6424a7ca907c8ec1df21e51062e8f3d88e32.zip
cpython-492d6424a7ca907c8ec1df21e51062e8f3d88e32.tar.gz
cpython-492d6424a7ca907c8ec1df21e51062e8f3d88e32.tar.bz2
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.
Diffstat (limited to 'Lib/test/support')
-rw-r--r--Lib/test/support/__init__.py18
1 files changed, 10 insertions, 8 deletions
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index a3a42cd..d8dabd4 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -2768,10 +2768,19 @@ def fd_count():
if sys.platform.startswith(('linux', 'freebsd')):
try:
names = os.listdir("/proc/self/fd")
- return len(names)
+ # Substract one because listdir() opens internally a file
+ # descriptor to list the content of the /proc/self/fd/ directory.
+ return len(names) - 1
except FileNotFoundError:
pass
+ MAXFD = 256
+ if hasattr(os, 'sysconf'):
+ try:
+ MAXFD = os.sysconf("SC_OPEN_MAX")
+ except OSError:
+ pass
+
old_modes = None
if sys.platform == 'win32':
# bpo-25306, bpo-31009: Call CrtSetReportMode() to not kill the process
@@ -2789,13 +2798,6 @@ def fd_count():
msvcrt.CRT_ASSERT):
old_modes[report_type] = msvcrt.CrtSetReportMode(report_type, 0)
- MAXFD = 256
- if hasattr(os, 'sysconf'):
- try:
- MAXFD = os.sysconf("SC_OPEN_MAX")
- except OSError:
- pass
-
try:
count = 0
for fd in range(MAXFD):