From 5b10b9824780b2181158902067912ee9e7b04657 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 5 Mar 2019 10:06:26 +0200 Subject: bpo-22831: Use "with" to avoid possible fd leaks in tests (part 2). (GH-10929) --- Lib/test/support/__init__.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'Lib/test/support/__init__.py') diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index c0938d90010..5bd15a2feae 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -706,9 +706,8 @@ def find_unused_port(family=socket.AF_INET, socktype=socket.SOCK_STREAM): issue if/when we come across it. """ - tempsock = socket.socket(family, socktype) - port = bind_port(tempsock) - tempsock.close() + with socket.socket(family, socktype) as tempsock: + port = bind_port(tempsock) del tempsock return port @@ -1785,10 +1784,11 @@ class _MemoryWatchdog: sys.stderr.flush() return - watchdog_script = findfile("memory_watchdog.py") - self.mem_watchdog = subprocess.Popen([sys.executable, watchdog_script], - stdin=f, stderr=subprocess.DEVNULL) - f.close() + with f: + watchdog_script = findfile("memory_watchdog.py") + self.mem_watchdog = subprocess.Popen([sys.executable, watchdog_script], + stdin=f, + stderr=subprocess.DEVNULL) self.started = True def stop(self): -- cgit v1.2.3