From: Daniele Varrazzo Date: Mon, 7 Aug 2023 16:09:26 +0000 (+0100) Subject: test: fix use of aclosing in Python < 3.10 X-Git-Tag: pool-3.2.0~12^2~61 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=08370c138beb04f3ceef9626ab3dd6358210aa8c;p=thirdparty%2Fpsycopg.git test: fix use of aclosing in Python < 3.10 --- diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 18376125e..622d9d1f9 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -8,7 +8,6 @@ Tests common to psycopg.Cursor and its subclasses. import weakref import datetime as dt from typing import Any, List -from contextlib import closing import pytest @@ -17,7 +16,7 @@ from psycopg import sql, rows from psycopg.adapt import PyFormat from psycopg.types import TypeInfo -from .utils import gc_collect, raiseif +from .utils import gc_collect, raiseif, closing from .fix_crdb import crdb_encoding from ._test_cursor import my_row_factory, ph from ._test_cursor import execmany, _execmany # noqa: F401 diff --git a/tests/test_cursor_async.py b/tests/test_cursor_async.py index 155d23ad9..c0c49e68a 100644 --- a/tests/test_cursor_async.py +++ b/tests/test_cursor_async.py @@ -5,7 +5,6 @@ Tests common to psycopg.AsyncCursor and its subclasses. import weakref import datetime as dt from typing import Any, List -from contextlib import aclosing import pytest @@ -14,8 +13,7 @@ from psycopg import sql, rows from psycopg.adapt import PyFormat from psycopg.types import TypeInfo -from .utils import alist, anext -from .utils import gc_collect, raiseif +from .utils import gc_collect, raiseif, aclosing, alist, anext from .fix_crdb import crdb_encoding from ._test_cursor import my_row_factory, ph from ._test_cursor import execmany, _execmany # noqa: F401 diff --git a/tests/utils.py b/tests/utils.py index 57252e0dc..f20ca7920 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -3,7 +3,8 @@ import re import sys import operator from typing import Callable, Optional, Tuple -from contextlib import contextmanager +from contextlib import contextmanager, asynccontextmanager +from contextlib import closing as closing # noqa: F401 - re-export import pytest @@ -177,11 +178,25 @@ def gc_count() -> int: async def alist(it): + """Consume an async iterator into a list. Async equivalent of list(it).""" return [i async for i in it] -async def anext(it): - return await it.__anext__() +if sys.version_info >= (3, 10): + from builtins import anext as anext + from contextlib import aclosing as aclosing + +else: + + async def anext(it): + return await it.__anext__() + + @asynccontextmanager + async def aclosing(thing): + try: + yield thing + finally: + await thing.aclose() @contextmanager diff --git a/tools/async_to_sync.py b/tools/async_to_sync.py index 2e6f98dc1..90ab125aa 100755 --- a/tools/async_to_sync.py +++ b/tools/async_to_sync.py @@ -114,9 +114,10 @@ class RenameAsyncToSync(ast.NodeTransformer): _skip_imports = {"alist", "anext"} def visit_ImportFrom(self, node: ast.ImportFrom) -> ast.AST | None: - # Remove import of async utils eclypsing builtings + # Remove import of async utils eclypsing builtins if node.module == "utils": - if {n.name for n in node.names} <= self._skip_imports: + node.names = [n for n in node.names if n.name not in self._skip_imports] + if not node.names: return None for n in node.names: