Skip to content
Merged
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
2 changes: 0 additions & 2 deletions Lib/test/test_sqlite3/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1469,8 +1469,6 @@ def test_blob_closed(self):
with self.assertRaisesRegex(sqlite.ProgrammingError, msg):
blob[0] = b""

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_blob_closed_db_read(self):
with memory_database() as cx:
cx.execute("create table test(b blob)")
Expand Down
11 changes: 11 additions & 0 deletions crates/stdlib/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,10 @@ mod _sqlite {
Ok(())
}

fn is_closed(&self) -> bool {
self.db.lock().is_none()
}

#[pymethod]
fn commit(&self, vm: &VirtualMachine) -> PyResult<()> {
self.db_lock(vm)?.implicit_commit(vm)
Expand Down Expand Up @@ -2169,6 +2173,13 @@ mod _sqlite {
length: OptionalArg<c_int>,
vm: &VirtualMachine,
) -> PyResult<PyRef<PyBytes>> {
if self.connection.is_closed() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please put the link in the PR here? it will be easier to access rather than going to git blame -> PR -> links

Copy link
Contributor Author

@ever0de ever0de Nov 21, 2025

Choose a reason for hiding this comment

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

Could you please clarify the context? I am currently unable to understand why it's required.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not required. Just my personal preference

Copy link
Member

Choose a reason for hiding this comment

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

This isn’t the official stance of our project, but I’d like to share my personal view on the matter.

If the behavior is something that’s common sense and easily predictable, I don’t think we need to attach a reference. In my opinion, this change falls into that category. Before adding this code, it might have been unclear whether such a check was necessary, but once the code is added, it becomes obvious that it is.

Some behaviors are hard to infer from the code alone. In those cases, having a reference linked is helpful. Depending on the situation, when pointing to the exact code is useful, we link specific line numbers; when it’s more important to refer to the conceptual location, we just mention the function name instead.

return Err(new_programming_error(
vm,
"Cannot operate on a closed database".to_owned(),
));
}
Comment on lines +2176 to +2181
Copy link
Contributor

@coderabbitai coderabbitai bot Nov 21, 2025

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

Verify that other blob methods handle closed connections correctly.

The check for closed connections in read() is correct and matches the CPython behavior. However, other blob methods like write, tell, seek, and the subscript operations may also need this check to be consistent.

Run the following script to examine if other blob methods should also include the closed-connection check:


🏁 Script executed:

#!/bin/bash
# Description: Search for all Blob methods to verify consistent closed-connection handling

echo "=== Blob methods in the file ==="
ast-grep --pattern $'impl Blob {
  $$$
  fn $METHOD($$$) $$$
  $$$
}'

echo -e "\n=== Checking which methods call connection.db_lock or check connection state ==="
rg -n -A5 -B2 'impl Blob' crates/stdlib/src/sqlite.rs | rg -A5 'fn (write|tell|seek|__enter__|__exit__|subscript|ass_subscript)'

Length of output: 114


The web search doesn't provide targeted information about the specific codebase. Let me generate new shell scripts to examine the Blob implementation directly:


🏁 Script executed:

#!/bin/bash
# Description: Extract Blob impl block and examine all methods

echo "=== Finding impl Blob block ==="
rg -n 'impl Blob' crates/stdlib/src/sqlite.rs -A 300

echo -e "\n=== Counting methods in Blob impl ==="
rg -n 'fn ' crates/stdlib/src/sqlite.rs | grep -A1 -B1 'Blob'

Length of output: 12999


🏁 Script executed:

#!/bin/bash
# Search for CPython's blob.c file content from GitHub

echo "=== Searching for CPython blob.c on GitHub ==="
curl -s https://raw.githubusercontent.com/python/cpython/main/Modules/_sqlite/blob.c | head -400

Length of output: 9655


Add closed-connection check to other blob methods for consistency with CPython behavior.

CPython's check_blob() function checks connection status on ALL blob operations (read, write, seek, tell, enter, exit). The Rust implementation only added this check to read(). The following methods should include the same check before calling self.inner(vm)?:

  • write() (line 2207)
  • tell() (line 2222)
  • seek() (line 2227)
  • __enter__() (line 2261)
  • __exit__() (line 2267)
  • subscript() (line 2313)
  • ass_subscript() (line 2350)

Without these checks, operations on other methods could fail with unclear errors when the connection is closed, unlike CPython which provides a consistent error message.

🤖 Prompt for AI Agents
In crates/stdlib/src/sqlite.rs around lines 2176-2360 (affecting the methods
at/near lines 2207, 2222, 2227, 2261, 2267, 2313, 2350), add the same
closed-connection guard used at lines 2176-2181 to each listed blob method:
before calling self.inner(vm)? in write(), tell(), seek(), __enter__(),
__exit__(), subscript(), and ass_subscript(), check if
self.connection.is_closed() and if so return Err(new_programming_error(vm,
"Cannot operate on a closed database".to_owned())); this ensures consistent
CPython-like error messages for all blob operations when the connection is
closed.

Copy link
Member

Choose a reason for hiding this comment

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

@ever0de How do you think about this comment?

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@youknowone Can I handle this in a separate issue?


let mut length = length.unwrap_or(-1);
let mut inner = self.inner(vm)?;
let blob_len = inner.blob.bytes();
Expand Down
Loading