Skip to content

Commit bfa74dd

Browse files
committed
Add dropwhile.__reduce__
1 parent 3a36e55 commit bfa74dd

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

extra_tests/snippets/stdlib_itertools.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from testutils import assert_raises
44

5+
import pickle
56

67
# itertools.chain tests
78
chain = itertools.chain
@@ -279,6 +280,17 @@ def assert_matches_seq(it, seq):
279280
with assert_raises(StopIteration):
280281
next(it)
281282

283+
def underten(x):
284+
return x<10
285+
286+
it = itertools.dropwhile(underten, [1, 3, 5, 20, 2, 4, 6, 8])
287+
assert pickle.dumps(it, 0) == b'citertools\ndropwhile\np0\n(c__main__\nunderten\np1\nc__builtin__\niter\np2\n((lp3\nI1\naI3\naI5\naI20\naI2\naI4\naI6\naI8\natp4\nRp5\nI0\nbtp6\nRp7\nI0\nb.'
288+
assert pickle.dumps(it, 1) == b'citertools\ndropwhile\nq\x00(c__main__\nunderten\nq\x01c__builtin__\niter\nq\x02(]q\x03(K\x01K\x03K\x05K\x14K\x02K\x04K\x06K\x08etq\x04Rq\x05K\x00btq\x06Rq\x07K\x00b.'
289+
assert pickle.dumps(it, 2) == b'\x80\x02citertools\ndropwhile\nq\x00c__main__\nunderten\nq\x01c__builtin__\niter\nq\x02]q\x03(K\x01K\x03K\x05K\x14K\x02K\x04K\x06K\x08e\x85q\x04Rq\x05K\x00b\x86q\x06Rq\x07K\x00b.'
290+
assert pickle.dumps(it, 3) == b'\x80\x03citertools\ndropwhile\nq\x00c__main__\nunderten\nq\x01cbuiltins\niter\nq\x02]q\x03(K\x01K\x03K\x05K\x14K\x02K\x04K\x06K\x08e\x85q\x04Rq\x05K\x00b\x86q\x06Rq\x07K\x00b.'
291+
assert pickle.dumps(it, 4) == b'\x80\x04\x95i\x00\x00\x00\x00\x00\x00\x00\x8c\titertools\x94\x8c\tdropwhile\x94\x93\x94\x8c\x08__main__\x94\x8c\x08underten\x94\x93\x94\x8c\x08builtins\x94\x8c\x04iter\x94\x93\x94]\x94(K\x01K\x03K\x05K\x14K\x02K\x04K\x06K\x08e\x85\x94R\x94K\x00b\x86\x94R\x94K\x00b.'
292+
assert pickle.dumps(it, 5) == b'\x80\x05\x95i\x00\x00\x00\x00\x00\x00\x00\x8c\titertools\x94\x8c\tdropwhile\x94\x93\x94\x8c\x08__main__\x94\x8c\x08underten\x94\x93\x94\x8c\x08builtins\x94\x8c\x04iter\x94\x93\x94]\x94(K\x01K\x03K\x05K\x14K\x02K\x04K\x06K\x08e\x85\x94R\x94K\x00b\x86\x94R\x94K\x00b.'
293+
282294

283295
# itertools.accumulate
284296
it = itertools.accumulate([6, 3, 7, 1, 0, 9, 8, 8])

vm/src/stdlib/itertools.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@ pub(crate) use decl::make_module;
22

33
#[pymodule(name = "itertools")]
44
mod decl {
5-
use crate::common::{
6-
lock::{PyMutex, PyRwLock, PyRwLockWriteGuard},
7-
rc::PyRc,
8-
};
95
use crate::{
106
builtins::{int, PyGenericAlias, PyInt, PyIntRef, PyList, PyTuple, PyTupleRef, PyTypeRef},
7+
common::{
8+
lock::{PyMutex, PyRwLock, PyRwLockWriteGuard},
9+
rc::PyRc,
10+
},
1111
convert::ToPyObject,
12-
function::{ArgCallable, FuncArgs, OptionalArg, OptionalOption, PosArgs},
12+
function::{ArgCallable, ArgIntoBool, FuncArgs, OptionalArg, OptionalOption, PosArgs},
1313
identifier,
1414
protocol::{PyIter, PyIterReturn, PyNumber},
1515
stdlib::sys,
1616
types::{Constructor, IterNext, IterNextIterable},
17-
AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, PyWeakRef, VirtualMachine,
17+
AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, PyWeakRef, TryFromObject,
18+
VirtualMachine,
1819
};
1920
use crossbeam_utils::atomic::AtomicCell;
21+
use num_bigint::BigInt;
2022
use num_traits::{Signed, ToPrimitive};
2123
use std::fmt;
2224

@@ -540,7 +542,23 @@ mod decl {
540542
}
541543

542544
#[pyclass(with(IterNext, Constructor), flags(BASETYPE))]
543-
impl PyItertoolsDropwhile {}
545+
impl PyItertoolsDropwhile {
546+
#[pymethod(magic)]
547+
fn reduce(zelf: PyRef<Self>) -> (PyTypeRef, (PyObjectRef, PyIter), BigInt) {
548+
(
549+
zelf.class().clone(),
550+
(zelf.predicate.clone().into(), zelf.iterable.clone()),
551+
(if zelf.start_flag.load() { 1 } else { 0 }).into(),
552+
)
553+
}
554+
#[pymethod(magic)]
555+
fn setstate(zelf: PyRef<Self>, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
556+
if let Ok(obj) = ArgIntoBool::try_from_object(vm, state) {
557+
zelf.start_flag.store(*obj);
558+
}
559+
Ok(())
560+
}
561+
}
544562
impl IterNextIterable for PyItertoolsDropwhile {}
545563
impl IterNext for PyItertoolsDropwhile {
546564
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {

0 commit comments

Comments
 (0)