self.described = True
def _close_gen(self, cur: BaseCursor[ConnectionType]) -> PQGen[None]:
+ # 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,
+ ):
+ return
+
# if we didn't declare the cursor ourselves we still have to close it
# but we must make sure it exists.
if not self.described:
import pytest
+from psycopg3 import errors as e
from psycopg3.pq import Format
assert not recwarn
+def test_close_no_clobber(conn):
+ with pytest.raises(e.DivisionByZero):
+ with conn.cursor("foo") as cur:
+ cur.execute("select 1 / %s", (0,))
+
+
def test_warn_close(conn, recwarn):
cur = conn.cursor("foo")
cur.execute("select generate_series(1, 10) as bar")
def test_executemany(conn):
cur = conn.cursor("foo")
- with pytest.raises(conn.NotSupportedError):
+ with pytest.raises(e.NotSupportedError):
cur.executemany("select %s", [(1,), (2,)])
def test_scroll(conn):
cur = conn.cursor("tmp")
- with pytest.raises(conn.ProgrammingError):
+ with pytest.raises(e.ProgrammingError):
cur.scroll(0)
cur.execute("select generate_series(0,9)")
curs = conn.cursor("foo")
curs.execute("select generate_series(0, 5)", scrollable=False)
curs.scroll(5)
- with pytest.raises(conn.OperationalError):
+ with pytest.raises(e.OperationalError):
curs.scroll(-1)
import pytest
+from psycopg3 import errors as e
from psycopg3.pq import Format
pytestmark = pytest.mark.asyncio
assert not recwarn
+async def test_close_no_clobber(aconn):
+ with pytest.raises(e.DivisionByZero):
+ async with aconn.cursor("foo") as cur:
+ await cur.execute("select 1 / %s", (0,))
+
+
async def test_warn_close(aconn, recwarn):
cur = aconn.cursor("foo")
await cur.execute("select generate_series(1, 10) as bar")
async def test_executemany(aconn):
cur = aconn.cursor("foo")
- with pytest.raises(aconn.NotSupportedError):
+ with pytest.raises(e.NotSupportedError):
await cur.executemany("select %s", [(1,), (2,)])
async def test_scroll(aconn):
cur = aconn.cursor("tmp")
- with pytest.raises(aconn.ProgrammingError):
+ with pytest.raises(e.ProgrammingError):
await cur.scroll(0)
await cur.execute("select generate_series(0,9)")
curs = aconn.cursor("foo")
await curs.execute("select generate_series(0, 5)", scrollable=False)
await curs.scroll(5)
- with pytest.raises(aconn.OperationalError):
+ with pytest.raises(e.OperationalError):
await curs.scroll(-1)