From: Daniele Varrazzo Date: Mon, 18 Dec 2023 23:58:35 +0000 (+0100) Subject: fix: fix empty ports handling in async multiple connection attempts X-Git-Tag: 3.1.16~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6c473d41d27a8647dfd25bd5040cf9c8c48f5a6e;p=thirdparty%2Fpsycopg.git fix: fix empty ports handling in async multiple connection attempts Close #703. --- diff --git a/docs/news.rst b/docs/news.rst index ead1cb982..425c132f0 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -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 --------------- diff --git a/psycopg/psycopg/conninfo.py b/psycopg/psycopg/conninfo.py index ef07e5eca..c57a6d9c0 100644 --- a/psycopg/psycopg/conninfo.py +++ b/psycopg/psycopg/conninfo.py @@ -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 diff --git a/tests/test_conninfo.py b/tests/test_conninfo.py index 7db911236..d9888d3bd 100644 --- a/tests/test_conninfo.py +++ b/tests/test_conninfo.py @@ -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"],