]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix: fix empty ports handling in async multiple connection attempts
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 18 Dec 2023 23:58:35 +0000 (00:58 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 19 Dec 2023 00:58:29 +0000 (01:58 +0100)
Close #703.

docs/news.rst
psycopg/psycopg/conninfo.py
tests/test_conninfo.py

index ead1cb9829ae1da53be77dc11f627dbb2557403c..425c132f056d3a728e5e4588472b7329b53ae4b8 100644 (file)
@@ -7,6 +7,16 @@
 ``psycopg`` release notes
 =========================
 
+Future releases
+---------------
+
+Psycopg 3.1.16 (unreleased)
+^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+- Fix empty ports handling in async multiple connection attempts
+  (:ticket:`#703`).
+
+
 Current release
 ---------------
 
index ef07e5ecac1899e1a7bf5839ebdc8f812984477d..c57a6d9c095ff4d70fcd0fb528b36280788d306e 100644 (file)
@@ -422,8 +422,11 @@ async def _resolve_hostnames(params: ConnDict) -> list[ConnDict]:
 
     loop = asyncio.get_running_loop()
 
-    port = _get_param(params, "port", compiled_default=True)
-    assert port and "," not in port  # assume a libpq default and no multi
+    port = _get_param(params, "port")
+    if not port:
+        port_def = _get_param_def("port")
+        port = port_def and port_def.compiled or "5432"
+
     ans = await loop.getaddrinfo(
         host, int(port), proto=socket.IPPROTO_TCP, type=socket.SOCK_STREAM
     )
@@ -461,9 +464,7 @@ def timeout_from_conninfo(params: ConnDict) -> int:
     return timeout
 
 
-def _get_param(
-    params: ConnDict, name: str, compiled_default: bool = False
-) -> str | None:
+def _get_param(params: ConnDict, name: str) -> str | None:
     """
     Return a value from a connection string.
 
@@ -482,7 +483,7 @@ def _get_param(
     if env is not None:
         return env
 
-    return paramdef.compiled if compiled_default else None
+    return None
 
 
 @dataclass
index 7db911236d1a1e6aca93f3cb34113069217be769..d9888d3bdd44fc11139aa1d0cac224b44efb71c9 100644 (file)
@@ -338,6 +338,11 @@ class TestConnectionInfo:
             ["host=1.1.1.1 port=5432", "host=2.2.2.2 port=5432"],
             None,
         ),
+        (
+            "host=1.1.1.1,1.1.1.1 port=5432,",
+            ["host=1.1.1.1 port=5432", "host=1.1.1.1 port=''"],
+            None,
+        ),
         (
             "host=foo.com port=5432",
             ["host=foo.com port=5432"],
@@ -359,6 +364,11 @@ def test_conninfo_attempts(setpgenv, conninfo, want, env):
     [
         ("", [""], None),
         ("host='' user=bar", ["host='' user=bar"], None),
+        (
+            "host=127.0.0.1 user=bar port=''",
+            ["host=127.0.0.1 user=bar port='' hostaddr=127.0.0.1"],
+            None,
+        ),
         (
             "host=127.0.0.1 user=bar",
             ["host=127.0.0.1 user=bar hostaddr=127.0.0.1"],
@@ -380,6 +390,14 @@ def test_conninfo_attempts(setpgenv, conninfo, want, env):
             ],
             None,
         ),
+        (
+            "host=1.1.1.1,2.2.2.2 port=5432,",
+            [
+                "host=1.1.1.1 port=5432 hostaddr=1.1.1.1",
+                "host=2.2.2.2 port='' hostaddr=2.2.2.2",
+            ],
+            None,
+        ),
         (
             "port=5432",
             [
@@ -430,6 +448,14 @@ async def test_conninfo_attempts_async_no_resolve(
             ],
             None,
         ),
+        (
+            "host=foo.com,foo.com port=5432,",
+            [
+                "host=foo.com hostaddr=1.1.1.1 port=5432",
+                "host=foo.com hostaddr=1.1.1.1 port=''",
+            ],
+            None,
+        ),
         (
             "host=foo.com,nosuchhost.com",
             ["host=foo.com hostaddr=1.1.1.1"],