Prevent direct instantiation of sqlite3.{Statement,Blob}

This commit is contained in:
Jiseok CHOI
2025-08-01 13:39:45 +09:00
parent f32a5b105a
commit b2d6594bd9
2 changed files with 20 additions and 6 deletions

View File

@@ -346,8 +346,6 @@ class ModuleTests(unittest.TestCase):
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")))

View File

@@ -2036,7 +2036,7 @@ mod _sqlite {
}
#[pyattr]
#[pyclass(name, traverse)]
#[pyclass(module = "sqlite3", name = "Blob", traverse)]
#[derive(Debug, PyPayload)]
struct Blob {
connection: PyRef<Connection>,
@@ -2044,6 +2044,14 @@ mod _sqlite {
inner: PyMutex<Option<BlobInner>>,
}
impl Constructor for Blob {
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,
@@ -2056,7 +2064,7 @@ mod _sqlite {
}
}
#[pyclass(with(AsMapping))]
#[pyclass(with(AsMapping, Constructor))]
impl Blob {
#[pymethod]
fn close(&self) {
@@ -2356,7 +2364,7 @@ mod _sqlite {
impl PrepareProtocol {}
#[pyattr]
#[pyclass(name)]
#[pyclass(module = "sqlite3", name = "Statement")]
#[derive(PyPayload)]
struct Statement {
st: PyMutex<SqliteStatement>,
@@ -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,