ext/python: Gracefully close connection by closing it at Drop

This commit is contained in:
Diego Reis
2025-03-24 12:21:15 -03:00
parent 4ca5b11bed
commit ab8187f4e6

View File

@@ -193,9 +193,9 @@ impl Cursor {
}
pub fn close(&self) -> PyResult<()> {
Err(PyErr::new::<NotSupportedError, _>(
"close() is not supported in this version",
))
self.conn.close()?;
Ok(())
}
#[pyo3(signature = (sql, parameters=None))]
@@ -244,8 +244,12 @@ impl Connection {
})
}
pub fn close(&self) {
drop(self.conn.clone());
pub fn close(&self) -> PyResult<()> {
self.conn.close().map_err(|e| {
PyErr::new::<OperationalError, _>(format!("Failed to close connection: {:?}", e))
})?;
Ok(())
}
pub fn commit(&self) -> PyResult<()> {
@@ -281,6 +285,14 @@ impl Connection {
}
}
impl Drop for Connection {
fn drop(&mut self) {
self.conn
.close()
.expect("Failed to drop (close) connection");
}
}
#[allow(clippy::arc_with_non_send_sync)]
#[pyfunction]
pub fn connect(path: &str) -> Result<Connection> {