Current release
---------------
-Psycopg 3.0.3
-^^^^^^^^^^^^^
-
-- Fix disable cursors methods after close() (:ticket:`#125`).
-
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
^^^^^^^^^^^^^
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()
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: