diff options
author | Victor Stinner <vstinner@redhat.com> | 2018-06-06 17:04:50 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-06 17:04:50 (GMT) |
commit | 67b7158d53f33ed644cc11ef394470a859ea8bad (patch) | |
tree | 0832d9af25f0afd10cbb4d66d15abb6099dd2cc2 | |
parent | e5b79c546370521764457ea2ec809303e580f5ea (diff) | |
download | cpython-67b7158d53f33ed644cc11ef394470a859ea8bad.zip cpython-67b7158d53f33ed644cc11ef394470a859ea8bad.tar.gz cpython-67b7158d53f33ed644cc11ef394470a859ea8bad.tar.bz2 |
bpo-33773: Fix test.support.fd_count() on Linux/FreBSD (GH-7421) (GH-7456)
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.
(cherry picked from commit 492d6424a7ca907c8ec1df21e51062e8f3d88e32)
-rw-r--r-- | Lib/test/support/__init__.py | 18 | ||||
-rw-r--r-- | Lib/test/test_test_support.py | 11 |
2 files changed, 21 insertions, 8 deletions
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index f67c0da..6da2597 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -2068,11 +2068,20 @@ 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 OSError as exc: if exc.errno != errno.ENOENT: raise + 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 @@ -2090,13 +2099,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_test_support.py b/Lib/test/test_test_support.py index 421a9ef..60476b7 100644 --- a/Lib/test/test_test_support.py +++ b/Lib/test/test_test_support.py @@ -417,6 +417,17 @@ class TestSupport(unittest.TestCase): self.assertTrue(support.match_test(test_access)) self.assertFalse(support.match_test(test_chdir)) + def test_fd_count(self): + # 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 |