Skip to content

Commit b2d6594

Browse files
committed
Prevent direct instantiation of sqlite3.{Statement,Blob}
1 parent f32a5b1 commit b2d6594

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

Lib/test/test_sqlite3/test_dbapi.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,6 @@ def test_extended_error_code_on_exception(self):
346346
sqlite.SQLITE_CONSTRAINT_CHECK)
347347
self.assertEqual(exc.sqlite_errorname, "SQLITE_CONSTRAINT_CHECK")
348348

349-
# TODO: RUSTPYTHON
350-
@unittest.expectedFailure
351349
def test_disallow_instantiation(self):
352350
cx = sqlite.connect(":memory:")
353351
check_disallow_instantiation(self, type(cx("select 1")))

stdlib/src/sqlite.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,14 +2036,22 @@ mod _sqlite {
20362036
}
20372037

20382038
#[pyattr]
2039-
#[pyclass(name, traverse)]
2039+
#[pyclass(module = "sqlite3", name = "Blob", traverse)]
20402040
#[derive(Debug, PyPayload)]
20412041
struct Blob {
20422042
connection: PyRef<Connection>,
20432043
#[pytraverse(skip)]
20442044
inner: PyMutex<Option<BlobInner>>,
20452045
}
20462046

2047+
impl Constructor for Blob {
2048+
type Args = FuncArgs;
2049+
2050+
fn py_new(_cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult {
2051+
Err(vm.new_type_error("cannot create 'sqlite3.Blob' instances"))
2052+
}
2053+
}
2054+
20472055
#[derive(Debug)]
20482056
struct BlobInner {
20492057
blob: SqliteBlob,
@@ -2056,7 +2064,7 @@ mod _sqlite {
20562064
}
20572065
}
20582066

2059-
#[pyclass(with(AsMapping))]
2067+
#[pyclass(with(AsMapping, Constructor))]
20602068
impl Blob {
20612069
#[pymethod]
20622070
fn close(&self) {
@@ -2356,7 +2364,7 @@ mod _sqlite {
23562364
impl PrepareProtocol {}
23572365

23582366
#[pyattr]
2359-
#[pyclass(name)]
2367+
#[pyclass(module = "sqlite3", name = "Statement")]
23602368
#[derive(PyPayload)]
23612369
struct Statement {
23622370
st: PyMutex<SqliteStatement>,
@@ -2373,7 +2381,15 @@ mod _sqlite {
23732381
}
23742382
}
23752383

2376-
#[pyclass()]
2384+
impl Constructor for Statement {
2385+
type Args = FuncArgs;
2386+
2387+
fn py_new(_cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult {
2388+
Err(vm.new_type_error("cannot create 'sqlite3.Statement' instances"))
2389+
}
2390+
}
2391+
2392+
#[pyclass(with(Constructor))]
23772393
impl Statement {
23782394
fn new(
23792395
connection: &Connection,

0 commit comments

Comments
 (0)