# HG changeset patch # User Matt Harbison # Date 2020-11-29 13:47:35 # Node ID 064449f9fdc2900079ffa40131f2e7951acf7ffd # Parent a120d1c9c70445ab2eba9f6c40415ffcb12b3560 run-tests: use a context manager when looking for available ports Differential Revision: https://phab.mercurial-scm.org/D9441 Differential Revision: https://phab.mercurial-scm.org/D9452 diff --git a/tests/run-tests.py b/tests/run-tests.py --- a/tests/run-tests.py +++ b/tests/run-tests.py @@ -47,6 +47,7 @@ from __future__ import absolute_import, import argparse import collections +import contextlib import difflib import distutils.version as version import errno @@ -255,9 +256,8 @@ def checkportisavailable(port): else: family = socket.AF_INET try: - s = socket.socket(family, socket.SOCK_STREAM) - s.bind(('localhost', port)) - s.close() + with contextlib.closing(socket.socket(family, socket.SOCK_STREAM)) as s: + s.bind(('localhost', port)) return True except socket.error as exc: if exc.errno not in (