+# 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.
"""
from ._conninfo_utils import ConnDict, get_param, is_ip_address, get_param_def
from ._conninfo_utils import split_attempts
+
logger = logging.getLogger("psycopg")
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.
ans = socket.getaddrinfo(
host, port, proto=socket.IPPROTO_TCP, type=socket.SOCK_STREAM
)
+
return [{**params, "hostaddr": item[4][0]} for item in ans]
from __future__ import annotations
import socket
-import asyncio
import logging
from random import shuffle
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")
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
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.
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]
+# 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",
],
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"],
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
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