Use nullcontext in case when lock is None. Shutdown pool after code formatting. (#928)

This commit is contained in:
Yurii Karabas 2019-07-16 20:45:56 +03:00 committed by Zsolt Dollenstein
parent f3bb22a828
commit 90d205d1f5

View File

@ -1,5 +1,6 @@
import asyncio import asyncio
from concurrent.futures import Executor, ProcessPoolExecutor from concurrent.futures import Executor, ProcessPoolExecutor
from contextlib import contextmanager
from datetime import datetime from datetime import datetime
from enum import Enum from enum import Enum
from functools import lru_cache, partial, wraps from functools import lru_cache, partial, wraps
@ -523,6 +524,7 @@ def reformat_many(
) )
finally: finally:
shutdown(loop) shutdown(loop)
executor.shutdown()
async def schedule_formatting( async def schedule_formatting(
@ -628,9 +630,8 @@ def format_file_in_place(
src_name = f"{src}\t{then} +0000" src_name = f"{src}\t{then} +0000"
dst_name = f"{src}\t{now} +0000" dst_name = f"{src}\t{now} +0000"
diff_contents = diff(src_contents, dst_contents, src_name, dst_name) diff_contents = diff(src_contents, dst_contents, src_name, dst_name)
if lock:
lock.acquire() with lock or nullcontext():
try:
f = io.TextIOWrapper( f = io.TextIOWrapper(
sys.stdout.buffer, sys.stdout.buffer,
encoding=encoding, encoding=encoding,
@ -639,9 +640,7 @@ def format_file_in_place(
) )
f.write(diff_contents) f.write(diff_contents)
f.detach() f.detach()
finally:
if lock:
lock.release()
return True return True
@ -3593,6 +3592,13 @@ def dump_to_file(*output: str) -> str:
return f.name return f.name
@contextmanager
def nullcontext() -> Iterator[None]:
"""Return context manager that does nothing.
Similar to `nullcontext` from python 3.7"""
yield
def diff(a: str, b: str, a_name: str, b_name: str) -> str: def diff(a: str, b: str, a_name: str, b_name: str) -> str:
"""Return a unified diff string between strings `a` and `b`.""" """Return a unified diff string between strings `a` and `b`."""
import difflib import difflib