Skip to content

Implement PyObject_SelfIter to actually share same function #4945

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 3 commits into from
May 5, 2023
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
8 changes: 4 additions & 4 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ mod array {
SliceableSequenceOp,
},
types::{
AsBuffer, AsMapping, AsSequence, Comparable, Constructor, IterNext,
IterNextIterable, Iterable, PyComparisonOp, Representable,
AsBuffer, AsMapping, AsSequence, Comparable, Constructor, IterNext, Iterable,
PyComparisonOp, Representable, SelfIter,
},
AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
},
Expand Down Expand Up @@ -1405,7 +1405,7 @@ mod array {
internal: PyMutex<PositionIterInternal<PyArrayRef>>,
}

#[pyclass(with(IterNext), flags(HAS_DICT))]
#[pyclass(with(IterNext, Iterable), flags(HAS_DICT))]
impl PyArrayIter {
#[pymethod(magic)]
fn setstate(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
Expand All @@ -1422,7 +1422,7 @@ mod array {
}
}

impl IterNextIterable for PyArrayIter {}
impl SelfIter for PyArrayIter {}
impl IterNext for PyArrayIter {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
zelf.internal.lock().next(|array, pos| {
Expand Down
6 changes: 3 additions & 3 deletions stdlib/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod _csv {
function::{ArgIterable, ArgumentError, FromArgs, FuncArgs},
match_class,
protocol::{PyIter, PyIterReturn},
types::{IterNext, IterNextIterable},
types::{IterNext, Iterable, SelfIter},
AsObject, Py, PyObjectRef, PyPayload, PyResult, TryFromObject, VirtualMachine,
};
use itertools::{self, Itertools};
Expand Down Expand Up @@ -166,9 +166,9 @@ mod _csv {
}
}

#[pyclass(with(IterNext))]
#[pyclass(with(IterNext, Iterable))]
impl Reader {}
impl IterNextIterable for Reader {}
impl SelfIter for Reader {}
impl IterNext for Reader {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
let string = match zelf.iter.next(vm)? {
Expand Down
6 changes: 3 additions & 3 deletions stdlib/src/pystruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) mod _struct {
function::{ArgBytesLike, ArgMemoryBuffer, PosArgs},
match_class,
protocol::PyIterReturn,
types::{Constructor, IterNext, IterNextIterable},
types::{Constructor, IterNext, Iterable, SelfIter},
AsObject, Py, PyObjectRef, PyPayload, PyResult, TryFromObject, VirtualMachine,
};
use crossbeam_utils::atomic::AtomicCell;
Expand Down Expand Up @@ -194,14 +194,14 @@ pub(crate) mod _struct {
}
}

#[pyclass(with(IterNext))]
#[pyclass(with(IterNext, Iterable))]
impl UnpackIterator {
#[pymethod(magic)]
fn length_hint(&self) -> usize {
self.buffer.len().saturating_sub(self.offset.load()) / self.format_spec.size
}
}
impl IterNextIterable for UnpackIterator {}
impl SelfIter for UnpackIterator {}
impl IterNext for UnpackIterator {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
let size = zelf.format_spec.size;
Expand Down
8 changes: 4 additions & 4 deletions stdlib/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ mod _sqlite {
protocol::{PyBuffer, PyIterReturn, PyMappingMethods, PySequence, PySequenceMethods},
sliceable::{SaturatedSliceIter, SliceableSequenceOp},
types::{
AsMapping, AsSequence, Callable, Comparable, Constructor, Hashable, IterNext,
IterNextIterable, Iterable, PyComparisonOp,
AsMapping, AsSequence, Callable, Comparable, Constructor, Hashable, IterNext, Iterable,
PyComparisonOp, SelfIter,
},
utils::ToCString,
AsObject, Py, PyAtomicRef, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
Expand Down Expand Up @@ -1377,7 +1377,7 @@ mod _sqlite {
statement: Option<PyRef<Statement>>,
}

#[pyclass(with(Constructor, IterNext), flags(BASETYPE))]
#[pyclass(with(Constructor, IterNext, Iterable), flags(BASETYPE))]
impl Cursor {
fn new(
connection: PyRef<Connection>,
Expand Down Expand Up @@ -1708,7 +1708,7 @@ mod _sqlite {
}
}

impl IterNextIterable for Cursor {}
impl SelfIter for Cursor {}
impl IterNext for Cursor {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
let mut inner = zelf.inner(vm)?;
Expand Down
10 changes: 5 additions & 5 deletions vm/src/builtins/asyncgenerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
frame::FrameRef,
function::OptionalArg,
protocol::PyIterReturn,
types::{Constructor, IterNext, IterNextIterable, Representable, Unconstructible},
types::{Constructor, IterNext, Iterable, Representable, SelfIter, Unconstructible},
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
};

Expand Down Expand Up @@ -195,7 +195,7 @@ impl PyPayload for PyAsyncGenASend {
}
}

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

impl IterNextIterable for PyAsyncGenASend {}
impl SelfIter for PyAsyncGenASend {}
impl IterNext for PyAsyncGenASend {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
PyIterReturn::from_pyresult(zelf.send(vm.ctx.none(), vm), vm)
Expand All @@ -290,7 +290,7 @@ impl PyPayload for PyAsyncGenAThrow {
}
}

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

impl IterNextIterable for PyAsyncGenAThrow {}
impl SelfIter for PyAsyncGenAThrow {}
impl IterNext for PyAsyncGenAThrow {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
PyIterReturn::from_pyresult(zelf.send(vm.ctx.none(), vm), 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 @@ -31,7 +31,7 @@ use crate::{
sliceable::{SequenceIndex, SliceableSequenceMutOp, SliceableSequenceOp},
types::{
AsBuffer, AsMapping, AsNumber, AsSequence, Callable, Comparable, Constructor, Initializer,
IterNext, IterNextIterable, Iterable, PyComparisonOp, Representable, Unconstructible,
IterNext, Iterable, PyComparisonOp, Representable, SelfIter, Unconstructible,
},
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
VirtualMachine,
Expand Down Expand Up @@ -891,7 +891,7 @@ impl PyPayload for PyByteArrayIterator {
}
}

#[pyclass(with(Constructor, IterNext))]
#[pyclass(with(Constructor, IterNext, Iterable))]
impl PyByteArrayIterator {
#[pymethod(magic)]
fn length_hint(&self) -> usize {
Expand All @@ -914,7 +914,7 @@ impl PyByteArrayIterator {

impl Unconstructible for PyByteArrayIterator {}

impl IterNextIterable for PyByteArrayIterator {}
impl SelfIter for PyByteArrayIterator {}
impl IterNext for PyByteArrayIterator {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
zelf.internal.lock().next(|bytearray, 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 @@ -21,7 +21,7 @@ use crate::{
sliceable::{SequenceIndex, SliceableSequenceOp},
types::{
AsBuffer, AsMapping, AsNumber, AsSequence, Callable, Comparable, Constructor, Hashable,
IterNext, IterNextIterable, Iterable, PyComparisonOp, Representable, Unconstructible,
IterNext, Iterable, PyComparisonOp, Representable, SelfIter, Unconstructible,
},
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
TryFromBorrowedObject, TryFromObject, VirtualMachine,
Expand Down Expand Up @@ -688,7 +688,7 @@ impl PyPayload for PyBytesIterator {
}
}

#[pyclass(with(Constructor, IterNext))]
#[pyclass(with(Constructor, IterNext, Iterable))]
impl PyBytesIterator {
#[pymethod(magic)]
fn length_hint(&self) -> usize {
Expand All @@ -711,7 +711,7 @@ impl PyBytesIterator {
}
impl Unconstructible for PyBytesIterator {}

impl IterNextIterable for PyBytesIterator {}
impl SelfIter for PyBytesIterator {}
impl IterNext for PyBytesIterator {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
zelf.internal.lock().next(|bytes, pos| {
Expand Down
8 changes: 4 additions & 4 deletions vm/src/builtins/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
frame::FrameRef,
function::OptionalArg,
protocol::PyIterReturn,
types::{Constructor, IterNext, IterNextIterable, Representable, Unconstructible},
types::{Constructor, IterNext, Iterable, Representable, SelfIter, Unconstructible},
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
};

Expand Down Expand Up @@ -108,7 +108,7 @@ impl Representable for PyCoroutine {
}
}

impl IterNextIterable for PyCoroutine {}
impl SelfIter for PyCoroutine {}
impl IterNext for PyCoroutine {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
Self::send(zelf, vm.ctx.none(), vm)
Expand All @@ -128,7 +128,7 @@ impl PyPayload for PyCoroutineWrapper {
}
}

#[pyclass(with(IterNext))]
#[pyclass(with(IterNext, Iterable))]
impl PyCoroutineWrapper {
#[pymethod]
fn send(&self, val: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
Expand All @@ -147,7 +147,7 @@ impl PyCoroutineWrapper {
}
}

impl IterNextIterable for PyCoroutineWrapper {}
impl SelfIter for PyCoroutineWrapper {}
impl IterNext for PyCoroutineWrapper {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
Self::send(zelf, vm.ctx.none(), vm)
Expand Down
10 changes: 5 additions & 5 deletions vm/src/builtins/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
recursion::ReprGuard,
types::{
AsMapping, AsNumber, AsSequence, Callable, Comparable, Constructor, Initializer, IterNext,
IterNextIterable, Iterable, PyComparisonOp, Representable, Unconstructible,
Iterable, PyComparisonOp, Representable, SelfIter, Unconstructible,
},
vm::VirtualMachine,
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyRefExact, PyResult,
Expand Down Expand Up @@ -839,7 +839,7 @@ macro_rules! dict_view {
}
}

#[pyclass(with(Constructor, IterNext))]
#[pyclass(with(Constructor, IterNext, Iterable))]
impl $iter_name {
fn new(dict: PyDictRef) -> Self {
$iter_name {
Expand Down Expand Up @@ -870,7 +870,7 @@ macro_rules! dict_view {
}
impl Unconstructible for $iter_name {}

impl IterNextIterable for $iter_name {}
impl SelfIter for $iter_name {}
impl IterNext for $iter_name {
#[allow(clippy::redundant_closure_call)]
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
Expand Down Expand Up @@ -912,7 +912,7 @@ macro_rules! dict_view {
}
}

#[pyclass(with(Constructor, IterNext))]
#[pyclass(with(Constructor, IterNext, Iterable))]
impl $reverse_iter_name {
fn new(dict: PyDictRef) -> Self {
let size = dict.size();
Expand Down Expand Up @@ -948,7 +948,7 @@ macro_rules! dict_view {
}
impl Unconstructible for $reverse_iter_name {}

impl IterNextIterable for $reverse_iter_name {}
impl SelfIter for $reverse_iter_name {}
impl IterNext for $reverse_iter_name {
#[allow(clippy::redundant_closure_call)]
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
Expand Down
10 changes: 5 additions & 5 deletions vm/src/builtins/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
convert::ToPyObject,
function::OptionalArg,
protocol::{PyIter, PyIterReturn},
types::{Constructor, IterNext, IterNextIterable},
types::{Constructor, IterNext, Iterable, SelfIter},
AsObject, Context, Py, PyObjectRef, PyPayload, PyResult, VirtualMachine,
};
use num_bigint::BigInt;
Expand Down Expand Up @@ -52,7 +52,7 @@ impl Constructor for PyEnumerate {
}
}

#[pyclass(with(Py, IterNext, Constructor), flags(BASETYPE))]
#[pyclass(with(Py, IterNext, Iterable, Constructor), flags(BASETYPE))]
impl PyEnumerate {
#[pyclassmethod(magic)]
fn class_getitem(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
Expand All @@ -71,7 +71,7 @@ impl Py<PyEnumerate> {
}
}

impl IterNextIterable for PyEnumerate {}
impl SelfIter for PyEnumerate {}
impl IterNext for PyEnumerate {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
let next_obj = match zelf.iterator.next(vm)? {
Expand All @@ -97,7 +97,7 @@ impl PyPayload for PyReverseSequenceIterator {
}
}

#[pyclass(with(IterNext))]
#[pyclass(with(IterNext, Iterable))]
impl PyReverseSequenceIterator {
pub fn new(obj: PyObjectRef, len: usize) -> Self {
let position = len.saturating_sub(1);
Expand Down Expand Up @@ -130,7 +130,7 @@ impl PyReverseSequenceIterator {
}
}

impl IterNextIterable for PyReverseSequenceIterator {}
impl SelfIter for PyReverseSequenceIterator {}
impl IterNext for PyReverseSequenceIterator {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
zelf.internal
Expand Down
6 changes: 3 additions & 3 deletions vm/src/builtins/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{PyType, PyTypeRef};
use crate::{
class::PyClassImpl,
protocol::{PyIter, PyIterReturn},
types::{Constructor, IterNext, IterNextIterable},
types::{Constructor, IterNext, Iterable, SelfIter},
Context, Py, PyObjectRef, PyPayload, PyResult, VirtualMachine,
};

Expand Down Expand Up @@ -32,7 +32,7 @@ impl Constructor for PyFilter {
}
}

#[pyclass(with(IterNext, Constructor), flags(BASETYPE))]
#[pyclass(with(IterNext, Iterable, Constructor), flags(BASETYPE))]
impl PyFilter {
#[pymethod(magic)]
fn reduce(&self, vm: &VirtualMachine) -> (PyTypeRef, (PyObjectRef, PyIter)) {
Expand All @@ -43,7 +43,7 @@ impl PyFilter {
}
}

impl IterNextIterable for PyFilter {}
impl SelfIter for PyFilter {}
impl IterNext for PyFilter {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
let predicate = &zelf.predicate;
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 @@ -9,7 +9,7 @@ use crate::{
frame::FrameRef,
function::OptionalArg,
protocol::PyIterReturn,
types::{Constructor, IterNext, IterNextIterable, Representable, Unconstructible},
types::{Constructor, IterNext, Iterable, Representable, SelfIter, Unconstructible},
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
};

Expand All @@ -25,7 +25,7 @@ impl PyPayload for PyGenerator {
}
}

#[pyclass(with(Py, Constructor, IterNext))]
#[pyclass(with(Py, Constructor, IterNext, Iterable))]
impl PyGenerator {
pub fn as_coro(&self) -> &Coro {
&self.inner
Expand Down Expand Up @@ -104,7 +104,7 @@ impl Representable for PyGenerator {
}
}

impl IterNextIterable for PyGenerator {}
impl SelfIter for PyGenerator {}
impl IterNext for PyGenerator {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
zelf.send(vm.ctx.none(), vm)
Expand Down
Loading