]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Added result status in PGresult and Cursor repr
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 9 Jan 2021 03:46:09 +0000 (04:46 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 9 Jan 2021 03:46:09 +0000 (04:46 +0100)
psycopg3/psycopg3/cursor.py
psycopg3/psycopg3/pq/pq_ctypes.py
psycopg3_c/psycopg3_c/pq/pgresult.pyx
tests/pq/test_pgresult.py
tests/test_cursor.py
tests/test_cursor_async.py

index 9c029cf8df3f176c7c4cf0b671b989718ae4a7bc..88c8f5fe00f3932f92f7b88df698b0cdfdf012e8 100644 (file)
@@ -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:
index 13828704481ac0052c66ec014a3885439e29a602..2ef92fbf685dd0fe56f16185bfbe989469ace192 100644 (file)
@@ -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:
index 4f6479a9e63e56f34429350c6f7c74027ef2172f..d5e7d3ff8814740dd1555ffb7e05bb46e9a406db 100644 (file)
@@ -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)
index 69b68eda09f51f3afe9665c3a7805404d0838800..874c6bc68fe19f3a7604f547a5a930391bd4dc50 100644 (file)
@@ -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):
index a607833dac611b1386dbe012cf8bc145dfa614d1..e5be0972f031e1bac2c6ab6ee78faee102ba6bb0 100644 (file)
@@ -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)
index d04f2ba555384f4b96f333f65ef7749c082df091..e3d8c8773914b66018aa0f8227319c652c614a5c 100644 (file)
@@ -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)