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_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,6 @@ def test_str_subclass(self):
class MyStr(str): pass
self.con.execute("select ?", (MyStr("abc"),))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_connection_constructor_call_check(self):
"""
Verifies that connection methods check whether base class __init__ was
Expand Down
64 changes: 47 additions & 17 deletions stdlib/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ mod _sqlite {
},
sliceable::{SaturatedSliceIter, SliceableSequenceOp},
types::{
AsMapping, AsNumber, AsSequence, Callable, Comparable, Constructor, Hashable, IterNext,
Iterable, PyComparisonOp, SelfIter, Unconstructible,
AsMapping, AsNumber, AsSequence, Callable, Comparable, Constructor, Hashable,
Initializer, IterNext, Iterable, PyComparisonOp, SelfIter, Unconstructible,
},
utils::ToCString,
};
Expand Down Expand Up @@ -851,7 +851,31 @@ mod _sqlite {
type Args = ConnectArgs;

fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
Ok(Self::new(args, vm)?.into_ref_with_type(vm, cls)?.into())
let text_factory = PyStr::class(&vm.ctx).to_owned().into_object();

// For non-subclassed Connection, initialize in __new__
// For subclassed Connection, leave db as None and require __init__ to be called
let is_base_class = cls.is(Connection::class(&vm.ctx).as_object());

let db = if is_base_class {
// Initialize immediately for base class
Some(Connection::initialize_db(&args, vm)?)
} else {
// For subclasses, require __init__ to be called
None
};

let conn = Self {
db: PyMutex::new(db),
detect_types: args.detect_types,
isolation_level: PyAtomicRef::from(args.isolation_level),
check_same_thread: args.check_same_thread,
thread_ident: std::thread::current().id(),
row_factory: PyAtomicRef::from(None),
text_factory: PyAtomicRef::from(text_factory),
};

Ok(conn.into_ref_with_type(vm, cls)?.into())
}
}

Expand All @@ -871,27 +895,33 @@ mod _sqlite {
}
}

#[pyclass(with(Constructor, Callable), flags(BASETYPE))]
impl Initializer for Connection {
type Args = ConnectArgs;

fn init(zelf: PyRef<Self>, args: Self::Args, vm: &VirtualMachine) -> PyResult<()> {
let mut guard = zelf.db.lock();
if guard.is_some() {
// Already initialized
return Ok(());
}

let db = Self::initialize_db(&args, vm)?;
*guard = Some(db);
Ok(())
}
}

#[pyclass(with(Constructor, Callable, Initializer), flags(BASETYPE))]
impl Connection {
fn new(args: ConnectArgs, vm: &VirtualMachine) -> PyResult<Self> {
fn initialize_db(args: &ConnectArgs, vm: &VirtualMachine) -> PyResult<Sqlite> {
let path = args.database.to_cstring(vm)?;
let db = Sqlite::from(SqliteRaw::open(path.as_ptr(), args.uri, vm)?);
let timeout = (args.timeout * 1000.0) as c_int;
db.busy_timeout(timeout);
if let Some(isolation_level) = &args.isolation_level {
begin_statement_ptr_from_isolation_level(isolation_level, vm)?;
}
let text_factory = PyStr::class(&vm.ctx).to_owned().into_object();

Ok(Self {
db: PyMutex::new(Some(db)),
detect_types: args.detect_types,
isolation_level: PyAtomicRef::from(args.isolation_level),
check_same_thread: args.check_same_thread,
thread_ident: std::thread::current().id(),
row_factory: PyAtomicRef::from(None),
text_factory: PyAtomicRef::from(text_factory),
})
Ok(db)
}

fn db_lock(&self, vm: &VirtualMachine) -> PyResult<PyMappedMutexGuard<'_, Sqlite>> {
Expand All @@ -908,7 +938,7 @@ mod _sqlite {
} else {
Err(new_programming_error(
vm,
"Cannot operate on a closed database.".to_owned(),
"Base Connection.__init__ not called.".to_owned(),
))
}
}
Expand Down
Loading