X-Git-Url: https://git.madduck.net/etc/vim.git/blobdiff_plain/f2ea461e9e9fa5c47bb61fd72d512c748928badc..b517dfb396a82ef263f0d366c4dc107451cf0c3c:/src/black/concurrency.py diff --git a/src/black/concurrency.py b/src/black/concurrency.py index 119a9a7..24f67b6 100644 --- a/src/black/concurrency.py +++ b/src/black/concurrency.py @@ -6,6 +6,21 @@ from typing import Any, Iterable from black.output import err +def maybe_install_uvloop() -> None: + """If our environment has uvloop installed we use it. + + This is called only from command-line entry points to avoid + interfering with the parent process if Black is used as a library. + + """ + try: + import uvloop + + uvloop.install() + except ImportError: + pass + + def cancel(tasks: Iterable["asyncio.Task[Any]"]) -> None: """asyncio signal handler that cancels all `tasks` and reports to stderr.""" err("Aborted!") @@ -27,9 +42,12 @@ def shutdown(loop: asyncio.AbstractEventLoop) -> None: for task in to_cancel: task.cancel() - loop.run_until_complete( - asyncio.gather(*to_cancel, loop=loop, return_exceptions=True) - ) + if sys.version_info >= (3, 7): + loop.run_until_complete(asyncio.gather(*to_cancel, return_exceptions=True)) + else: + loop.run_until_complete( + asyncio.gather(*to_cancel, loop=loop, return_exceptions=True) + ) finally: # `concurrent.futures.Future` objects cannot be cancelled once they # are already running. There might be some when the `shutdown()` happened.