Skip to content

PyIter protocol object #3117

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
Sep 29, 2021
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
10 changes: 5 additions & 5 deletions vm/src/builtins/asyncgenerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
coroutine::{Coro, Variant},
frame::FrameRef,
function::OptionalArg,
slots::{IteratorIterable, PyIter},
slots::{IteratorIterable, SlotIterator},
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
VirtualMachine,
};
Expand Down Expand Up @@ -182,7 +182,7 @@ impl PyValue for PyAsyncGenASend {
}
}

#[pyimpl(with(PyIter))]
#[pyimpl(with(SlotIterator))]
impl PyAsyncGenASend {
#[pymethod(name = "__await__")]
fn r#await(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
Expand Down Expand Up @@ -255,7 +255,7 @@ impl PyAsyncGenASend {
}

impl IteratorIterable for PyAsyncGenASend {}
impl PyIter for PyAsyncGenASend {
impl SlotIterator for PyAsyncGenASend {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.send(vm.ctx.none(), vm)
}
Expand All @@ -276,7 +276,7 @@ impl PyValue for PyAsyncGenAThrow {
}
}

#[pyimpl(with(PyIter))]
#[pyimpl(with(SlotIterator))]
impl PyAsyncGenAThrow {
#[pymethod(name = "__await__")]
fn r#await(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
Expand Down Expand Up @@ -397,7 +397,7 @@ impl PyAsyncGenAThrow {
}

impl IteratorIterable for PyAsyncGenAThrow {}
impl PyIter for PyAsyncGenAThrow {
impl SlotIterator for PyAsyncGenAThrow {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.send(vm.ctx.none(), vm)
}
Expand Down
6 changes: 3 additions & 3 deletions vm/src/builtins/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
sliceable::{PySliceableSequence, PySliceableSequenceMut, SequenceIndex},
slots::{
AsBuffer, Callable, Comparable, Hashable, Iterable, IteratorIterable, PyComparisonOp,
PyIter, Unhashable,
SlotIterator, Unhashable,
},
utils::Either,
IdProtocol, IntoPyObject, PyClassDef, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef,
Expand Down Expand Up @@ -741,10 +741,10 @@ impl PyValue for PyByteArrayIterator {
}
}

#[pyimpl(with(PyIter))]
#[pyimpl(with(SlotIterator))]
impl PyByteArrayIterator {}
impl IteratorIterable for PyByteArrayIterator {}
impl PyIter for PyByteArrayIterator {
impl SlotIterator for PyByteArrayIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
let pos = zelf.position.fetch_add(1);
if let Some(&ret) = zelf.bytearray.borrow_buf().get(pos) {
Expand Down
6 changes: 3 additions & 3 deletions vm/src/builtins/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
protocol::{BufferInternal, BufferOptions, PyBuffer},
slots::{
AsBuffer, Callable, Comparable, Hashable, Iterable, IteratorIterable, PyComparisonOp,
PyIter, SlotConstructor,
SlotConstructor, SlotIterator,
},
utils::Either,
IdProtocol, IntoPyObject, IntoPyResult, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef,
Expand Down Expand Up @@ -594,10 +594,10 @@ impl PyValue for PyBytesIterator {
}
}

#[pyimpl(with(PyIter))]
#[pyimpl(with(SlotIterator))]
impl PyBytesIterator {}
impl IteratorIterable for PyBytesIterator {}
impl PyIter for PyBytesIterator {
impl SlotIterator for PyBytesIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
let pos = zelf.position.fetch_add(1);
if let Some(&ret) = zelf.bytes.as_bytes().get(pos) {
Expand Down
10 changes: 5 additions & 5 deletions vm/src/builtins/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
coroutine::{Coro, Variant},
frame::FrameRef,
function::OptionalArg,
slots::{IteratorIterable, PyIter},
slots::{IteratorIterable, SlotIterator},
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
};

Expand All @@ -19,7 +19,7 @@ impl PyValue for PyCoroutine {
}
}

#[pyimpl(with(PyIter))]
#[pyimpl(with(SlotIterator))]
impl PyCoroutine {
pub fn as_coro(&self) -> &Coro {
&self.inner
Expand Down Expand Up @@ -102,7 +102,7 @@ impl PyCoroutine {
}

impl IteratorIterable for PyCoroutine {}
impl PyIter for PyCoroutine {
impl SlotIterator for PyCoroutine {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.send(vm.ctx.none(), vm)
}
Expand All @@ -120,7 +120,7 @@ impl PyValue for PyCoroutineWrapper {
}
}

#[pyimpl(with(PyIter))]
#[pyimpl(with(SlotIterator))]
impl PyCoroutineWrapper {
#[pymethod]
fn send(&self, val: PyObjectRef, vm: &VirtualMachine) -> PyResult {
Expand All @@ -140,7 +140,7 @@ impl PyCoroutineWrapper {
}

impl IteratorIterable for PyCoroutineWrapper {}
impl PyIter for PyCoroutineWrapper {
impl SlotIterator for PyCoroutineWrapper {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.send(vm.ctx.none(), vm)
}
Expand Down
18 changes: 10 additions & 8 deletions vm/src/builtins/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::{
dictdatatype::{self, DictKey},
function::{ArgIterable, FuncArgs, KwArgs, OptionalArg},
iterator,
slots::{Comparable, Hashable, Iterable, IteratorIterable, PyComparisonOp, PyIter, Unhashable},
slots::{
Comparable, Hashable, Iterable, IteratorIterable, PyComparisonOp, SlotIterator, Unhashable,
},
vm::{ReprGuard, VirtualMachine},
IdProtocol, IntoPyObject, ItemProtocol,
PyArithmaticValue::*,
Expand Down Expand Up @@ -83,13 +85,13 @@ impl PyDict {
return Err(vm.new_runtime_error("dict mutated during update".to_owned()));
}
} else if let Some(keys) = vm.get_method(dict_obj.clone(), "keys") {
let keys = iterator::get_iter(vm, vm.invoke(&keys?, ())?)?;
let keys = vm.invoke(&keys?, ())?.get_iter(vm)?;
while let Some(key) = iterator::get_next_object(vm, &keys)? {
let val = dict_obj.get_item(key.clone(), vm)?;
dict.insert(vm, key, val)?;
}
} else {
let iter = iterator::get_iter(vm, dict_obj)?;
let iter = dict_obj.get_iter(vm)?;
loop {
fn err(vm: &VirtualMachine) -> PyBaseExceptionRef {
vm.new_value_error("Iterator must have exactly two elements".to_owned())
Expand All @@ -98,7 +100,7 @@ impl PyDict {
Some(obj) => obj,
None => break,
};
let elem_iter = iterator::get_iter(vm, element)?;
let elem_iter = element.get_iter(vm)?;
let key = iterator::get_next_object(vm, &elem_iter)?.ok_or_else(|| err(vm))?;
let value =
iterator::get_next_object(vm, &elem_iter)?.ok_or_else(|| err(vm))?;
Expand Down Expand Up @@ -708,7 +710,7 @@ macro_rules! dict_iterator {
}
}

#[pyimpl(with(PyIter))]
#[pyimpl(with(SlotIterator))]
impl $iter_name {
fn new(dict: PyDictRef) -> Self {
$iter_name {
Expand All @@ -730,7 +732,7 @@ macro_rules! dict_iterator {
}

impl IteratorIterable for $iter_name {}
impl PyIter for $iter_name {
impl SlotIterator for $iter_name {
#[allow(clippy::redundant_closure_call)]
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
match zelf.status.load() {
Expand Down Expand Up @@ -769,7 +771,7 @@ macro_rules! dict_iterator {
}
}

#[pyimpl(with(PyIter))]
#[pyimpl(with(SlotIterator))]
impl $reverse_iter_name {
fn new(dict: PyDictRef) -> Self {
$reverse_iter_name {
Expand All @@ -791,7 +793,7 @@ macro_rules! dict_iterator {
}

impl IteratorIterable for $reverse_iter_name {}
impl PyIter for $reverse_iter_name {
impl SlotIterator for $reverse_iter_name {
#[allow(clippy::redundant_closure_call)]
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
match zelf.status.load() {
Expand Down
29 changes: 15 additions & 14 deletions vm/src/builtins/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use super::{
use crate::common::lock::PyRwLock;
use crate::{
function::OptionalArg,
iterator,
slots::{IteratorIterable, PyIter, SlotConstructor},
protocol::PyIter,
slots::{IteratorIterable, SlotConstructor, SlotIterator},
IntoPyObject, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
TypeProtocol, VirtualMachine,
};
Expand All @@ -19,7 +19,7 @@ use num_traits::Zero;
#[derive(Debug)]
pub struct PyEnumerate {
counter: PyRwLock<BigInt>,
iterator: PyObjectRef,
iterator: PyIter,
}

impl PyValue for PyEnumerate {
Expand All @@ -30,19 +30,20 @@ impl PyValue for PyEnumerate {

#[derive(FromArgs)]
pub struct EnumerateArgs {
iterable: PyObjectRef,
iterator: PyIter,
#[pyarg(any, optional)]
start: OptionalArg<PyIntRef>,
}

impl SlotConstructor for PyEnumerate {
type Args = EnumerateArgs;

fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
let counter = args
.start
.map_or_else(BigInt::zero, |start| start.as_bigint().clone());
let iterator = iterator::get_iter(vm, args.iterable)?;
fn py_new(
cls: PyTypeRef,
Self::Args { iterator, start }: Self::Args,
vm: &VirtualMachine,
) -> PyResult {
let counter = start.map_or_else(BigInt::zero, |start| start.as_bigint().clone());
PyEnumerate {
counter: PyRwLock::new(counter),
iterator,
Expand All @@ -51,13 +52,13 @@ impl SlotConstructor for PyEnumerate {
}
}

#[pyimpl(with(PyIter, SlotConstructor), flags(BASETYPE))]
#[pyimpl(with(SlotIterator, SlotConstructor), flags(BASETYPE))]
impl PyEnumerate {}

impl IteratorIterable for PyEnumerate {}
impl PyIter for PyEnumerate {
impl SlotIterator for PyEnumerate {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
let next_obj = iterator::call_next(vm, &zelf.iterator)?;
let next_obj = zelf.iterator.next(vm)?;
let mut counter = zelf.counter.write();
let position = counter.clone();
*counter += 1;
Expand All @@ -79,7 +80,7 @@ impl PyValue for PyReverseSequenceIterator {
}
}

#[pyimpl(with(PyIter))]
#[pyimpl(with(SlotIterator))]
impl PyReverseSequenceIterator {
pub fn new(obj: PyObjectRef, len: usize) -> Self {
Self {
Expand Down Expand Up @@ -137,7 +138,7 @@ impl PyReverseSequenceIterator {
}

impl IteratorIterable for PyReverseSequenceIterator {}
impl PyIter for PyReverseSequenceIterator {
impl SlotIterator for PyReverseSequenceIterator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
if let Exhausted = zelf.status.load() {
return Err(vm.new_stop_iteration());
Expand Down
31 changes: 8 additions & 23 deletions vm/src/builtins/filter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::PyTypeRef;
use crate::{
iterator,
slots::{IteratorIterable, PyIter, SlotConstructor},
protocol::PyIter,
slots::{IteratorIterable, SlotConstructor, SlotIterator},
PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
};

Expand All @@ -13,7 +13,7 @@ use crate::{
#[derive(Debug)]
pub struct PyFilter {
predicate: PyObjectRef,
iterator: PyObjectRef,
iterator: PyIter,
}

impl PyValue for PyFilter {
Expand All @@ -22,24 +22,10 @@ impl PyValue for PyFilter {
}
}

#[derive(FromArgs)]
pub struct FilterArgs {
#[pyarg(positional)]
function: PyObjectRef,
#[pyarg(positional)]
iterable: PyObjectRef,
}

impl SlotConstructor for PyFilter {
type Args = FilterArgs;

fn py_new(
cls: PyTypeRef,
Self::Args { function, iterable }: Self::Args,
vm: &VirtualMachine,
) -> PyResult {
let iterator = iterator::get_iter(vm, iterable)?;
type Args = (PyObjectRef, PyIter);

fn py_new(cls: PyTypeRef, (function, iterator): Self::Args, vm: &VirtualMachine) -> PyResult {
Self {
predicate: function,
iterator,
Expand All @@ -48,16 +34,15 @@ impl SlotConstructor for PyFilter {
}
}

#[pyimpl(with(PyIter, SlotConstructor), flags(BASETYPE))]
#[pyimpl(with(SlotIterator, SlotConstructor), flags(BASETYPE))]
impl PyFilter {}

impl IteratorIterable for PyFilter {}
impl PyIter for PyFilter {
impl SlotIterator for PyFilter {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
let predicate = &zelf.predicate;
let iterator = &zelf.iterator;
loop {
let next_obj = iterator::call_next(vm, iterator)?;
let next_obj = zelf.iterator.next(vm)?;
let predicate_value = if vm.is_none(predicate) {
next_obj.clone()
} else {
Expand Down
6 changes: 3 additions & 3 deletions vm/src/builtins/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
coroutine::{Coro, Variant},
frame::FrameRef,
function::OptionalArg,
slots::{IteratorIterable, PyIter},
slots::{IteratorIterable, SlotIterator},
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
};

Expand All @@ -23,7 +23,7 @@ impl PyValue for PyGenerator {
}
}

#[pyimpl(with(PyIter))]
#[pyimpl(with(SlotIterator))]
impl PyGenerator {
pub fn as_coro(&self) -> &Coro {
&self.inner
Expand Down Expand Up @@ -95,7 +95,7 @@ impl PyGenerator {
}

impl IteratorIterable for PyGenerator {}
impl PyIter for PyGenerator {
impl SlotIterator for PyGenerator {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.send(vm.ctx.none(), vm)
}
Expand Down
Loading