]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
connection and cursor's close() made async
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 14 Apr 2020 05:40:53 +0000 (17:40 +1200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 14 Apr 2020 08:26:13 +0000 (20:26 +1200)
Dropped close() on __del__ for cursor: it is of no utility. The
important thing is that PGresult frees its memory and that happens at
PGresult's own __del__.

psycopg3/cursor.py
tests/test_async_connection.py
tests/test_async_cursor.py

index dcac4cbf190b51db348d828e4ee89d8da707c3ad..e1b3f877713c87f131acfe2268e9f153d4fc1656 100644 (file)
@@ -77,13 +77,6 @@ class BaseCursor:
         self._pos = 0
         self._iresult = 0
 
-    def __del__(self) -> None:
-        self.close()
-
-    def close(self) -> None:
-        self._closed = True
-        self._reset()
-
     @property
     def closed(self) -> bool:
         return self._closed
@@ -263,6 +256,10 @@ class Cursor(BaseCursor):
     def __init__(self, connection: "Connection", binary: bool = False):
         super().__init__(connection, binary)
 
+    def close(self) -> None:
+        self._closed = True
+        self._reset()
+
     def execute(self, query: Query, vars: Optional[Params] = None) -> "Cursor":
         with self.connection.lock:
             self._start_query()
@@ -332,6 +329,10 @@ class AsyncCursor(BaseCursor):
     def __init__(self, connection: "AsyncConnection", binary: bool = False):
         super().__init__(connection, binary)
 
+    async def close(self) -> None:
+        self._closed = True
+        self._reset()
+
     async def execute(
         self, query: Query, vars: Optional[Params] = None
     ) -> "AsyncCursor":
index b639ba9b98ed54b267dc7166179e0f400946c911..4f7dcdad8527a2fa143ca0053a50bce4ca299075 100644 (file)
@@ -14,12 +14,12 @@ def test_connect_bad(loop):
         loop.run_until_complete(AsyncConnection.connect("dbname=nosuchdb"))
 
 
-def test_close(aconn):
+def test_close(aconn, loop):
     assert not aconn.closed
-    aconn.close()
+    loop.run_until_complete(aconn.close())
     assert aconn.closed
     assert aconn.status == aconn.ConnStatus.BAD
-    aconn.close()
+    loop.run_until_complete(aconn.close())
     assert aconn.closed
     assert aconn.status == aconn.ConnStatus.BAD
 
@@ -35,7 +35,7 @@ def test_commit(loop, aconn):
     res = aconn.pgconn.exec_(b"select id from foo where id = 1")
     assert res.get_value(0, 0) == b"1"
 
-    aconn.close()
+    loop.run_until_complete(aconn.close())
     with pytest.raises(psycopg3.OperationalError):
         loop.run_until_complete(aconn.commit())
 
@@ -51,7 +51,7 @@ def test_rollback(loop, aconn):
     res = aconn.pgconn.exec_(b"select id from foo where id = 1")
     assert res.get_value(0, 0) is None
 
-    aconn.close()
+    loop.run_until_complete(aconn.close())
     with pytest.raises(psycopg3.OperationalError):
         loop.run_until_complete(aconn.rollback())
 
index 707fb87efdf7aba8685d7603ca8b3c13ed636f7c..1f785bdb89a4febcdc24789b27b17ac92b2cdc6a 100644 (file)
@@ -5,13 +5,13 @@ import psycopg3
 def test_close(aconn, loop):
     cur = aconn.cursor()
     assert not cur.closed
-    cur.close()
+    loop.run_until_complete(cur.close())
     assert cur.closed
 
     with pytest.raises(psycopg3.OperationalError):
         loop.run_until_complete(cur.execute("select 'foo'"))
 
-    cur.close()
+    loop.run_until_complete(cur.close())
     assert cur.closed
 
 
@@ -22,7 +22,7 @@ def test_status(aconn, loop):
     assert cur.status == cur.ExecStatus.COMMAND_OK
     loop.run_until_complete(cur.execute("select 1"))
     assert cur.status == cur.ExecStatus.TUPLES_OK
-    cur.close()
+    loop.run_until_complete(cur.close())
     assert cur.status is None
 
 
@@ -38,7 +38,7 @@ def test_execute_many_results(aconn, loop):
     assert cur.pgresult.get_value(0, 0) == b"bar"
     assert cur.nextset() is None
 
-    cur.close()
+    loop.run_until_complete(cur.close())
     assert cur.nextset() is None