Skip to content

sqlite3: fix closed connection error message to match CPython - #8362

Merged
youknowone merged 1 commit into
RustPython:mainfrom
ever0de:fix/sqlite-closed-connection-error-message
Jul 25, 2026
Merged

sqlite3: fix closed connection error message to match CPython#8362
youknowone merged 1 commit into
RustPython:mainfrom
ever0de:fix/sqlite-closed-connection-error-message

Conversation

@ever0de

@ever0de ever0de commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

When a Connection is explicitly closed via con.close(), subsequent operations (cursor(), commit(), rollback(), create_function(), etc.) should raise ProgrammingError with 'Cannot operate on a closed database.' to match CPython behaviour.

Previously, _db_lock() always returned 'Base Connection.init not called.' when self.db was None, without distinguishing between a connection that was never initialised (subclass before init) and one that was initialised and then explicitly closed.

Assisted-by: GitHub Copilot:claude-sonnet-4-6

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Improved error reporting for operations on closed database connections.
    • Closed connections now display a clear “Cannot operate on a closed database.” message instead of an initialization-related error.

Copilot AI review requested due to automatic review settings July 25, 2026 08:53
@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro Plus

Run ID: e02e6447-6767-4bb6-bfab-32709718ccb1

📥 Commits

Reviewing files that changed from the base of the PR and between 28454cc and e1ca6c9.

⛔ Files ignored due to path filters (1)
  • Lib/test/test_sqlite3/test_dbapi.py is excluded by !Lib/**
📒 Files selected for processing (1)
  • crates/stdlib/src/_sqlite3.rs

📝 Walkthrough

Walkthrough

The SQLite connection lock path now distinguishes an uninitialized connection from a connection that was initialized and subsequently closed, reporting a dedicated ProgrammingError for the latter.

Changes

SQLite error handling

Layer / File(s) Summary
Closed database error distinction
crates/stdlib/src/_sqlite3.rs
Connection::_db_lock now reports “Cannot operate on a closed database.” when an initialized connection has no underlying database handle.

Estimated code review effort: 2 (Simple) | ~5 minutes

Suggested reviewers: shaharnaveh

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: fixing sqlite3 closed-connection error messaging to match CPython.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

When a Connection is explicitly closed via con.close(), subsequent
operations (cursor(), commit(), rollback(), create_function(), etc.)
should raise ProgrammingError with 'Cannot operate on a closed database.'
to match CPython behaviour.

Previously, _db_lock() always returned 'Base Connection.__init__ not
called.' when self.db was None, without distinguishing between a
connection that was never initialised (subclass before __init__) and
one that was initialised and then explicitly closed.

Fix: inspect the initialized atomic flag — if True but db is None,
the connection was closed; if False, it was never initialised.

Assisted-by: GitHub Copilot:claude-sonnet-4-6
@ever0de
ever0de force-pushed the fix/sqlite-closed-connection-error-message branch from a9012f5 to e1ca6c9 Compare July 25, 2026 08:54
@github-actions

Copy link
Copy Markdown
Contributor

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

[x] lib: cpython/Lib/sqlite3
[x] test: cpython/Lib/test/test_sqlite3 (TODO: 71)

dependencies:

  • sqlite3

dependent tests: (2 tests)

  • sqlite3: test_dbm_sqlite3 test_sqlite3

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Aligns RustPython’s _sqlite3.Connection behavior with CPython by distinguishing “never initialized” connections (e.g., subclass where __init__ wasn’t called) from connections that were successfully initialized and later explicitly closed, so post-close operations raise the correct ProgrammingError message.

Changes:

  • Updates Connection::_db_lock() to raise ProgrammingError: Cannot operate on a closed database. when db is None and the connection has previously been initialized.
  • Keeps the existing ProgrammingError: Base Connection.__init__ not called. for truly uninitialized subclass instances.
  • Removes @unittest.expectedFailure markers from ClosedConTests now that the error message matches CPython.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
Lib/test/test_sqlite3/test_dbapi.py Unmarks previously-expected failures for closed-connection message checks, making the CPython-aligned behavior required by tests.
crates/stdlib/src/_sqlite3.rs Adjusts _db_lock() to select the CPython-matching closed-connection error message based on the initialized flag.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ever0de
ever0de marked this pull request as ready for review July 25, 2026 09:15
@youknowone
youknowone merged commit 63542a6 into RustPython:main Jul 25, 2026
27 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Jul 27, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants