Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Prevent direct instantiation of sqlite3.{Statement,Blob}
  • Loading branch information
ever0de committed Aug 1, 2025
commit b2d6594bd9cc7a91b85cfe107d0e4ce7916bae62
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 @@ -346,8 +346,6 @@ def test_extended_error_code_on_exception(self):
sqlite.SQLITE_CONSTRAINT_CHECK)
self.assertEqual(exc.sqlite_errorname, "SQLITE_CONSTRAINT_CHECK")

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_disallow_instantiation(self):
cx = sqlite.connect(":memory:")
check_disallow_instantiation(self, type(cx("select 1")))
Expand Down
24 changes: 20 additions & 4 deletions stdlib/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2036,14 +2036,22 @@ mod _sqlite {
}

#[pyattr]
#[pyclass(name, traverse)]
#[pyclass(module = "sqlite3", name = "Blob", traverse)]
#[derive(Debug, PyPayload)]
struct Blob {
connection: PyRef<Connection>,
#[pytraverse(skip)]
inner: PyMutex<Option<BlobInner>>,
}

impl Constructor for Blob {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you check if Unconstructible works?

Suggested change
impl Constructor for Blob {
impl Unconstructible for Blob {

Copy link
Copy Markdown
Contributor Author

@ever0de ever0de Aug 1, 2025

Choose a reason for hiding this comment

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

We can proceed with this fix by adding single quotes to the error message. I will apply the patch for now.

https://github.com/ever0de/RustPython/blob/b2d6594bd9cc7a91b85cfe107d0e4ce7916bae62/vm/src/types/slot.rs#L801-L808

- Err(vm.new_type_error(format!("cannot create {} instances", cls.slot_name())))
+ Err(vm.new_type_error(format!("cannot create '{}' instances", cls.slot_name())))

type Args = FuncArgs;

fn py_new(_cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult {
Err(vm.new_type_error("cannot create 'sqlite3.Blob' instances"))
}
}

#[derive(Debug)]
struct BlobInner {
blob: SqliteBlob,
Expand All @@ -2056,7 +2064,7 @@ mod _sqlite {
}
}

#[pyclass(with(AsMapping))]
#[pyclass(with(AsMapping, Constructor))]
impl Blob {
#[pymethod]
fn close(&self) {
Expand Down Expand Up @@ -2356,7 +2364,7 @@ mod _sqlite {
impl PrepareProtocol {}

#[pyattr]
#[pyclass(name)]
#[pyclass(module = "sqlite3", name = "Statement")]
#[derive(PyPayload)]
struct Statement {
st: PyMutex<SqliteStatement>,
Expand All @@ -2373,7 +2381,15 @@ mod _sqlite {
}
}

#[pyclass()]
impl Constructor for Statement {
type Args = FuncArgs;

fn py_new(_cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult {
Err(vm.new_type_error("cannot create 'sqlite3.Statement' instances"))
}
}

#[pyclass(with(Constructor))]
impl Statement {
fn new(
connection: &Connection,
Expand Down
Loading