FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add reduce and setstate for zip_longest by dannasman · Pull Request #4235 · RustPython/RustPython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rs  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 0 additions & 2 deletions Lib/test/test_itertools.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
28 changes: 24 additions & 4 deletions vm/src/stdlib/itertools.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -1605,11 +1605,31 @@ mod decl {
#[derive(Debug, PyPayload)]
struct PyItertoolsZipLongest {
iterators: Vec<PyIter>,
fillvalue: PyObjectRef,
fillvalue: PyRwLock<PyObjectRef>,
}

#[pyclass(with(IterNext, Constructor))]
impl PyItertoolsZipLongest {}
impl PyItertoolsZipLongest {
#[pymethod(magic)]
fn reduce(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyTupleRef> {
let args: Vec<PyObjectRef> = zelf
.iterators
.iter()
.map(|i| i.clone().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<Self>, state: PyObjectRef, _vm: &VirtualMachine) -> PyResult<()> {
*zelf.fillvalue.write() = state;
Ok(())
}
}
impl IterNextIterable for PyItertoolsZipLongest {}
impl IterNext for PyItertoolsZipLongest {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
Expand All @@ -1627,7 +1647,7 @@ mod decl {
if numactive == 0 {
return Ok(PyIterReturn::StopIteration(v));
}
zelf.fillvalue.clone()
zelf.fillvalue.read().clone()
}
};
result.push(next_obj);
Expand Down

Back | FazBrowse Home | New Git URL