Skip to content

gh-161837: Fix SystemError in sqlite3 when a collation fails during fetch - #161845

Open
imarturm wants to merge 2 commits into
python:mainfrom
imarturm:gh-161837-sqlite-collation-exc
Open

imarturm wants to merge 2 commits into
python:mainfrom
imarturm:gh-161837-sqlite-collation-exc

Conversation

@imarturm

Copy link
Copy Markdown

A collation callback cannot report an error to SQLite, so the Python exception stays set. A later error in the same step can make sqlite3_step() fail. On the execute() path, the module prints or clears the pending exception and then raises OperationalError. On the fetch path (fetchone(), fetchmany(), fetchall() and iteration), pysqlite_cursor_iternext() calls set_error_from_db() with the exception still set. This causes a SystemError in release builds. In debug builds, an assertion fails in type_call().

This change moves the existing code from _pysqlite_query_execute() into a new helper, clear_callback_error(), and calls it from both functions. The behavior of execute() does not change. Now the fetch path has the same behavior as execute():

  • With callback tracebacks disabled (the default), the module clears the collation exception and raises OperationalError.
  • With sqlite3.enable_callback_tracebacks(True), the module also prints the collation traceback.

The new test does fetchone() and iteration, with tracebacks enabled and disabled. Without the fix, the test causes an assertion failure on a debug build. With the fix, test_sqlite3 passes, and -R 3:3 shows no reference leaks.

…ring fetch

A collation callback cannot report an error to SQLite, so the Python
exception stays set. A later error in the same step can make
sqlite3_step() fail. Then pysqlite_cursor_iternext() calls
set_error_from_db() with the exception still set. This causes a
SystemError. In debug builds, an assertion fails.

_pysqlite_query_execute() already prints or clears the pending
exception before it raises the DB-API exception. Move that code into
the new helper clear_callback_error() and use it in both functions.
@python-cla-bot

python-cla-bot Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

The test compares the printed traceback with plain text. When colors
are enabled, the traceback contains escape codes and the comparison
fails. Use force_not_colorized to make the output plain.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant