-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix itertools chain #3788
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
Fix itertools chain #3788
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a9d16e2
test: itertools.chain evaluate lazliy
rng-dynamics 4c11720
feat: itertools.chain evaluate lazily
rng-dynamics 33bcc89
test: itertools.chain stop iteration on error
rng-dynamics ec18a12
refactor
rng-dynamics 76ef074
Release source when PyIterReturn::StopIteration
rng-dynamics 480f5f1
fix tests
rng-dynamics File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -7,7 +7,7 @@ mod decl { | |
rc::PyRc, | ||
}; | ||
use crate::{ | ||
builtins::{int, PyGenericAlias, PyInt, PyIntRef, PyTuple, PyTupleRef, PyTypeRef}, | ||
builtins::{int, PyGenericAlias, PyInt, PyIntRef, PyList, PyTuple, PyTupleRef, PyTypeRef}, | ||
convert::ToPyObject, | ||
function::{ArgCallable, FuncArgs, OptionalArg, OptionalOption, PosArgs}, | ||
identifier, | ||
|
@@ -25,19 +25,18 @@ mod decl { | |
#[pyclass(name = "chain")] | ||
#[derive(Debug, PyPayload)] | ||
struct PyItertoolsChain { | ||
iterables: Vec<PyObjectRef>, | ||
cur_idx: AtomicCell<usize>, | ||
cached_iter: PyRwLock<Option<PyIter>>, | ||
source: PyRwLock<Option<PyIter>>, | ||
active: PyRwLock<Option<PyIter>>, | ||
} | ||
|
||
#[pyimpl(with(IterNext))] | ||
impl PyItertoolsChain { | ||
#[pyslot] | ||
fn slot_new(cls: PyTypeRef, 8000 args: FuncArgs, vm: &VirtualMachine) -> PyResult { | ||
let args_list = PyList::from(args.args); | ||
PyItertoolsChain { | ||
iterables: args.args, | ||
cur_idx: AtomicCell::new(0), | ||
cached_iter: PyRwLock::new(None), | ||
source: PyRwLock::new(Some(args_list.to_pyobject(vm).get_iter(vm)?)), | ||
active: PyRwLock::new(None), | ||
} | ||
.into_ref_with_type(vm, cls) | ||
.map(Into::into) | ||
|
@@ -46,13 +45,12 @@ mod decl { | |
#[pyclassmethod] | ||
fn from_iterable( | ||
cls: PyTypeRef, | ||
iterable: PyObjectRef, | ||
source: PyObjectRef, | ||
vm: &VirtualMachine, | ||
) -> PyResult<PyRef<Self>> { | ||
PyItertoolsChain { | ||
iterables: iterable.try_to_value(vm)?, | ||
cur_idx: AtomicCell::new(0), | ||
cached_iter: PyRwLock::new(None), | ||
source: PyRwLock::new(Some(source.get_iter(vm)?)), | ||
active: PyRwLock::new(None), | ||
} | ||
.into_ref_with_type(vm, cls) | ||
} | ||
|
@@ -65,37 +63,45 @@ mod decl { | |
impl IterNextIterable for PyItertoolsChain {} | ||
impl IterNext for PyItertoolsChain { | ||
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> { | ||
loop { | ||
let pos = zelf.cur_idx.load(); | ||
if pos >= zelf.iterables.len() { | ||
break; | ||
} | ||
let cur_iter = if zelf.cached_iter.read().is_none() { | ||
// We need to call "get_iter" outside of the lock. | ||
let iter = zelf.iterables[pos].clone().get_iter(vm)?; | ||
*zelf.cached_iter.write() = Some(iter.clone()); | ||
iter | ||
} else if let Some(cached_iter) = (*zelf.cached_iter.read()).clone() { | ||
cached_iter | ||
} else { | ||
// Someone changed cached iter to None since we checked. | ||
continue; | ||
}; | ||
|
||
// We need to call "next" outside of the lock. | ||
match cur_iter.next(vm) { | ||
Ok(PyIterReturn::Return(ok)) => return Ok(PyIterReturn::Return(ok)), | ||
Ok(PyIterReturn::StopIteration(_)) => { | ||
zelf.cur_idx.fetch_add(1); | ||
*zelf.cached_iter.write() = None; | ||
} | ||
Err(err) => { | ||
return Err(err); | ||
let next = || { | ||
let source = zelf.source.read().clone(); | ||
match source { | ||
None => { | ||
return Ok(PyIterReturn::StopIteration(None)); | ||
} | ||
Some(source) => loop { | ||
let active = zelf.active.read().clone(); | ||
match active { | ||
None => match source.next(vm) { | ||
Ok(PyIterReturn::Return(ok)) => { | ||
*zelf.active.write() = Some(ok.get_iter(vm)?); | ||
} | ||
Ok(PyIterReturn::StopIteration(_)) => { | ||
return Ok(PyIterReturn::StopIteration(None)); | ||
} | ||
Err(err) => { | ||
return Err(err); | ||
} | ||
}, | ||
Some(active) => match active.next(vm) { | ||
Ok(PyIterReturn::Return(ok)) => { | ||
return Ok(PyIterReturn::Return(ok)); | ||
} | ||
Ok(PyIterReturn::StopIteration(_)) => { | ||
*zelf.active.write() = None; | ||
} | ||
Err(err) => { | ||
return Err(err); | ||
} | ||
}, | ||
} | ||
}, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📎 "It looks like you're if let Some(source) = zelf.source.read().clone() {
loop {
if let Some(active) = zelf.active.read().clone() {
match active.next(vm) {
Ok(PyIterReturn::Return(ok)) => {
return Ok(PyIterReturn::Return(ok));
}
Ok(PyIterReturn::StopIteration(_)) => {
*zelf.active.write() = None;
}
Err(err) => {
return Err(err);
}
}
} else {
match source.next(vm) {
Ok(PyIterReturn::Return(ok)) => {
*zelf.active.write() = Some(ok.get_iter(vm)?);
}
Ok(PyIterReturn::StopIteration(_)) => {
return Ok(PyIterReturn::StopIteration(None));
}
Err(err) => {
return Err(err);
}
}
}
}
} else {
Ok(PyIterReturn::StopIteration(None))
} |
||
} | ||
|
||
Ok(PyIterReturn::StopIteration(None)) | ||
}; | ||
next().map_err(|err| { | ||
*zelf.source.write() = None; | ||
err | ||
}) | ||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have different suggestion to @fanninpm's one. Let's keep indent depth shallower
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@youknowone that suggestion has a few syntax errors in lines 68 and 71.
EDIT: I stand corrected regarding line 68.