[Python-checkins] bpo-18174: Fix file descriptor leaks in tests (GH-7408)

Victor Stinner webhook-mailer at python.org
Mon Jun 4 18:36:45 EDT 2018


https://github.com/python/cpython/commit/270581905cab2747ae8f7ee945301d6a29509cc7
commit: 270581905cab2747ae8f7ee945301d6a29509cc7
branch: 2.7
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2018-06-05T00:36:42+02:00
summary:

bpo-18174: Fix file descriptor leaks in tests (GH-7408)

* test_tempfile.test_no_leak_fd() mocks os.close() but it doesn't
  call the original os.close() method and so leaks an open file
  descriptor. Fix the test by calling the original os.close()
  function.
* test_posix.test_fdopen_directory(): close the directory file
  descriptor when the test completes.

files:
M Lib/test/test_posix.py
M Lib/test/test_tempfile.py

diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py
index bce4e21e992b..c4283b604b96 100644
--- a/Lib/test/test_posix.py
+++ b/Lib/test/test_posix.py
@@ -199,6 +199,7 @@ def test_fdopen(self):
     def test_fdopen_directory(self):
         try:
             fd = os.open('.', os.O_RDONLY)
+            self.addCleanup(os.close, fd)
         except OSError as e:
             self.assertEqual(e.errno, errno.EACCES)
             self.skipTest("system cannot open directories")
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py
index 5c111a29ca60..2efb8362fe1b 100644
--- a/Lib/test/test_tempfile.py
+++ b/Lib/test/test_tempfile.py
@@ -821,6 +821,7 @@ def test_no_leak_fd(self):
         old_fdopen = os.fdopen
         closed = []
         def close(fd):
+            old_close(fd)
             closed.append(fd)
         def fdopen(*args):
             raise ValueError()



More information about the Python-checkins mailing list