Skip to content

gh-161837: sqlite3 collation callback (withdrawn) - #161845

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

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

Conversation

@imarturm

@imarturm imarturm commented Nov 12, 2026 •

Copy link
Copy Markdown

…etch

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.
A collation exception also stays set when the step does not fail. Then
pysqlite_cursor_iternext() and executescript() return a result with an
exception set. Now the collation exception propagates on these paths.
executescript() stops after the statement that caused the exception.
It also reports the pending exception when a step fails.

Report the pending exception with PyErr_FormatUnraisable() instead of
PyErr_Print(). The documentation of enable_callback_tracebacks() says
this, and the other callbacks do this.

Move the tests into the new class CollationErrorTests and use
with_tracebacks().
The failed collation returns a wrong result to SQLite. Thus, the
collation exception is the cause of any error later in the step. After
each step, check PyErr_Occurred() first and propagate the exception.
This also prevents execute() and executemany() from calling converters,
adapters and parameter iterators while the exception is set.

Other callbacks can run in the same step after the collation raised.
Do not call Python code from them while the exception is set.

Remove the code that printed or cleared the exception.
The authorizer can run during a step, because table-valued pragma
functions prepare statements. inverse() can run after the collation
raised, for example when SQLite adds a row to an index that uses the
collation. Do not call Python code from these callbacks while an
exception is set.

final_callback() calls finalize() with the earlier exception set aside.
If finalize() raised, the earlier exception was lost. Now the module
sets it again, so that it propagates.

Make the comments generic, because a callback other than a collation
can also leave an exception set.

Make the tests check the order of the calls, and add tests for the
authorizer, inverse() and finalize().
With STAT4, the query planner can call a collation when SQLite
prepares a statement. Check PyErr_Occurred() after
sqlite3_prepare_v2() and propagate the exception.

Add a control to the authorizer test, so that it shows that SQLite
calls the authorizer during the step.
@imarturm imarturm changed the title gh-161837: Fix SystemError in sqlite3 when a collation fails during fetch gh-161837: Fix crashes in sqlite3 when a collation callback raises Nov 12, 2026
@aisk aisk added the AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor label Nov 17, 2026
@bedevere-app

bedevere-app Bot commented Nov 17, 2026

Copy link
Copy Markdown

Thank you for your interest in contributing to Python. This is a community-driven project, and we want it to be a welcoming place to learn and collaborate.

Contributors are responsible for reviewing and understanding what they submit, checking its claims, and being able to explain changes in their own words. This helps reviewers focus on the contribution and leaves more time to support other contributors.

This contribution appears to have been authored with AI assistance. Please explain your contribution in your own words and describe how you checked its correctness.

Please read our guidelines for using AI tools.

@picnixz picnixz added AI-guidelines-not-met The contribution does not meet our guidelines for using AI tools and is closed automatically. and removed AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor labels Nov 29, 2026
@bedevere-app

bedevere-app Bot commented Nov 29, 2026

Copy link
Copy Markdown

Thank you for your interest in contributing to Python. This is a community-driven project, and we want it to be a welcoming place to learn and collaborate.

Contributors are responsible for reviewing and understanding what they submit, checking its claims, and being able to explain changes in their own words. This helps reviewers focus on the contribution and leaves more time to support other contributors.

We believe this contribution does not meet our guidelines for using AI tools, so we're closing it. If you think we've misunderstood your contribution, please let us know. You're welcome to explain your approach or ask questions about the guidelines.

Please read our guidelines for using AI tools.

@bedevere-app bedevere-app Bot closed this Nov 29, 2026
@picnixz

picnixz commented Nov 29, 2026

Copy link
Copy Markdown
Member

This removes PyErr_Print calls. We would appreciate if LLMs were only used for learning the code, and mechanical tasks, not for touching areas that are not properly understood.

@imarturm imarturm changed the title gh-161837: Fix crashes in sqlite3 when a collation callback raises gh-161837: sqlite3 collation callback (withdrawn) Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-guidelines-not-met The contribution does not meet our guidelines for using AI tools and is closed automatically. awaiting review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants