From 7155769f064d9f18570def09d7cc8c6cd011d39a Mon Sep 17 00:00:00 2001 From: Jan Kiszka Date: Wed, 7 Nov 2018 14:48:31 +0100 Subject: [PATCH] libkas: Ensure Python 3.7 compatibility In 3.7, async became a reserved word. So we can no longer use it directly for pre-3.5 compatibility. At this chance, extract the common compat part into a helper function. Signed-off-by: Jan Kiszka --- kas/libkas.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/kas/libkas.py b/kas/libkas.py index 2706796..bff2d17 100644 --- a/kas/libkas.py +++ b/kas/libkas.py @@ -157,18 +157,24 @@ def find_program(paths, name): return None +def _create_task(routine): + try: + creation_func = asyncio.ensure_future + except AttributeError: + # for Python < 3.5, avoiding the keyword 'async' introduced in 3.7 + creation_func = getattr(asyncio, 'async') + + # pylint: disable=deprecated-method + return creation_func(routine) + + def repos_fetch(repos): """ Fetches the list of repositories to the kas_work_dir. """ tasks = [] for repo in repos: - if not hasattr(asyncio, 'ensure_future'): - # pylint: disable=no-member,deprecated-method - task = asyncio.async(repo.fetch_async()) - else: - task = asyncio.ensure_future(repo.fetch_async()) - tasks.append(task) + tasks.append(_create_task(repo.fetch_async())) loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.wait(tasks)) @@ -184,12 +190,7 @@ def repos_apply_patches(repos): """ tasks = [] for repo in repos: - if not hasattr(asyncio, 'ensure_future'): - # pylint: disable=no-member,deprecated-method - task = asyncio.async(repo.apply_patches_async()) - else: - task = asyncio.ensure_future(repo.apply_patches_async()) - tasks.append(task) + tasks.append(_create_task(repo.apply_patches_async())) loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.wait(tasks))