From d1531574dc1da55b8ebc0a42cee1640e2b05b424 Mon Sep 17 00:00:00 2001 From: Amin Ghadersohi Date: Sat, 26 Sep 2026 03:07:33 +0000 Subject: [PATCH 1/2] Return -1 from Cursor.affected_rows for statements without row counts Cursor.affected_rows requested the RECORDS statement info before checking the statement type. For DDL statements the server does not answer that request, so reading rowcount after e.g. CREATE TABLE raised InterfaceError("Result code does not match request code") instead of returning -1 as documented (and as PEP 249 requires). Check the statement type first and only request the row counts for SELECT/INSERT/UPDATE/DELETE statements. This matches the behaviour of the 2.x line. --- src/firebird/driver/core.py | 4 +++- tests/test_driver.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/firebird/driver/core.py b/src/firebird/driver/core.py index e5ab0f0..e69040c 100644 --- a/src/firebird/driver/core.py +++ b/src/firebird/driver/core.py @@ -4134,7 +4134,6 @@ def affected_rows(self) -> int: """ if self._stmt is None: return -1 - rows: Dict[ReqInfoCode, int] = self._stmt.info.get_info(StmtInfoCode.RECORDS) code: ReqInfoCode = None if self._stmt.type in (StatementType.SELECT, StatementType.SELECT_FOR_UPD): code = ReqInfoCode.SELECT_COUNT @@ -4145,7 +4144,10 @@ def affected_rows(self) -> int: elif self._stmt.type == StatementType.DELETE: code = ReqInfoCode.DELETE_COUNT else: + # Row counts are not available for other statement types (e.g. DDL), + # and the RECORDS info request is not answered for them. return -1 + rows: Dict[ReqInfoCode, int] = self._stmt.info.get_info(StmtInfoCode.RECORDS) return rows[code] rowcount = affected_rows @property diff --git a/tests/test_driver.py b/tests/test_driver.py index 29d2f1b..b3dcecf 100644 --- a/tests/test_driver.py +++ b/tests/test_driver.py @@ -1054,6 +1054,12 @@ def test_affected_rows_with_multiple_execute_statements_and_different_command(se rcount = 1 self.assertEqual(cur.affected_rows, rcount) self.assertEqual(cur.rowcount, rcount) + def test_affected_rows_ddl(self): + with self.con.cursor() as cur: + cur.execute("create table affected_rows_ddl (c1 integer)") + self.assertEqual(cur.affected_rows, -1) + self.assertEqual(cur.rowcount, -1) + self.con.rollback() def test_name(self): def assign_name(): cur.set_cursor_name('testx') From 375055e2a093fd7c5d21b0f772890c71cac90a22 Mon Sep 17 00:00:00 2001 From: Amin Ghadersohi Date: Sat, 26 Sep 2026 04:29:38 +0000 Subject: [PATCH 2/2] Guard iAttachment.detach() against an already-released interface (1.x) Backport of the #69 fix (#72) to release-1.x. During cyclic garbage collection the iAttachment's own iReferenceCounted.__del__ may run release() before Connection.__del__ calls detach(); the v5 detach() override then dereferenced freed memory and segfaulted. Restore the if self._refcnt guard that iAttachment_v4.detach() already has. Fixes #77. --- src/firebird/driver/interfaces.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/firebird/driver/interfaces.py b/src/firebird/driver/interfaces.py index a4816ef..0bf6786 100644 --- a/src/firebird/driver/interfaces.py +++ b/src/firebird/driver/interfaces.py @@ -1233,9 +1233,15 @@ class iAttachment(iAttachment_v4): VERSION = 5 def detach(self) -> None: "Replaces `isc_detach_database()`. On success releases interface." - self.vtable.detach(self, self.status) - self._check() - self._refcnt -= 1 + # Guard against detaching an already-released interface. During cyclic + # garbage collection the iAttachment's own iReferenceCounted.__del__ + # may run release() before Connection.__del__ calls detach(); without + # this guard the native call dereferences freed memory and segfaults. + # iAttachment_v4.detach already guards the same way. + if self._refcnt: + self.vtable.detach(self, self.status) + self._check() + self._refcnt -= 1 def drop_database(self) -> None: "Replaces `isc_drop_database()`. On success releases interface." self.vtable.dropDatabase(self, self.status)