]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
chore: drop conninfo.resolve_hostaddr_async 675/head
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 13 Nov 2023 22:56:46 +0000 (23:56 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 13 Nov 2023 22:56:46 +0000 (23:56 +0100)
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.

psycopg/psycopg/_dns.py
psycopg/psycopg/conninfo.py

index 1e146ba216c7bad4dac15f8138af5f3ddbdb7306..ae0a71429a6ec13c03c0ebda378791502e79104b 100644 (file)
@@ -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]:
index 6356a2d0f4524bcf8051925eb854672441d51584..293ba99febfe449dc5e8e6667e496808963be569 100644 (file)
@@ -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.