From: Daniele Varrazzo Date: Mon, 13 Nov 2023 22:56:46 +0000 (+0100) Subject: chore: drop conninfo.resolve_hostaddr_async X-Git-Tag: 3.2.0~133^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F675%2Fhead;p=thirdparty%2Fpsycopg.git chore: drop conninfo.resolve_hostaddr_async The function is no more used internally and only exposed from the _dns module with a warning. Its implementation is now an application of a few internal functions exposed internally by the conninfo module. Move the whole implementation into _dns. --- diff --git a/psycopg/psycopg/_dns.py b/psycopg/psycopg/_dns.py index 1e146ba21..ae0a71429 100644 --- a/psycopg/psycopg/_dns.py +++ b/psycopg/psycopg/_dns.py @@ -23,7 +23,7 @@ except ImportError: ) from . import errors as e -from .conninfo import resolve_hostaddr_async as resolve_hostaddr_async_ +from . import conninfo if TYPE_CHECKING: from dns.rdtypes.IN.SRV import SRV @@ -48,7 +48,36 @@ async def resolve_hostaddr_async(params: Dict[str, Any]) -> Dict[str, Any]: "from psycopg 3.1, resolve_hostaddr_async() is not needed anymore", DeprecationWarning, ) - return await resolve_hostaddr_async_(params) + hosts: list[str] = [] + hostaddrs: list[str] = [] + ports: list[str] = [] + + for attempt in conninfo._split_attempts(conninfo._inject_defaults(params)): + try: + async for a2 in conninfo._split_attempts_and_resolve(attempt): + hosts.append(a2["host"]) + hostaddrs.append(a2["hostaddr"]) + if "port" in params: + ports.append(a2["port"]) + except OSError as ex: + last_exc = ex + + if params.get("host") and not hosts: + # We couldn't resolve anything + raise e.OperationalError(str(last_exc)) + + out = params.copy() + shosts = ",".join(hosts) + if shosts: + out["host"] = shosts + shostaddrs = ",".join(hostaddrs) + if shostaddrs: + out["hostaddr"] = shostaddrs + sports = ",".join(ports) + if ports: + out["port"] = sports + + return out def resolve_srv(params: Dict[str, Any]) -> Dict[str, Any]: diff --git a/psycopg/psycopg/conninfo.py b/psycopg/psycopg/conninfo.py index 6356a2d0f..293ba99fe 100644 --- a/psycopg/psycopg/conninfo.py +++ b/psycopg/psycopg/conninfo.py @@ -282,55 +282,6 @@ class ConnectionInfo: return value.decode(self.encoding) -async def resolve_hostaddr_async(params: ConnDict) -> ConnDict: - """ - Perform async DNS lookup of the hosts and return a new params dict. - - :param params: The input parameters, for instance as returned by - `~psycopg.conninfo.conninfo_to_dict()`. - - If a ``host`` param is present but not ``hostname``, resolve the host - addresses dynamically. - - The function may change the input ``host``, ``hostname``, ``port`` to allow - connecting without further DNS lookups, eventually removing hosts that are - not resolved, keeping the lists of hosts and ports consistent. - - Raise `~psycopg.OperationalError` if connection is not possible (e.g. no - host resolve, inconsistent lists length). - """ - hosts: list[str] = [] - hostaddrs: list[str] = [] - ports: list[str] = [] - - for attempt in _split_attempts(_inject_defaults(params)): - try: - async for a2 in _split_attempts_and_resolve(attempt): - hosts.append(a2["host"]) - hostaddrs.append(a2["hostaddr"]) - if "port" in params: - ports.append(a2["port"]) - except OSError as ex: - last_exc = ex - - if params.get("host") and not hosts: - # We couldn't resolve anything - raise e.OperationalError(str(last_exc)) - - out = params.copy() - shosts = ",".join(hosts) - if shosts: - out["host"] = shosts - shostaddrs = ",".join(hostaddrs) - if shostaddrs: - out["hostaddr"] = shostaddrs - sports = ",".join(ports) - if ports: - out["port"] = sports - - return out - - def conninfo_attempts(params: ConnDict) -> Iterator[ConnDict]: """Split a set of connection params on the single attempts to perforn.