[pypy-svn] r56425 - in pypy/dist/pypy/module/itertools: . test
jlg at codespeak.net
jlg at codespeak.net
Thu Jul 10 19:14:45 CEST 2008
Author: jlg
Date: Thu Jul 10 19:14:43 2008
New Revision: 56425
Modified:
pypy/dist/pypy/module/itertools/__init__.py
pypy/dist/pypy/module/itertools/interp_itertools.py
pypy/dist/pypy/module/itertools/test/test_itertools.py
Log:
(adurdin, jlg) interp_itertools - islice partialy implemented
Modified: pypy/dist/pypy/module/itertools/__init__.py
==============================================================================
--- pypy/dist/pypy/module/itertools/__init__.py (original)
+++ pypy/dist/pypy/module/itertools/__init__.py Thu Jul 10 19:14:43 2008
@@ -9,6 +9,7 @@
'dropwhile' : 'interp_itertools.W_DropWhile',
'ifilter' : 'interp_itertools.W_IFilter',
'ifilterfalse' : 'interp_itertools.W_IFilterFalse',
+ 'islice' : 'interp_itertools.W_ISlice',
'repeat' : 'interp_itertools.W_Repeat',
'takewhile' : 'interp_itertools.W_TakeWhile',
}
Modified: pypy/dist/pypy/module/itertools/interp_itertools.py
==============================================================================
--- pypy/dist/pypy/module/itertools/interp_itertools.py (original)
+++ pypy/dist/pypy/module/itertools/interp_itertools.py Thu Jul 10 19:14:43 2008
@@ -276,3 +276,60 @@
yield x
""")
+class W_ISlice (Wrappable):
+
+ def __init__(self, space, w_iterable, start, stop, step):
+ self.iterable = space.iter(w_iterable)
+ self.space = space
+ if stop == -1:
+ stop = start
+ start = 0
+
+ if step == -1:
+ step = 1
+
+ self.start = start
+ self.stop = stop
+ self.step = step
+
+ def iter_w(self):
+ return self.space.wrap(self)
+
+ def next_w(self):
+ if self.stop <= 0:
+ raise OperationError(self.space.w_StopIteration, self.space.w_None)
+
+ if self.start >= 0:
+ skip = self.start
+ self.start = -1
+ else:
+ skip = self.step - 1
+
+ try:
+ while skip > 0:
+ self.space.next(self.iterable)
+ skip -= 1
+ self.stop -= 1
+
+ except StopIteration:
+ raise OperationError(self.space.w_StopIteration, self.space.w_None)
+
+ try:
+ w_obj = self.space.next(self.iterable)
+ except StopIteration:
+ raise OperationError(self.space.w_StopIteration, self.space.w_None)
+
+ self.stop -= 1
+ return w_obj
+
+def W_ISlice___new__(space, w_subtype, w_iterable, start, stop, step):
+ # TODO varible arguments number not implemented (optional start, step)
+ return space.wrap(W_ISlice(space, w_iterable, start, stop, step))
+
+W_ISlice.typedef = TypeDef(
+ 'islice',
+ __new__ = interp2app(W_ISlice___new__, unwrap_spec=[ObjSpace, W_Root, W_Root, int, int, int]),
+ __iter__ = interp2app(W_ISlice.iter_w, unwrap_spec=['self']),
+ next = interp2app(W_ISlice.next_w, unwrap_spec=['self']),
+ __doc__ = "")
+
Modified: pypy/dist/pypy/module/itertools/test/test_itertools.py
==============================================================================
--- pypy/dist/pypy/module/itertools/test/test_itertools.py (original)
+++ pypy/dist/pypy/module/itertools/test/test_itertools.py Thu Jul 10 19:14:43 2008
@@ -14,6 +14,7 @@
itertools.dropwhile(bool, []),
itertools.ifilter(None, []),
itertools.ifilterfalse(None, []),
+ itertools.islice([], 0, -1, -1),
]
for it in iterables:
@@ -185,3 +186,44 @@
raises(TypeError, itertools.ifilterfalse, bool, None)
+ def test_islice(self):
+ import itertools
+
+ it = itertools.islice([], 0, -1, -1)
+ raises(StopIteration, it.next)
+
+ it = itertools.islice([1, 2, 3], 0, -1, -1)
+ raises(StopIteration, it.next)
+
+ it = itertools.islice([1, 2, 3, 4, 5], 3, -1, -1)
+ for x in [1, 2, 3]:
+ assert it.next() == x
+ raises(StopIteration, it.next)
+
+ it = itertools.islice([1, 2, 3, 4, 5], 3, -1, -1)
+ for x in [1, 2, 3]:
+ assert it.next() == x
+ raises(StopIteration, it.next)
+
+ it = itertools.islice([1, 2, 3, 4, 5], 1, 3, -1)
+ for x in [2, 3]:
+ assert it.next() == x
+ raises(StopIteration, it.next)
+
+ it = itertools.islice([1, 2, 3, 4, 5], 0, 3, 2)
+ for x in [1, 3]:
+ assert it.next() == x
+ raises(StopIteration, it.next)
+
+ def test_islice_overflow(self):
+ import itertools
+ import sys
+
+ raises(OverflowError, itertools.islice, [], sys.maxint + 1, -1, -1)
+
+ def test_islice_wrongargs(self):
+ import itertools
+
+ raises(TypeError, itertools.islice, [], None, -1, -1)
+ raises(TypeError, itertools.islice, None, 0, -1, -1)
+
More information about the Pypy-commit
mailing list