Also add C implementation of PGconn.hostaddr.
"""The host name of the database."""
return self._get_pgconn_attr("host")
+ @property
+ def hostaddr(self) -> str:
+ """The server ip address of the connection."""
+ return self._get_pgconn_attr("hostaddr")
+
@property
def port(self) -> int:
"""The port of the database connection."""
from cpython.bytes cimport PyBytes_AsString, PyBytes_AsStringAndSize
from cpython.memoryview cimport PyMemoryView_FromObject
+import ctypes
import logging
from psycopg3.pq import Format as PqFormat
@property
def hostaddr(self) -> bytes:
- return b'TODO'
+ # Available only from PG 12. Use the dynamic ctypes implementation
+ from psycopg3.pq import _pq_ctypes
+
+ _ensure_pgconn(self)
+ ctypes_ptr = ctypes.cast(
+ <long><void *>self.pgconn_ptr, _pq_ctypes.PGconn_ptr)
+ return _pq_ctypes.PQhostaddr(ctypes_ptr)
@property
def port(self) -> bytes:
# not in info
assert isinstance(pgconn.hostaddr, bytes), pgconn.hostaddr
pgconn.finish()
- if psycopg3.pq.__impl__ == "python":
- with pytest.raises(psycopg3.OperationalError):
- pgconn.hostaddr
-
- else:
- if pgconn.hostaddr == b"TODO":
- pytest.xfail("implement hostaddr in psycopg3_c.pq")
- else:
- assert False, "you did it! not fix the test"
+ with pytest.raises(psycopg3.OperationalError):
+ pgconn.hostaddr
@pytest.mark.xfail
class TestConnectionInfo:
@pytest.mark.parametrize(
- "attr", [("dbname", "db"), "host", "user", "password", "options"]
+ "attr",
+ [("dbname", "db"), "host", "hostaddr", "user", "password", "options"],
)
def test_attrs(self, conn, attr):
if isinstance(attr, tuple):