[Python-checkins] bpo-33773: Fix test.support.fd_count() on Linux/FreBSD (GH-7421)

Miss Islington (bot) webhook-mailer at python.org
Wed Jun 6 12:09:28 EDT 2018


https://github.com/python/cpython/commit/97fe1b493df979956c66c57095bc7fce22104faf
commit: 97fe1b493df979956c66c57095bc7fce22104faf
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018年06月06日T09:09:18-07:00
summary:
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.
(cherry picked from commit 492d6424a7ca907c8ec1df21e51062e8f3d88e32)
Co-authored-by: Victor Stinner <vstinner at redhat.com>
files:
M Lib/test/support/__init__.py
M Lib/test/test_support.py
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 94e4aafab302..20c1edc53c21 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -2741,10 +2741,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
@@ -2762,13 +2771,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 fff0fc742f36..6e0c122c4fe9 100644
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -451,6 +451,17 @@ def id(self):
 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


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /