From fad84c771cfde67cf29f54642f2634968276ff4b Mon Sep 17 00:00:00 2001 From: Jonathan Moody <103143855+moodyjon@users.noreply.github.com> Date: Fri, 29 Apr 2022 08:39:31 -0400 Subject: [PATCH] Support official contextlib.aclosing() when it's available. --- lbry/utils.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lbry/utils.py b/lbry/utils.py index cebba675e..75f6c7a58 100644 --- a/lbry/utils.py +++ b/lbry/utils.py @@ -130,12 +130,16 @@ def get_sd_hash(stream_info): def json_dumps_pretty(obj, **kwargs): return json.dumps(obj, sort_keys=True, indent=2, separators=(',', ': '), **kwargs) -@contextlib.asynccontextmanager -async def aclosing(thing): - try: - yield thing - finally: - await thing.aclose() +try: + # the standard contextlib.aclosing() is available in 3.10+ + from contextlib import aclosing +except ImportError: + @contextlib.asynccontextmanager + async def aclosing(thing): + try: + yield thing + finally: + await thing.aclose() def async_timed_cache(duration: int): def wrapper(func):