Skip to content

Add itertools.cycle() and itertools.chain.from_iterable() #1645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 25, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
58 changes: 57 additions & 1 deletion tests/snippets/stdlib_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,36 @@
with assert_raises(TypeError):
next(x)

# empty
with assert_raises(TypeError):
chain.from_iterable()

with assert_raises(TypeError):
chain.from_iterable("abc", "def")

with assert_raises(TypeError):
# iterables are lazily evaluated -- can be constructed but will fail to execute
list(chain.from_iterable([1, 2, 3]))

with assert_raises(TypeError):
list(chain(1))

args = ["abc", "def"]
assert list(chain.from_iterable(args)) == ['a', 'b', 'c', 'd', 'e', 'f']

args = [[], "", b"", ()]
assert list(chain.from_iterable(args)) == []

args = ["ab", "cd", (), 'e']
assert list(chain.from_iterable(args)) == ['a', 'b', 'c', 'd', 'e']

x = chain.from_iterable(["ab", 1])
assert next(x) == 'a'
assert next(x) == 'b'
with assert_raises(TypeError):
next(x)


# itertools.count tests

# default arguments
Expand Down Expand Up @@ -76,6 +106,32 @@
# assert next(c) == 1.5


# itertools.cycle tests

r = itertools.cycle([1, 2, 3])
assert next(r) == 1
assert next(r) == 2
assert next(r) == 3
assert next(r) == 1
assert next(r) == 2
assert next(r) == 3
assert next(r) == 1

r = itertools.cycle([1])
assert next(r) == 1
assert next(r) == 1
assert next(r) == 1

r = itertools.cycle([])
with assert_raises(StopIteration):
next(r)

with assert_raises(TypeError):
itertools.cycle(None)

with assert_raises(TypeError):
itertools.cycle(10)

# itertools.repeat tests

# no times
Expand All @@ -91,7 +147,7 @@
with assert_raises(StopIteration):
next(r)

# timees = 0
# times = 0
r = itertools.repeat(1, 0)
with assert_raises(StopIteration):
next(r)
Expand Down
89 changes: 88 additions & 1 deletion vm/src/stdlib/itertools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use num_traits::ToPrimitive;
use crate::function::{Args, OptionalArg, OptionalOption, PyFuncArgs};
use crate::obj::objbool;
use crate::obj::objint::{self, PyInt, PyIntRef};
use crate::obj::objiter::{call_next, get_all, get_iter, new_stop_iteration};
use crate::obj::objiter::{call_next, get_all, get_iter, get_next_object, new_stop_iteration};
use crate::obj::objtuple::PyTuple;
use crate::obj::objtype::{self, PyClassRef};
use crate::pyobject::{
Expand Down Expand Up @@ -73,6 +73,22 @@ impl PyItertoolsChain {
fn iter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
zelf
}

#[pyclassmethod(name = "from_iterable")]
fn from_iterable(
cls: PyClassRef,
iterable: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyRef<Self>> {
let it = get_iter(vm, &iterable)?;
let iterables = get_all(vm, &it)?;

PyItertoolsChain {
iterables,
cur: RefCell::new((0, None)),
}
.into_ref_with_type(vm, cls)
}
}

#[pyclass(name = "compress")]
Expand Down Expand Up @@ -177,6 +193,73 @@ impl PyItertoolsCount {
}
}

#[pyclass]
#[derive(Debug)]
struct PyItertoolsCycle {
iter: RefCell<PyObjectRef>,
saved: RefCell<Vec<PyObjectRef>>,
index: Cell<usize>,
first_pass: Cell<bool>,
}

impl PyValue for PyItertoolsCycle {
fn class(vm: &VirtualMachine) -> PyClassRef {
vm.class("itertools", "cycle")
}
}

#[pyimpl]
impl PyItertoolsCycle {
#[pyslot(new)]
fn tp_new(
cls: PyClassRef,
iterable: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyRef<Self>> {
let iter = get_iter(vm, &iterable)?;

PyItertoolsCycle {
iter: RefCell::new(iter.clone()),
saved: RefCell::new(Vec::new()),
index: Cell::new(0),
first_pass: Cell::new(false),
}
.into_ref_with_type(vm, cls)
}

#[pymethod(name = "__next__")]
fn next(&self, vm: &VirtualMachine) -> PyResult {
let item = if let Some(item) = get_next_object(vm, &self.iter.borrow())? {
if self.first_pass.get() {
Copy link
Contributor Author

@dralley dralley Dec 25, 2019

Choose a reason for hiding this comment

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

So I copied the CPython implementation verbatim basically, but I'm confused as to why this variable exists.

https://github.com/python/cpython/blob/master/Modules/itertoolsmodule.c#L1015-L1043

If you look at the code, it appears it is read from but never written to, except for __setstate__. It looks like a few years ago it was used in the actual algorithm, but not anymore, after this commit:

python/cpython@ca3788c#diff-2e8fa44b11c7ea371dfc1101d176ec87L953

I'm not sure whether it is left behind intentionally (needed for compatibility purposes or something) or if it was an accident.

Copy link
Member

Choose a reason for hiding this comment

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

That's really weird; I think it's fine for now but it'll definitely be interesting to see what the CPython team thinks.

return Ok(item);
}

self.saved.borrow_mut().push(item.clone());
item
} else {
if self.saved.borrow().len() == 0 {
return Err(new_stop_iteration(vm));
}

let last_index = self.index.get();
self.index.set(self.index.get() + 1);

if self.index.get() >= self.saved.borrow().len() {
self.index.set(0);
}

self.saved.borrow()[last_index].clone()
};

Ok(item)
}

#[pymethod(name = "__iter__")]
fn iter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
zelf
}
}

#[pyclass]
#[derive(Debug)]
struct PyItertoolsRepeat {
Expand Down Expand Up @@ -1177,6 +1260,9 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let count = ctx.new_class("count", ctx.object());
PyItertoolsCount::extend_class(ctx, &count);

let cycle = ctx.new_class("cycle", ctx.object());
PyItertoolsCycle::extend_class(ctx, &cycle);

let dropwhile = ctx.new_class("dropwhile", ctx.object());
PyItertoolsDropwhile::extend_class(ctx, &dropwhile);

Expand Down Expand Up @@ -1211,6 +1297,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
"compress" => compress,
"combinations" => combinations,
"count" => count,
"cycle" => cycle,
"dropwhile" => dropwhile,
"islice" => islice,
"filterfalse" => filterfalse,
Expand Down