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
sqlite3: add Cursor.row_factory pygetset and fix Connection.cursor() …
…propagation

Add #[pygetset] getter/setter for Cursor.row_factory so that Python-level
attribute access reads/writes the Rust struct field instead of the
instance dict.

Fix Connection.cursor() to only propagate the connection's row_factory
to the cursor when the connection's row_factory is not None, matching
CPython behavior. Previously it unconditionally overwrote the cursor's
row_factory, discarding any factory set by a cursor subclass __init__.
  • Loading branch information
ever0de committed Feb 28, 2026
commit ab4e638d8c130084a9c086045f72c9b24d8a0820
1 change: 0 additions & 1 deletion Lib/test/test_sqlite3/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def test_invalid_factory(self):

class RowFactoryTestsBackwardsCompat(MemoryDatabaseMixin, unittest.TestCase):

@unittest.expectedFailure # TODO: RUSTPYTHON
def test_is_produced_by_factory(self):
cur = self.con.cursor(factory=MyCursor)
cur.execute("select 4+5 as foo")
Expand Down
16 changes: 14 additions & 2 deletions crates/stdlib/src/_sqlite3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,8 +1043,10 @@ mod _sqlite3 {
)));
}

if let Some(cursor_ref) = cursor.downcast_ref::<Cursor>() {
let _ = unsafe { cursor_ref.row_factory.swap(zelf.row_factory.to_owned()) };
if let Some(cursor_ref) = cursor.downcast_ref::<Cursor>()
&& let Some(factory) = zelf.row_factory.to_owned()
{
let _ = unsafe { cursor_ref.row_factory.swap(Some(factory)) };
}

Ok(cursor)
Expand Down Expand Up @@ -1977,6 +1979,16 @@ mod _sqlite3 {
Ok(())
}

#[pygetset]
fn row_factory(&self) -> Option<PyObjectRef> {
self.row_factory.to_owned()
}

#[pygetset(setter)]
fn set_row_factory(&self, val: Option<PyObjectRef>) {
let _ = unsafe { self.row_factory.swap(val) };
}

fn build_row_cast_map(
&self,
st: &SqliteStatementRaw,
Expand Down
Loading