From 4761690b5a858362b42fc459411180924f475dc8 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 14 Apr 2020 17:40:53 +1200 Subject: [PATCH] connection and cursor's close() made async 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 | 15 ++++++++------- tests/test_async_connection.py | 10 +++++----- tests/test_async_cursor.py | 8 ++++---- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/psycopg3/cursor.py b/psycopg3/cursor.py index dcac4cbf1..e1b3f8777 100644 --- a/psycopg3/cursor.py +++ b/psycopg3/cursor.py @@ -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": diff --git a/tests/test_async_connection.py b/tests/test_async_connection.py index b639ba9b9..4f7dcdad8 100644 --- a/tests/test_async_connection.py +++ b/tests/test_async_connection.py @@ -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()) diff --git a/tests/test_async_cursor.py b/tests/test_async_cursor.py index 707fb87ef..1f785bdb8 100644 --- a/tests/test_async_cursor.py +++ b/tests/test_async_cursor.py @@ -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 -- 2.47.2