]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Add name to the ServerCursor repr()
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 10 Feb 2021 15:28:05 +0000 (16:28 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 10 Feb 2021 16:12:11 +0000 (17:12 +0100)
psycopg3/psycopg3/server_cursor.py
tests/test_server_cursor.py
tests/test_server_cursor_async.py

index cffda31341d8aa1da876b49458f3a3c49facf9d5..065967de54ef9b5d464769123f2618ee9b9276e4 100644 (file)
@@ -9,9 +9,9 @@ from types import TracebackType
 from typing import Any, AsyncIterator, Generic, List, Iterator, Optional
 from typing import Sequence, Type, Tuple, TYPE_CHECKING
 
+from . import pq
 from . import sql
 from . import errors as e
-from .pq import Format
 from .cursor import BaseCursor, execute
 from .proto import ConnectionType, Query, Params, PQGen
 
@@ -34,6 +34,17 @@ class ServerCursorHelper(Generic[ConnectionType]):
         self.name = name
         self.described = False
 
+    def _repr(self, cur: BaseCursor[ConnectionType]) -> str:
+        cls = f"{cur.__class__.__module__}.{cur.__class__.__qualname__}"
+        info = pq.misc.connection_summary(cur._conn.pgconn)
+        if cur._closed:
+            status = "closed"
+        elif not cur._pgresult:
+            status = "no result"
+        else:
+            status = pq.ExecStatus(cur._pgresult.status).name
+        return f"<{cls} {self.name!r} [{status}] {info} at 0x{id(cur):x}>"
+
     def _declare_gen(
         self,
         cur: BaseCursor[ConnectionType],
@@ -141,7 +152,7 @@ class ServerCursor(BaseCursor["Connection"]):
         connection: "Connection",
         name: str,
         *,
-        format: Format = Format.TEXT,
+        format: pq.Format = pq.Format.TEXT,
     ):
         super().__init__(connection, format=format)
         self._helper: ServerCursorHelper["Connection"] = ServerCursorHelper(
@@ -157,6 +168,9 @@ class ServerCursor(BaseCursor["Connection"]):
                 ResourceWarning,
             )
 
+    def __repr__(self) -> str:
+        return self._helper._repr(self)
+
     def __enter__(self) -> "ServerCursor":
         return self
 
@@ -257,7 +271,7 @@ class AsyncServerCursor(BaseCursor["AsyncConnection"]):
         connection: "AsyncConnection",
         name: str,
         *,
-        format: Format = Format.TEXT,
+        format: pq.Format = pq.Format.TEXT,
     ):
         super().__init__(connection, format=format)
         self._helper: ServerCursorHelper["AsyncConnection"]
@@ -272,6 +286,9 @@ class AsyncServerCursor(BaseCursor["AsyncConnection"]):
                 ResourceWarning,
             )
 
+    def __repr__(self) -> str:
+        return self._helper._repr(self)
+
     async def __aenter__(self) -> "AsyncServerCursor":
         return self
 
index cbbf23904c1827ab43ac156dc048f46e98286472..fa9bbd12af86772aba2fffcbeb621abfc826b8ca 100644 (file)
@@ -10,6 +10,12 @@ def test_funny_name(conn):
     assert cur.name == "1-2-3"
 
 
+def test_repr(conn):
+    cur = conn.cursor("my-name")
+    assert "ServerCursor" in repr(cur)
+    assert "my-name" in repr(cur)
+
+
 def test_connection(conn):
     cur = conn.cursor("foo")
     assert cur.connection is conn
index 79bb35338c87a03d3bf40f3fd0e89e9e633c9e18..ace61771b2cfddc8fdb4f52efba46c7a3375ef6d 100644 (file)
@@ -12,6 +12,12 @@ async def test_funny_name(aconn):
     assert cur.name == "1-2-3"
 
 
+async def test_repr(aconn):
+    cur = aconn.cursor("my-name")
+    assert "AsyncServerCursor" in repr(cur)
+    assert "my-name" in repr(cur)
+
+
 async def test_connection(aconn):
     cur = aconn.cursor("foo")
     assert cur.connection is aconn