mtest: simplify deprecated access to current loop

These functions constantly want the current asyncio loop, and we run a
function call each time to get it. And the function call is a deprecated
one. Python 3.7 brings the more explicit get_running_loop for use when
we know we're inside one, with the aim of getting rid of get_event_loop
once support for python <3.7 disappears. Meson no longer supports python
<3.7 either.

Switch to the new API, and save the reference for reuse instead of
constantly re-calculating it.
pull/11215/head
Eli Schwartz 2 years ago
parent ab8081fab6
commit 40315e6ebb
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
  1. 20
      mesonbuild/mtest.py

@ -574,7 +574,7 @@ class ConsoleLogger(TestLogger):
def start(self, harness: 'TestHarness') -> None: def start(self, harness: 'TestHarness') -> None:
async def report_progress() -> None: async def report_progress() -> None:
loop = asyncio.get_event_loop() loop = asyncio.get_running_loop()
next_update = 0.0 next_update = 0.0
self.request_update() self.request_update()
while not self.stop: while not self.stop:
@ -1232,13 +1232,14 @@ async def complete_all(futures: T.Iterable[asyncio.Future],
# Python is silly and does not have a variant of asyncio.wait with an # Python is silly and does not have a variant of asyncio.wait with an
# absolute time as deadline. # absolute time as deadline.
deadline = None if timeout is None else asyncio.get_event_loop().time() + timeout loop = asyncio.get_running_loop()
deadline = None if timeout is None else loop.time() + timeout
while futures and (timeout is None or timeout > 0): while futures and (timeout is None or timeout > 0):
done, futures = await asyncio.wait(futures, timeout=timeout, done, futures = await asyncio.wait(futures, timeout=timeout,
return_when=asyncio.FIRST_EXCEPTION) return_when=asyncio.FIRST_EXCEPTION)
check_futures(done) check_futures(done)
if deadline: if deadline:
timeout = deadline - asyncio.get_event_loop().time() timeout = deadline - loop.time()
check_futures(futures) check_futures(futures)
@ -1948,6 +1949,7 @@ class TestHarness:
running_tests = {} # type: T.Dict[asyncio.Future, str] running_tests = {} # type: T.Dict[asyncio.Future, str]
interrupted = False interrupted = False
ctrlc_times = deque(maxlen=MAX_CTRLC) # type: T.Deque[float] ctrlc_times = deque(maxlen=MAX_CTRLC) # type: T.Deque[float]
loop = asyncio.get_running_loop()
async def run_test(test: SingleTestRunner) -> None: async def run_test(test: SingleTestRunner) -> None:
async with semaphore: async with semaphore:
@ -1996,7 +1998,7 @@ class TestHarness:
nonlocal interrupted nonlocal interrupted
if interrupted: if interrupted:
return return
ctrlc_times.append(asyncio.get_event_loop().time()) ctrlc_times.append(loop.time())
if len(ctrlc_times) == MAX_CTRLC and ctrlc_times[-1] - ctrlc_times[0] < 1: if len(ctrlc_times) == MAX_CTRLC and ctrlc_times[-1] - ctrlc_times[0] < 1:
self.flush_logfiles() self.flush_logfiles()
mlog.warning('CTRL-C detected, exiting') mlog.warning('CTRL-C detected, exiting')
@ -2013,10 +2015,10 @@ class TestHarness:
if sys.platform != 'win32': if sys.platform != 'win32':
if os.getpgid(0) == os.getpid(): if os.getpgid(0) == os.getpid():
asyncio.get_event_loop().add_signal_handler(signal.SIGINT, sigint_handler) loop.add_signal_handler(signal.SIGINT, sigint_handler)
else: else:
asyncio.get_event_loop().add_signal_handler(signal.SIGINT, sigterm_handler) loop.add_signal_handler(signal.SIGINT, sigterm_handler)
asyncio.get_event_loop().add_signal_handler(signal.SIGTERM, sigterm_handler) loop.add_signal_handler(signal.SIGTERM, sigterm_handler)
try: try:
for runner in runners: for runner in runners:
if not runner.is_parallel: if not runner.is_parallel:
@ -2033,8 +2035,8 @@ class TestHarness:
await complete_all(futures) await complete_all(futures)
finally: finally:
if sys.platform != 'win32': if sys.platform != 'win32':
asyncio.get_event_loop().remove_signal_handler(signal.SIGINT) loop.remove_signal_handler(signal.SIGINT)
asyncio.get_event_loop().remove_signal_handler(signal.SIGTERM) loop.remove_signal_handler(signal.SIGTERM)
for l in self.loggers: for l in self.loggers:
await l.finish(self) await l.finish(self)

Loading…
Cancel
Save