self.described = True
def _close_gen(self, cur: BaseCursor[ConnectionType, Row]) -> PQGen[None]:
+ ts = cur._conn.pgconn.transaction_status
+
# if the connection is not in a sane state, don't even try
- if cur._conn.pgconn.transaction_status not in (
- pq.TransactionStatus.IDLE,
- pq.TransactionStatus.INTRANS,
- ):
+ if ts not in (pq.TransactionStatus.IDLE, pq.TransactionStatus.INTRANS):
+ return
+
+ # If we are IDLE, a WITHOUT HOLD cursor will surely have gone already.
+ if not self.withhold and ts == pq.TransactionStatus.IDLE:
return
# if we didn't declare the cursor ourselves we still have to close it
assert not recwarn, [str(w.message) for w in recwarn.list]
+def test_close_on_error(conn):
+ cur = conn.cursor("foo")
+ cur.execute("select 1")
+ with pytest.raises(e.ProgrammingError):
+ conn.execute("wat")
+ assert conn.info.transaction_status == conn.TransactionStatus.INERROR
+ cur.close()
+
+
def test_context(conn, recwarn, retries):
for retry in retries:
with retry:
@pytest.mark.parametrize("kwargs", [{}, {"withhold": False}])
def test_no_hold(conn, kwargs):
- with pytest.raises(e.InvalidCursorName):
- with conn.cursor("foo", **kwargs) as curs:
- assert curs.withhold is False
- curs.execute("select generate_series(0, 2)")
- assert curs.fetchone() == (0,)
- conn.commit()
+ with conn.cursor("foo", **kwargs) as curs:
+ assert curs.withhold is False
+ curs.execute("select generate_series(0, 2)")
+ assert curs.fetchone() == (0,)
+ conn.commit()
+ with pytest.raises(e.InvalidCursorName):
curs.fetchone()
assert not recwarn, [str(w.message) for w in recwarn.list]
+async def test_close_on_error(aconn):
+ cur = aconn.cursor("foo")
+ await cur.execute("select 1")
+ with pytest.raises(e.ProgrammingError):
+ await aconn.execute("wat")
+ assert aconn.info.transaction_status == aconn.TransactionStatus.INERROR
+ await cur.close()
+
+
async def test_context(aconn, recwarn, retries):
async for retry in retries:
with retry:
@pytest.mark.parametrize("kwargs", [{}, {"withhold": False}])
async def test_no_hold(aconn, kwargs):
- with pytest.raises(e.InvalidCursorName):
- async with aconn.cursor("foo", **kwargs) as curs:
- assert curs.withhold is False
- await curs.execute("select generate_series(0, 2)")
- assert await curs.fetchone() == (0,)
- await aconn.commit()
+ async with aconn.cursor("foo", **kwargs) as curs:
+ assert curs.withhold is False
+ await curs.execute("select generate_series(0, 2)")
+ assert await curs.fetchone() == (0,)
+ await aconn.commit()
+ with pytest.raises(e.InvalidCursorName):
await curs.fetchone()