From: Suresh Kumar Date: Sat, 30 Oct 2021 15:08:27 +0000 (+0530) Subject: fixed cursor close check + updated news X-Git-Tag: 3.0.2~11^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=432cb06fffc6a0e55e6e28ddde99a0f163c341aa;p=thirdparty%2Fpsycopg.git fixed cursor close check + updated news --- diff --git a/.gitignore b/.gitignore index cf12ee0e5..2554231eb 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ __pycache__/ *.html /psycopg_binary/ .vscode +.venv \ No newline at end of file diff --git a/docs/news.rst b/docs/news.rst index cc467eb8d..767227881 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -9,11 +9,6 @@ Release notes Current release --------------- -Psycopg 3.0.3 -^^^^^^^^^^^^^ - -- Fix disable cursors methods after close() (:ticket:`#125`). - Psycopg 3.0.2 ^^^^^^^^^^^^^ @@ -22,7 +17,7 @@ Psycopg 3.0.2 - Fix type hint for `Connection.notifies()` (:ticket:`#128`). - Fix call to `MultiRange.__setitem__()` with a non-iterable value and a slice, now raising a `TypeError` (:ticket:`#129`). - +- Fix disable cursors methods after close() (:ticket:`#125`). Psycopg 3.0.1 ^^^^^^^^^^^^^ diff --git a/psycopg/psycopg/server_cursor.py b/psycopg/psycopg/server_cursor.py index d050a7186..43ed3c6bf 100644 --- a/psycopg/psycopg/server_cursor.py +++ b/psycopg/psycopg/server_cursor.py @@ -126,6 +126,8 @@ class ServerCursorHelper(Generic[ConnectionType, Row]): def _fetch_gen( self, cur: BaseCursor[ConnectionType, Row], num: Optional[int] ) -> PQGen[List[Row]]: + if cur.closed: + raise e.InterfaceError("the cursor is closed") # If we are stealing the cursor, make sure we know its shape if not self.described: yield from cur._start_query() diff --git a/tests/test_server_cursor.py b/tests/test_server_cursor.py index 6686621d6..a35aad107 100644 --- a/tests/test_server_cursor.py +++ b/tests/test_server_cursor.py @@ -120,6 +120,52 @@ def test_close_idempotent(conn): cur.close() +def test_cursor_close_fetchone(conn): + cur = conn.cursor("foo") + assert not cur.closed + + query = "select * from generate_series(1, 10)" + cur.execute(query) + for _ in range(5): + cur.fetchone() + + cur.close() + assert cur.closed + + with pytest.raises(e.InterfaceError): + cur.fetchone() + + +def test_cursor_close_fetchmany(conn): + cur = conn.cursor("foo") + assert not cur.closed + + query = "select * from generate_series(1, 10)" + cur.execute(query) + assert len(cur.fetchmany(2)) == 2 + + cur.close() + assert cur.closed + + with pytest.raises(e.InterfaceError): + cur.fetchmany(2) + + +def test_cursor_close_fetchall(conn): + cur = conn.cursor("foo") + assert not cur.closed + + query = "select * from generate_series(1, 10)" + cur.execute(query) + assert len(cur.fetchall()) == 10 + + cur.close() + assert cur.closed + + with pytest.raises(e.InterfaceError): + cur.fetchall() + + def test_close_noop(conn, recwarn, retries): for retry in retries: with retry: