Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
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
5 changes: 1 addition & 4 deletions Lib/test/test_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,6 @@ def test_chain_from_iterable(self):
self.assertEqual(take(4, chain.from_iterable(['abc', 'def'])), list('abcd'))
self.assertRaises(TypeError, list, chain.from_iterable([2, 3]))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_chain_reducible(self):
for oper in [copy.deepcopy] + picklecopiers:
it = chain('abc', 'def')
Expand All @@ -195,8 +193,7 @@ def test_chain_reducible(self):
self.assertRaises(TypeError, list, oper(chain(2, 3)))
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
self.pickletest(proto, chain('abc', 'def'), compare=list('abcdef'))
# TODO: RUSTPYTHON
@unittest.expectedFailure

def test_chain_setstate(self):
self.assertRaises(TypeError, chain().__setstate__, ())
self.assertRaises(TypeError, chain().__setstate__, [])
Expand Down
47 changes: 46 additions & 1 deletion vm/src/stdlib/itertools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mod decl {
active: PyRwLock<Option<PyIter>>,
}

#[pyclass(with(IterNext), flags(BASETYPE))]
#[pyclass(with(IterNext), flags(BASETYPE, HAS_DICT))]
impl PyItertoolsChain {
#[pyslot]
fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Expand Down Expand Up @@ -59,6 +59,51 @@ mod decl {
fn class_getitem(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
PyGenericAlias::new(cls, args, vm)
}

#[pymethod(magic)]
fn reduce(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyTupleRef> {
let source = zelf.source.read().clone();
let active = zelf.active.read().clone();
let cls = zelf.class().to_owned();
match source {
Some(source) => match active {
Some(active) => {
Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), (source, active))))
}
None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), (source,)))),
},
None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone()))),
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
match source {
Some(source) => match active {
Some(active) => {
Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), (source, active))))
}
None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone(), (source,)))),
},
None => Ok(vm.new_tuple((cls, vm.ctx.empty_tuple.clone()))),
}
let empty_tuple = vm.ctx.empty_tuple.clone();
let reduced = match source {
Some(source) => match active {
Some(active) => vm.new_tuple((cls, empty_tuple, (source, active))),
None => vm.new_tuple((cls, empty_tuple, (source,))),
},
None => vm.new_tuple((cls, empty_tuple)),
};
Ok(reduced)

}

#[pymethod(magic)]
fn setstate(zelf: PyRef<Self>, state: PyTupleRef, vm: &VirtualMachine) -> PyResult<()> {
let args = state.as_slice();
if args.is_empty() {
let msg = String::from("function takes at leat 1 arguments (0 given)");
return Err(vm.new_type_error(msg));
}
if args.len() > 2 {
let msg = format!("function takes at most 2 arguments ({} given)", args.len());
return Err(vm.new_type_error(msg));
}
let source = &args[0];
if args.len() == 1 {
if !PyIter::check(source.as_ref()) {
return Err(vm.new_type_error(String::from("Arguments must be iterators.")));
}
*zelf.source.write() = source.to_owned().try_into_value(vm)?;
return Ok(());
}
let active = &args[1];

if !PyIter::check(source.as_ref()) || !PyIter::check(active.as_ref()) {
return Err(vm.new_type_error(String::from("Arguments must be iterators.")));
}
*zelf.source.write() = source.to_owned().try_into_value(vm)?;
*zelf.active.write() = active.to_owned().try_into_value(vm)?;

@youknowone youknowone Oct 21, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

To avoid unpleasure broken synchronization, locking both and writing will be better.

Suggested change
*zelf.source.write() = source.to_owned().try_into_value(vm)?;
*zelf.active.write() = active.to_owned().try_into_value(vm)?;
let mut source_lock = zelf.source.write();
let mut active_lock = zelf.active.write();
*source_lock = source.to_owned().try_into_value(vm)?;
*active_lock = active.to_owned().try_into_value(vm)?;

Ok(())
}
}
impl IterNextIterable for PyItertoolsChain {}
impl IterNext for PyItertoolsChain {
Expand Down