Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
sqlite3: Support 'size' keyword argument in Cursor::fetchmany
  • Loading branch information
ever0de committed Sep 1, 2025
commit a2b194a6f8fe2dbf2378ba9869cf1f61dce3e57e
2 changes: 0 additions & 2 deletions Lib/test/test_sqlite3/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,8 +1069,6 @@ def test_fetchmany(self):
res = self.cu.fetchmany(100)
self.assertEqual(res, [])

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_fetchmany_kw_arg(self):
"""Checks if fetchmany works with keyword arguments"""
self.cu.execute("select name from test")
Expand Down
32 changes: 29 additions & 3 deletions stdlib/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1684,14 +1684,40 @@ mod _sqlite {
#[pymethod]
fn fetchmany(
zelf: &Py<Self>,
max_rows: OptionalArg<c_int>,
mut args: FuncArgs,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can #[derive(FromArgs)] be applicable? Or is there any blocker not allowing to do it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can change it. However, I believe we can't use the FromArgs derive macro because the error messages would be different.

2e16f51 (#6118)

 --- Testing with too many positional arguments ---
-fetchmany() takes from 0 to 1 positional arguments but 2 were given  (cpython)
+expected at most 2 arguments, got 3 (FromArgs)
 
 --- Testing with an unexpected keyword argument ---
-fetchmany() got an unexpected keyword argument 'unexpected_kw' (cpython)
+Unexpected keyword argument unexpected_kw (FromArgs)
 
 --- Testing with both positional and keyword for the same argument ---
-fetchmany() got multiple values for argument 'size' (cpython)
+Unexpected keyword argument size (FromArgs)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you think the current error messages are acceptable, I believe we can apply this version as is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, those error messages are fine - there's discrepancy in so many places, that if we wanted to make the error messages line up we'd just change FromArgs all at once.

vm: &VirtualMachine,
) -> PyResult<Vec<PyObjectRef>> {
let max_rows = max_rows.unwrap_or_else(|| zelf.arraysize.load(Ordering::Relaxed));
let size_posarg = args.take_positional();
let size_kwarg = args.take_keyword("size");

if !args.args.is_empty() {
return Err(vm.new_type_error(format!(
"fetchmany() takes from 0 to 1 positional arguments but {} were given",
args.args.len() + 1
)));
}

if let Some((name_str, _)) = args.kwargs.into_iter().next() {
return Err(vm.new_type_error(format!(
"fetchmany() got an unexpected keyword argument {name_str}",
)));
}

let max_rows: c_int = match (size_posarg, size_kwarg) {
(Some(pos), None) => pos.try_into_value(vm)?,
(None, Some(kw)) => kw.try_into_value(vm)?,
(None, None) => zelf.arraysize.load(Ordering::Relaxed),
(Some(_), Some(_)) => {
return Err(vm.new_type_error(
"fetchmany() got multiple values for argument 'size'".to_owned(),
));
}
};

let mut list = vec![];
while let PyIterReturn::Return(row) = Self::next(zelf, vm)? {
list.push(row);
if list.len() as c_int >= max_rows {
if max_rows > 0 && list.len() as c_int >= max_rows {
break;
}
}
Expand Down
Loading