From: Daniele Varrazzo Date: Sat, 6 Jan 2024 17:06:54 +0000 (+0100) Subject: refactor: generate conninfo attempts from async counterpart X-Git-Tag: pool-3.2.1~3^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=21612cf864d961a89f6d0d17c6740b86c7ca8d28;p=thirdparty%2Fpsycopg.git refactor: generate conninfo attempts from async counterpart --- diff --git a/psycopg/psycopg/_conninfo_attempts.py b/psycopg/psycopg/_conninfo_attempts.py index 5262ab785..4fc0f792a 100644 --- a/psycopg/psycopg/_conninfo_attempts.py +++ b/psycopg/psycopg/_conninfo_attempts.py @@ -1,3 +1,6 @@ +# WARNING: this file is auto-generated by 'async_to_sync.py' +# from the original file '_conninfo_attempts_async.py' +# DO NOT CHANGE! Change the original file instead. """ Separate connection attempts from a connection string. """ @@ -14,6 +17,7 @@ from . import errors as e from ._conninfo_utils import ConnDict, get_param, is_ip_address, get_param_def from ._conninfo_utils import split_attempts + logger = logging.getLogger("psycopg") @@ -52,7 +56,7 @@ def conninfo_attempts(params: ConnDict) -> list[ConnDict]: def _resolve_hostnames(params: ConnDict) -> list[ConnDict]: """ - Perform DNS lookup of the hosts and return a list of connection attempts. + Perform async DNS lookup of the hosts and return a list of connection attempts. If a ``host`` param is present but not ``hostname``, resolve the host addresses asynchronously. @@ -87,4 +91,5 @@ def _resolve_hostnames(params: ConnDict) -> list[ConnDict]: ans = socket.getaddrinfo( host, port, proto=socket.IPPROTO_TCP, type=socket.SOCK_STREAM ) + return [{**params, "hostaddr": item[4][0]} for item in ans] diff --git a/psycopg/psycopg/_conninfo_attempts_async.py b/psycopg/psycopg/_conninfo_attempts_async.py index 037f213ff..6aca4ee3a 100644 --- a/psycopg/psycopg/_conninfo_attempts_async.py +++ b/psycopg/psycopg/_conninfo_attempts_async.py @@ -7,7 +7,6 @@ Separate connection attempts from a connection string. from __future__ import annotations import socket -import asyncio import logging from random import shuffle @@ -15,6 +14,9 @@ from . import errors as e from ._conninfo_utils import ConnDict, get_param, is_ip_address, get_param_def from ._conninfo_utils import split_attempts +if True: # ASYNC: + import asyncio + logger = logging.getLogger("psycopg") @@ -35,7 +37,7 @@ async def conninfo_attempts_async(params: ConnDict) -> list[ConnDict]: attempts = [] for attempt in split_attempts(params): try: - attempts.extend(await _resolve_hostnames_async(attempt)) + attempts.extend(await _resolve_hostnames(attempt)) except OSError as ex: logger.debug("failed to resolve host %r: %s", attempt.get("host"), str(ex)) last_exc = ex @@ -51,7 +53,7 @@ async def conninfo_attempts_async(params: ConnDict) -> list[ConnDict]: return attempts -async def _resolve_hostnames_async(params: ConnDict) -> list[ConnDict]: +async def _resolve_hostnames(params: ConnDict) -> list[ConnDict]: """ Perform async DNS lookup of the hosts and return a list of connection attempts. @@ -85,8 +87,14 @@ async def _resolve_hostnames_async(params: ConnDict) -> list[ConnDict]: port_def = get_param_def("port") port = port_def and port_def.compiled or "5432" - loop = asyncio.get_running_loop() - ans = await loop.getaddrinfo( - host, port, proto=socket.IPPROTO_TCP, type=socket.SOCK_STREAM - ) + if True: # ASYNC: + loop = asyncio.get_running_loop() + ans = await loop.getaddrinfo( + host, port, proto=socket.IPPROTO_TCP, type=socket.SOCK_STREAM + ) + else: + ans = socket.getaddrinfo( + host, port, proto=socket.IPPROTO_TCP, type=socket.SOCK_STREAM + ) + return [{**params, "hostaddr": item[4][0]} for item in ans] diff --git a/tests/test_conninfo_attempts.py b/tests/test_conninfo_attempts.py index f7bd141d1..c2855760a 100644 --- a/tests/test_conninfo_attempts.py +++ b/tests/test_conninfo_attempts.py @@ -1,8 +1,13 @@ +# WARNING: this file is auto-generated by 'async_to_sync.py' +# from the original file 'test_conninfo_attempts_async.py' +# DO NOT CHANGE! Change the original file instead. import pytest import psycopg from psycopg.conninfo import conninfo_to_dict, conninfo_attempts +pytestmark = pytest.mark.anyio + @pytest.mark.parametrize( "conninfo, want, env", @@ -99,21 +104,13 @@ def test_conninfo_attempts_no_resolve(setpgenv, conninfo, want, env, fail_resolv ], None, ), - ( - "host=foo.com,nosuchhost.com", - ["host=foo.com hostaddr=1.1.1.1"], - None, - ), + ("host=foo.com,nosuchhost.com", ["host=foo.com hostaddr=1.1.1.1"], None), ( "host=foo.com, port=5432,5433", ["host=foo.com hostaddr=1.1.1.1 port=5432", "host='' port=5433"], None, ), - ( - "host=nosuchhost.com,foo.com", - ["host=foo.com hostaddr=1.1.1.1"], - None, - ), + ("host=nosuchhost.com,foo.com", ["host=foo.com hostaddr=1.1.1.1"], None), ( "host=foo.com,qux.com", ["host=foo.com hostaddr=1.1.1.1", "host=qux.com hostaddr=2.2.2.2"], diff --git a/tools/async_to_sync.py b/tools/async_to_sync.py index ece9d9bad..d264c78bd 100755 --- a/tools/async_to_sync.py +++ b/tools/async_to_sync.py @@ -29,6 +29,7 @@ import ast_comments as ast PYVER = "3.11" ALL_INPUTS = """ + psycopg/psycopg/_conninfo_attempts_async.py psycopg/psycopg/_copy_async.py psycopg/psycopg/connection_async.py psycopg/psycopg/cursor_async.py @@ -40,6 +41,7 @@ ALL_INPUTS = """ tests/pool/test_pool_null_async.py tests/pool/test_sched_async.py tests/test_connection_async.py + tests/test_conninfo_attempts_async.py tests/test_copy_async.py tests/test_cursor_async.py tests/test_cursor_client_async.py