From: Daniele Varrazzo Date: Sat, 9 Jan 2021 03:46:09 +0000 (+0100) Subject: Added result status in PGresult and Cursor repr X-Git-Tag: 3.0.dev0~192 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1b70b324fe80f5895ddfd60ed85189a8036ec568;p=thirdparty%2Fpsycopg.git Added result status in PGresult and Cursor repr --- diff --git a/psycopg3/psycopg3/cursor.py b/psycopg3/psycopg3/cursor.py index 9c029cf8d..88c8f5fe0 100644 --- a/psycopg3/psycopg3/cursor.py +++ b/psycopg3/psycopg3/cursor.py @@ -82,8 +82,13 @@ class BaseCursor(Generic[ConnectionType]): def __repr__(self) -> str: cls = f"{self.__class__.__module__}.{self.__class__.__qualname__}" info = pq.misc.connection_summary(self._conn.pgconn) - status = " (closed)" if self._closed else "" - return f"<{cls}{status} {info} at 0x{id(self):x}>" + if self._closed: + status = "closed" + elif not self._pgresult: + status = "no result" + else: + status = pq.ExecStatus(self._pgresult.status).name + return f"<{cls} [{status}] {info} at 0x{id(self):x}>" @property def connection(self) -> ConnectionType: diff --git a/psycopg3/psycopg3/pq/pq_ctypes.py b/psycopg3/psycopg3/pq/pq_ctypes.py index 138287044..2ef92fbf6 100644 --- a/psycopg3/psycopg3/pq/pq_ctypes.py +++ b/psycopg3/psycopg3/pq/pq_ctypes.py @@ -21,7 +21,7 @@ from typing import cast as t_cast, TYPE_CHECKING from . import _pq_ctypes as impl from .misc import PGnotify, ConninfoOption, PQerror, PGresAttDesc from .misc import error_message, connection_summary -from ._enums import Format +from ._enums import Format, ExecStatus if TYPE_CHECKING: from . import proto @@ -601,6 +601,11 @@ class PGresult: def __del__(self) -> None: self.clear() + def __repr__(self) -> str: + cls = f"{self.__class__.__module__}.{self.__class__.__qualname__}" + status = ExecStatus(self.status) + return f"<{cls} [{status.name}] at 0x{id(self):x}>" + def clear(self) -> None: self.pgresult_ptr, p = None, self.pgresult_ptr if p: diff --git a/psycopg3_c/psycopg3_c/pq/pgresult.pyx b/psycopg3_c/psycopg3_c/pq/pgresult.pyx index 4f6479a9e..d5e7d3ff8 100644 --- a/psycopg3_c/psycopg3_c/pq/pgresult.pyx +++ b/psycopg3_c/psycopg3_c/pq/pgresult.pyx @@ -7,6 +7,7 @@ psycopg3_c.pq.PGresult object implementation. from cpython.mem cimport PyMem_Malloc, PyMem_Free from psycopg3.pq.misc import PGresAttDesc +from psycopg3.pq._enums import ExecStatus cdef class PGresult: @@ -22,6 +23,11 @@ cdef class PGresult: def __dealloc__(self) -> None: self.clear() + def __repr__(self) -> str: + cls = f"{self.__class__.__module__}.{self.__class__.__qualname__}" + status = ExecStatus(self.status) + return f"<{cls} [{status.name}] at 0x{id(self):x}>" + def clear(self) -> None: if self.pgresult_ptr is not NULL: libpq.PQclear(self.pgresult_ptr) diff --git a/tests/pq/test_pgresult.py b/tests/pq/test_pgresult.py index 69b68eda0..874c6bc68 100644 --- a/tests/pq/test_pgresult.py +++ b/tests/pq/test_pgresult.py @@ -16,6 +16,7 @@ from psycopg3 import pq def test_status(pgconn, command, status): res = pgconn.exec_(command) assert res.status == getattr(pq.ExecStatus, status) + assert status in repr(res) def test_clear(pgconn): diff --git a/tests/test_cursor.py b/tests/test_cursor.py index a607833da..e5be0972f 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -373,7 +373,13 @@ class TestColumn: def test_str(conn): cur = conn.cursor() assert "[IDLE]" in str(cur) - assert "(closed)" not in str(cur) + assert "[closed]" not in str(cur) + assert "[no result]" in str(cur) + cur.execute("select 1") + assert "[INTRANS]" in str(cur) + assert "[TUPLES_OK]" in str(cur) + assert "[closed]" not in str(cur) + assert "[no result]" not in str(cur) cur.close() - assert "(closed)" in str(cur) - assert "[IDLE]" in str(cur) + assert "[closed]" in str(cur) + assert "[INTRANS]" in str(cur) diff --git a/tests/test_cursor_async.py b/tests/test_cursor_async.py index d04f2ba55..e3d8c8773 100644 --- a/tests/test_cursor_async.py +++ b/tests/test_cursor_async.py @@ -283,7 +283,13 @@ async def test_iter_stop(aconn): async def test_str(aconn): cur = await aconn.cursor() assert "[IDLE]" in str(cur) - assert "(closed)" not in str(cur) + assert "[closed]" not in str(cur) + assert "[no result]" in str(cur) + await cur.execute("select 1") + assert "[INTRANS]" in str(cur) + assert "[TUPLES_OK]" in str(cur) + assert "[closed]" not in str(cur) + assert "[no result]" not in str(cur) await cur.close() - assert "(closed)" in str(cur) - assert "[IDLE]" in str(cur) + assert "[closed]" in str(cur) + assert "[INTRANS]" in str(cur)