From fb87390349bfdfbf9fd404f94c8fdded658e27ee Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 5 Jun 2018 13:26:35 +0200 Subject: [PATCH 1/3] bpo-33773: Fix test.support.fd_count() on Linux/FreBSD Substract one because listdir() opens internally a file descriptor to list the content of the /proc/self/fd/ directory. --- Lib/test/support/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 1015dd9af20272..5053370a4d51c3 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -2768,7 +2768,9 @@ 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 From e801f7b1ee2805f13ac5eb5650b6605d9631cccf Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 6 Jun 2018 12:50:25 +0200 Subject: [PATCH 2/3] 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. --- Lib/test/support/__init__.py | 14 +++++++------- Lib/test/test_support.py | 12 ++++++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index 5053370a4d51c3..f672c551f9fd8d 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -2774,6 +2774,13 @@ def fd_count(): 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 @@ -2791,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): diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index 36d5f849e1ad83..696d00152bebec 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -569,6 +569,18 @@ def id(self): self.assertTrue(support.match_test(test_access)) self.assertFalse(support.match_test(test_chdir)) + def test_fd_count(self): + code = "from test.support import fd_count; print(fd_count())" + proc = script_helper.assert_python_ok("-c", code) + out = proc.out.decode("ascii", "replace").strip() + # A fresh Python process should have exactly 3 open file descriptors + # (stdin, stdout, stderr) even if these standard streams have been + # closed in the parent process. assert_python_ok() opens one pipe + # per stream, so 3 pipes in total (stdin, stdout, stderr). + # + # subprocess.Popen(close_fds=True) and PEP 446 ensure that the child + # process don't inherit file descriptors from its parent. + self.assertEqual(out, "3") # XXX -follows a list of untested API # make_legacy_pyc From 3bd827a170bf602fc71d6eb74893123616113e8c Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 6 Jun 2018 17:07:12 +0200 Subject: [PATCH 3/3] Rewrite test_fd_count() --- Lib/test/test_support.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index 696d00152bebec..89f1fbfb6c2b52 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -570,17 +570,16 @@ def id(self): self.assertFalse(support.match_test(test_chdir)) def test_fd_count(self): - code = "from test.support import fd_count; print(fd_count())" - proc = script_helper.assert_python_ok("-c", code) - out = proc.out.decode("ascii", "replace").strip() - # A fresh Python process should have exactly 3 open file descriptors - # (stdin, stdout, stderr) even if these standard streams have been - # closed in the parent process. assert_python_ok() opens one pipe - # per stream, so 3 pipes in total (stdin, stdout, stderr). - # - # subprocess.Popen(close_fds=True) and PEP 446 ensure that the child - # process don't inherit file descriptors from its parent. - self.assertEqual(out, "3") + # We cannot test the absolute value of fd_count(): on old Linux + # kernel or glibc versions, os.urandom() keeps a FD open on + # /dev/urandom device and Python has 4 FD opens instead of 3. + start = support.fd_count() + fd = os.open(__file__, os.O_RDONLY) + try: + more = support.fd_count() + finally: + os.close(fd) + self.assertEqual(more - start, 1) # XXX -follows a list of untested API # make_legacy_pyc