diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-03-05 10:06:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-05 10:06:26 +0200 |
commit | 5b10b9824780b2181158902067912ee9e7b04657 (patch) | |
tree | 1c89bea944e6638eb008c8f106b2ee48cc9448d1 /Lib/test/support/__init__.py | |
parent | 9e4861f52349011cd5916eef8e8344575e8ac426 (diff) | |
download | cpython-5b10b9824780b2181158902067912ee9e7b04657.tar.gz cpython-5b10b9824780b2181158902067912ee9e7b04657.zip |
bpo-22831: Use "with" to avoid possible fd leaks in tests (part 2). (GH-10929)
Diffstat (limited to 'Lib/test/support/__init__.py')
-rw-r--r-- | Lib/test/support/__init__.py | 14 |
1 files changed, 7 insertions, 7 deletions
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): |