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.
This commit is contained in:
Victor Stinner 2018-06-05 00:36:42 +02:00 committed by GitHub
parent 146351860a
commit 270581905c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 0 deletions

View File

@ -199,6 +199,7 @@ class PosixTester(unittest.TestCase):
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")

View File

@ -821,6 +821,7 @@ class test_NamedTemporaryFile(TC):
old_fdopen = os.fdopen
closed = []
def close(fd):
old_close(fd)
closed.append(fd)
def fdopen(*args):
raise ValueError()