-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix sqlite Connection initialization check #6199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix sqlite Connection initialization check #6199
Conversation
WalkthroughThe SQLite Connection initialization now centralizes opening logic in Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant py_new
participant initialize_db
participant SubclassInit as __init__ (subclass)
User->>py_new: Create Connection
alt Base class
py_new->>initialize_db: initialize_db(args, vm)
initialize_db-->>py_new: Sqlite
py_new->>py_new: store PyMutex(Some(db))
py_new-->>User: Ready
else Subclass
py_new->>py_new: store PyMutex(None)
py_new-->>User: Returned (uninitialized)
User->>SubclassInit: __init__(args, vm)
SubclassInit->>initialize_db: initialize_db(args, vm)
initialize_db-->>SubclassInit: Sqlite
SubclassInit->>SubclassInit: set PyMutex(Some(db))
SubclassInit-->>User: Ready
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
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. Comment |
c015fd8 to
d9bc788
Compare
Add proper __init__ validation for sqlite3.Connection to ensure base class __init__ is called before using connection methods. This fixes the test_connection_constructor_call_check test case. Changes: - Modified Connection.py_new to detect subclassing - For base Connection class, initialization happens immediately in py_new - For subclassed Connection, db is initialized as None - Added __init__ method that performs actual database initialization - Updated _db_lock error message to match CPython: 'Base Connection.__init__ not called.' This ensures CPython compatibility where attempting to use a Connection subclass instance without calling the base __init__ raises ProgrammingError.
d9bc788 to
38e9fc5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
stdlib/src/sqlite.rs (2)
900-910: Clamp timeout to avoid c_int overflow
let timeout = (args.timeout * 1000.0) as c_int;can overflow for large timeouts. Clamp and round before casting.Apply within this block:
- let timeout = (args.timeout * 1000.0) as c_int; - db.busy_timeout(timeout); + // Saturate to c_int::MAX and avoid negatives + let timeout_ms = (args.timeout * 1000.0).clamp(0.0, i32::MAX as f64).round() as c_int; + db.busy_timeout(timeout_ms);
911-922: Subclass init path: good; consider making sqlite.connect() call the factory so init runs
__init__correctly performs one-time DB open for subclassed instances. However,sqlite.connect()still constructs viaConnection::py_new(...), so afactory=SubClasswon’t execute the subclass’__init__(unlike CPython, which callsfactory(...)). Recommend updatingconnect()to invoke the factory type, letting Python dispatch call__new__then__init__.Proposed approach (conceptual; outside this hunk): build args tuple matching
ConnectArgsorder and call the type:// Pseudocode: call the chosen factory so __init__ runs for subclasses // let obj = vm.invoke(args.factory.clone().into_object(), // (args.database, args.timeout, args.detect_types, args.isolation_level, // args.check_same_thread, /* omit passing 'factory' itself */ // args.cached_statements, args.uri))?;Please verify no downstream assumptions rely on the current “py_new-only” construction.
📜 Review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
Lib/test/test_sqlite3/test_regression.pyis excluded by!Lib/**
📒 Files selected for processing (1)
stdlib/src/sqlite.rs(3 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.rs
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.rs: Format Rust code with the default rustfmt style (runcargo fmt)
Run clippy and fix any warnings or lints introduced by your changes
Follow Rust best practices for error handling and memory management
Files:
stdlib/src/sqlite.rs
{vm,stdlib}/**/*.rs
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Use RustPython macros (
pyclass,pymodule,pyfunction, etc.) when implementing Python functionality in Rust
Files:
stdlib/src/sqlite.rs
🧬 Code graph analysis (1)
stdlib/src/sqlite.rs (3)
vm/src/builtins/type.rs (1)
class(147-149)vm/src/object/core.rs (1)
class(671-673)Lib/test/test_sqlite3/test_regression.py (1)
Connection(229-231)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Run snippets and cpython tests (windows-latest)
🔇 Additional comments (2)
stdlib/src/sqlite.rs (2)
854-879: Eager base init + deferred subclass init is correctType check via
cls.is(Connection::class(&vm.ctx).as_object())and gatingdbinitialization achieves the CPython-compatible behavior while preserving defaults (text_factory, thread id). Looks good.
936-940: Error text parity with CPythonUpdated message “Base Connection.init not called.” matches expected test string and CPython behavior. Good.
Run the sqlite3 regression test to confirm:
test_connection_constructor_call_checkpasses with this change.
stdlib/src/sqlite.rs
Outdated
| #[pymethod] | ||
| fn __init__(&self, args: ConnectArgs, vm: &VirtualMachine) -> PyResult<()> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also have an Initializer trait. It’s like the Constructor trait.
Would you apply this trait as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I'd forgotten because it's been a while. Thank you
youknowone
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thank you!
Add proper init validation for sqlite3.Connection to ensure base class init is called before using connection methods. This fixes the test_connection_constructor_call_check test case.
Changes:
Summary by CodeRabbit
Bug Fixes
Improvements