Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/firebird/driver/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3965,7 +3965,13 @@ def _execute(self, operation: str | Statement,
in_meta.release()
def _clear(self) -> None:
if self._result is not None:
self._result.close()
if self._stmt is not None and self._stmt._istmt is not None:
self._result.close()
else:
# Statement was already freed; the result set is invalidated
# at the Firebird API level, so we must not call close() on it.
# Also prevent __del__ from calling release() on the invalid interface.
self._result._refcnt = 0
self._result = None
self._name = None
self._last_fetch_status = None
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def pytest_configure(config):
client_lib = Path(client_lib)
if not client_lib.is_file():
pytest.exit(f"Client library '{client_lib}' not found!")
driver_config.fb_client_library.value = client_lib
driver_config.fb_client_library.value = str(client_lib)
#
if host := config.getoption('host'):
_vars_['host'] = host
Expand Down
26 changes: 26 additions & 0 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,29 @@ def test_issue_53(db_connection):
numeric_val_exponent = numeric_val.as_tuple()[2]
db_connection.commit()
assert numeric_val_exponent == -2

def test_issue_65_prepare_ctx_mgr(db_connection):
"""Freeing a Statement via context manager must not crash when cursor/connection closes."""
with db_connection.cursor() as cur:
with cur.prepare('select count(*) from country where 1 < ?') as stmt:
row = cur.execute(stmt, (2,)).fetchone()
assert row is not None

def test_issue_65_free_then_cursor_close(db_connection):
"""Explicit stmt.free() followed by cursor.close() must not crash."""
cur = db_connection.cursor()
stmt = cur.prepare('select count(*) from country where 1 < ?')
row = cur.execute(stmt, (2,)).fetchone()
assert row is not None
stmt.free()
cur.close()

def test_issue_65_free_then_conn_close(dsn):
"""stmt.free() followed by connection close must not crash."""
from firebird.driver import connect
with connect(dsn) as conn:
cur = conn.cursor()
stmt = cur.prepare('select count(*) from country where 1 < ?')
row = cur.execute(stmt, (2,)).fetchone()
assert row is not None
stmt.free()