psycopg.git / summary / log / commit / refs
commit 5e2fec0a3ee4fbe4cc855c5f9e6d2e856ec450be
Author: Daniele Varrazzo <daniele.varrazzo@gmail.com>
Date: Sat Jan 06 17:06:54 2024 +0000
refactor: generate conninfo attempts from async counterpart
psycopg/psycopg/_conninfo_attempts.py | 7 ++++++-
psycopg/psycopg/_conninfo_attempts_async.py | 22 +++++++++++++++-------
tests/test_conninfo_attempts.py | 17 +++++++----------
tools/async_to_sync.py | 2 ++
4 files changed, 30 insertions(+), 18 deletions(-)
diff --git a/psycopg/psycopg/_conninfo_attempts.py b/psycopg/psycopg/_conninfo_attempts.py
index 5262ab78..4fc0f792 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 037f213f..6aca4ee3 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 f7bd141d..c2855760 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 ece9d9ba..d264c78b 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
[parent: 2583a6fb3431]