From c85f68ae230d095a66f95aa01789b5346d2441fc Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Fri, 23 Apr 2021 01:29:53 +0100 Subject: [PATCH] Add ConnectionInfo.error_message, backend_pid --- docs/api/connections.rst | 3 +++ psycopg3/psycopg3/conninfo.py | 16 ++++++++++++++++ tests/test_conninfo.py | 20 ++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/docs/api/connections.rst b/docs/api/connections.rst index 566fbd069..7590e8ff0 100644 --- a/docs/api/connections.rst +++ b/docs/api/connections.rst @@ -273,6 +273,9 @@ Connection support objects server and not yet completed. .. autoattribute:: server_version + .. autoattribute:: backend_pid + .. autoattribute:: error_message + .. automethod:: get_parameters .. autoattribute:: host diff --git a/psycopg3/psycopg3/conninfo.py b/psycopg3/psycopg3/conninfo.py index 94d6149e4..b2171b683 100644 --- a/psycopg3/psycopg3/conninfo.py +++ b/psycopg3/psycopg3/conninfo.py @@ -201,6 +201,14 @@ class ConnectionInfo: """ return self.pgconn.server_version + @property + def backend_pid(self) -> int: + """ + The process ID (PID) of the backend process handling this connection. + See :pq:`PQbackendPID`. + """ + return self.pgconn.backend_pid + @property def protocol_version(self) -> int: """ @@ -208,6 +216,14 @@ class ConnectionInfo: """ return self.pgconn.protocol_version + @property + def error_message(self) -> str: + """ + The error message most recently generated by an operation on the connection. + See :pq:`PQerrorMessage`. + """ + return self._get_pgconn_attr("error_message") + def _get_pgconn_attr(self, name: str) -> str: value: bytes = getattr(self.pgconn, name) return value.decode(self._pyenc) diff --git a/tests/test_conninfo.py b/tests/test_conninfo.py index 64c75ef45..ff5e9544e 100644 --- a/tests/test_conninfo.py +++ b/tests/test_conninfo.py @@ -160,3 +160,23 @@ class TestConnectionInfo: def test_protocol_version(self, conn): assert conn.info.protocol_version >= 3 + + def test_error_message(self, conn): + assert conn.info.error_message == "" + with pytest.raises(psycopg3.ProgrammingError) as ex: + conn.execute("wat") + + assert conn.info.error_message + assert str(ex.value) in conn.info.error_message + assert ex.value.diag.severity in conn.info.error_message + + conn.close() + with pytest.raises(psycopg3.OperationalError): + conn.info.error_message + + def test_backend_pid(self, conn): + assert conn.info.backend_pid + assert conn.info.backend_pid == conn.pgconn.backend_pid + conn.close() + with pytest.raises(psycopg3.OperationalError): + conn.info.backend_pid -- 2.47.2