Skip to content

Commit a2b194a

Browse files
committed
sqlite3: Support 'size' keyword argument in Cursor::fetchmany
1 parent 7509387 commit a2b194a

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

Lib/test/test_sqlite3/test_dbapi.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,8 +1069,6 @@ def test_fetchmany(self):
10691069
res = self.cu.fetchmany(100)
10701070
self.assertEqual(res, [])
10711071

1072-
# TODO: RUSTPYTHON
1073-
@unittest.expectedFailure
10741072
def test_fetchmany_kw_arg(self):
10751073
"""Checks if fetchmany works with keyword arguments"""
10761074
self.cu.execute("select name from test")

stdlib/src/sqlite.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,14 +1684,40 @@ mod _sqlite {
16841684
#[pymethod]
16851685
fn fetchmany(
16861686
zelf: &Py<Self>,
1687-
max_rows: OptionalArg<c_int>,
1687+
mut args: FuncArgs,
16881688
vm: &VirtualMachine,
16891689
) -> PyResult<Vec<PyObjectRef>> {
1690-
let max_rows = max_rows.unwrap_or_else(|| zelf.arraysize.load(Ordering::Relaxed));
1690+
let size_posarg = args.take_positional();
1691+
let size_kwarg = args.take_keyword("size");
1692+
1693+
if !args.args.is_empty() {
1694+
return Err(vm.new_type_error(format!(
1695+
"fetchmany() takes from 0 to 1 positional arguments but {} were given",
1696+
args.args.len() + 1
1697+
)));
1698+
}
1699+
1700+
if let Some((name_str, _)) = args.kwargs.into_iter().next() {
1701+
return Err(vm.new_type_error(format!(
1702+
"fetchmany() got an unexpected keyword argument {name_str}",
1703+
)));
1704+
}
1705+
1706+
let max_rows: c_int = match (size_posarg, size_kwarg) {
1707+
(Some(pos), None) => pos.try_into_value(vm)?,
1708+
(None, Some(kw)) => kw.try_into_value(vm)?,
1709+
(None, None) => zelf.arraysize.load(Ordering::Relaxed),
1710+
(Some(_), Some(_)) => {
1711+
return Err(vm.new_type_error(
1712+
"fetchmany() got multiple values for argument 'size'".to_owned(),
1713+
));
1714+
}
1715+
};
1716+
16911717
let mut list = vec![];
16921718
while let PyIterReturn::Return(row) = Self::next(zelf, vm)? {
16931719
list.push(row);
1694-
if list.len() as c_int >= max_rows {
1720+
if max_rows > 0 && list.len() as c_int >= max_rows {
16951721
break;
16961722
}
16971723
}

0 commit comments

Comments
 (0)