From f6e423c4796d2e086217af37462703a10d69f883 Mon Sep 17 00:00:00 2001 From: Dan Nasman Date: Wed, 19 Oct 2022 20:59:05 +0300 Subject: [PATCH 1/2] add reduce and setstate for zip_longest --- Lib/test/test_itertools.py | 2 -- vm/src/stdlib/itertools.rs | 29 +++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index 53cc26355b3..6c638f07eba 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -985,8 +985,6 @@ def test_zip_longest_tuple_reuse(self): ids = list(map(id, list(zip_longest('abc', 'def')))) self.assertEqual(len(dict.fromkeys(ids)), len(ids)) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_zip_longest_pickling(self): for proto in range(pickle.HIGHEST_PROTOCOL + 1): self.pickletest(proto, zip_longest("abc", "def")) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index 8f571a939ac..cc7bac9aaab 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -1593,7 +1593,7 @@ mod decl { let iterators = iterators.into_vec(); PyItertoolsZipLongest { iterators, - fillvalue, + fillvalue: PyRwLock::new(fillvalue), } .into_ref_with_type(vm, cls) .map(Into::into) @@ -1605,11 +1605,32 @@ mod decl { #[derive(Debug, PyPayload)] struct PyItertoolsZipLongest { iterators: Vec, - fillvalue: PyObjectRef, + fillvalue: PyRwLock, } #[pyclass(with(IterNext, Constructor))] - impl PyItertoolsZipLongest {} + impl PyItertoolsZipLongest { + #[pymethod(magic)] + fn reduce(zelf: PyRef, vm: &VirtualMachine) -> PyResult { + let args: Vec = zelf + .iterators + .to_owned() + .into_iter() + .map(|i| i.to_pyobject(vm)) + .collect(); + Ok(vm.new_tuple(( + zelf.class().to_owned(), + vm.new_tuple(args), + zelf.fillvalue.read().to_owned(), + ))) + } + + #[pymethod(magic)] + fn setstate(zelf: PyRef, state: PyObjectRef, _vm: &VirtualMachine) -> PyResult<()> { + *zelf.fillvalue.write() = state.to_owned(); + Ok(()) + } + } impl IterNextIterable for PyItertoolsZipLongest {} impl IterNext for PyItertoolsZipLongest { fn next(zelf: &Py, vm: &VirtualMachine) -> PyResult { @@ -1627,7 +1648,7 @@ mod decl { if numactive == 0 { return Ok(PyIterReturn::StopIteration(v)); } - zelf.fillvalue.clone() + zelf.fillvalue.read().clone() } }; result.push(next_obj); From 0706d8ea732737c683ee9e5b6f868a4da83a2083 Mon Sep 17 00:00:00 2001 From: Dan Nasman Date: Wed, 19 Oct 2022 21:07:41 +0300 Subject: [PATCH 2/2] fix clippy issues --- vm/src/stdlib/itertools.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/vm/src/stdlib/itertools.rs b/vm/src/stdlib/itertools.rs index cc7bac9aaab..a8acdb11687 100644 --- a/vm/src/stdlib/itertools.rs +++ b/vm/src/stdlib/itertools.rs @@ -1614,9 +1614,8 @@ mod decl { fn reduce(zelf: PyRef, vm: &VirtualMachine) -> PyResult { let args: Vec = zelf .iterators - .to_owned() - .into_iter() - .map(|i| i.to_pyobject(vm)) + .iter() + .map(|i| i.clone().to_pyobject(vm)) .collect(); Ok(vm.new_tuple(( zelf.class().to_owned(), @@ -1627,7 +1626,7 @@ mod decl { #[pymethod(magic)] fn setstate(zelf: PyRef, state: PyObjectRef, _vm: &VirtualMachine) -> PyResult<()> { - *zelf.fillvalue.write() = state.to_owned(); + *zelf.fillvalue.write() = state; Ok(()) } }