Skip to content

Commit f861d0d

Browse files
authored
Merge pull request RustPython#3791 from rng-dynamics/feat-itertools-compress-reduce
Add itertools.compress.__reduce__
2 parents 60598b6 + e4267d9 commit f861d0d

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

Lib/test/test_itertools.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,6 @@ def test_combinatorics(self):
500500
self.assertEqual(comb, list(filter(set(perm).__contains__, cwr))) # comb: cwr that is a perm
501501
self.assertEqual(comb, sorted(set(cwr) & set(perm))) # comb: both a cwr and a perm
502502

503-
# TODO: RUSTPYTHON
504-
@unittest.expectedFailure
505503
def test_compress(self):
506504
self.assertEqual(list(compress(data='ABCDEF', selectors=[1,0,1,0,1,1])), list('ACEF'))
507505
self.assertEqual(list(compress('ABCDEF', [1,0,1,0,1,1])), list('ACEF'))

vm/src/stdlib/itertools.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,39 +116,47 @@ mod decl {
116116
#[derive(Debug, PyPayload)]
117117
struct PyItertoolsCompress {
118118
data: PyIter,
119-
selector: PyIter,
119+
selectors: PyIter,
120120
}
121121

122122
#[derive(FromArgs)]
123123
struct CompressNewArgs {
124-
#[pyarg(positional)]
124+
#[pyarg(any)]
125125
data: PyIter,
126-
#[pyarg(positional)]
127-
selector: PyIter,
126+
#[pyarg(any)]
127+
selectors: PyIter,
128128
}
129129

130130
impl Constructor for PyItertoolsCompress {
131131
type Args = CompressNewArgs;
132132

133133
fn py_new(
134134
cls: PyTypeRef,
135-
Self::Args { data, selector }: Self::Args,
135+
Self::Args { data, selectors }: Self::Args,
136136
vm: &VirtualMachine,
137137
) -> PyResult {
138-
PyItertoolsCompress { data, selector }
138+
PyItertoolsCompress { data, selectors }
139139
.into_ref_with_type(vm, cls)
140140
.map(Into::into)
141141
}
142142
}
143143

144144
#[pyimpl(with(IterNext, Constructor))]
145-
impl PyItertoolsCompress {}
145+
impl PyItertoolsCompress {
146+
#[pymethod(magic)]
147+
fn reduce(zelf: PyRef<Self>) -> (PyTypeRef, (PyIter, PyIter)) {
148+
(
149+
zelf.class().clone(),
150+
(zelf.data.clone(), zelf.selectors.clone()),
151+
)
152+
}
153+
}
146154

147155
impl IterNextIterable for PyItertoolsCompress {}
148156
impl IterNext for PyItertoolsCompress {
149157
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
150158
loop {
151-
let sel_obj = match zelf.selector.next(vm)? {
159+
let sel_obj = match zelf.selectors.next(vm)? {
152160
PyIterReturn::Return(obj) => obj,
153161
PyIterReturn::StopIteration(v) => return Ok(PyIterReturn::StopIteration(v)),
154162
};

0 commit comments

Comments
 (0)