From: Daniele Varrazzo Date: Fri, 30 Jul 2021 19:14:58 +0000 (+0200) Subject: Don't clear the cursor state after closing X-Git-Tag: 3.0.dev2~23 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9e0bc03b4ccd640c817afe2b287589d8ac582614;p=thirdparty%2Fpsycopg.git Don't clear the cursor state after closing It might be still useful to access the cursor state (e.g. rowcount, the status message) after the cursor is closed, especially if the block syntax is used. Memory use is not really a problem: memory is cleared on GC anyway and most functions using a cursor don't rely on the memory freed between close() and the end of the function. --- diff --git a/psycopg/psycopg/cursor.py b/psycopg/psycopg/cursor.py index c9c1fae25..860521368 100644 --- a/psycopg/psycopg/cursor.py +++ b/psycopg/psycopg/cursor.py @@ -462,11 +462,6 @@ class BaseCursor(Generic[ConnectionType, Row]): def _close(self) -> None: self._closed = True - # however keep the query available, which can be useful for debugging - # in case of errors - pgq = self._pgq - self._reset() - self._pgq = pgq AnyCursor = BaseCursor[Any, Row] diff --git a/tests/test_cursor.py b/tests/test_cursor.py index c008a530c..b88080292 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -50,7 +50,7 @@ def test_status(conn): cur.execute("select 1") assert cur.status == cur.ExecStatus.TUPLES_OK cur.close() - assert cur.status is None + assert cur.status == cur.ExecStatus.TUPLES_OK def test_execute_many_results(conn): @@ -241,7 +241,7 @@ def test_rowcount(conn): assert cur.rowcount == 42 cur.close() - assert cur.rowcount == -1 + assert cur.rowcount == 42 def test_rownumber(conn): diff --git a/tests/test_cursor_async.py b/tests/test_cursor_async.py index f4f35c38a..1e0ec046d 100644 --- a/tests/test_cursor_async.py +++ b/tests/test_cursor_async.py @@ -50,7 +50,7 @@ async def test_status(aconn): await cur.execute("select 1") assert cur.status == cur.ExecStatus.TUPLES_OK await cur.close() - assert cur.status is None + assert cur.status == cur.ExecStatus.TUPLES_OK async def test_execute_many_results(aconn): @@ -243,7 +243,7 @@ async def test_rowcount(aconn): assert cur.rowcount == 42 await cur.close() - assert cur.rowcount == -1 + assert cur.rowcount == 42 async def test_rownumber(aconn):