Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r58000:0fc04c38ab33
Date: 2012-10-10 23:24 +0200
http://bitbucket.org/pypy/pypy/changeset/0fc04c38ab33/
Log: map(), filter(), zip() now return iterators. Move the code and tests
from the itertool module.
Most of the tests now pass with and without -A.
diff --git a/pypy/module/__builtin__/__init__.py
b/pypy/module/__builtin__/__init__.py
--- a/pypy/module/__builtin__/__init__.py
+++ b/pypy/module/__builtin__/__init__.py
@@ -19,9 +19,6 @@
'any' : 'app_functional.any',
'all' : 'app_functional.all',
'sum' : 'app_functional.sum',
- 'map' : 'app_functional.map',
- 'filter' : 'app_functional.filter',
- 'zip' : 'app_functional.zip',
'vars' : 'app_inspect.vars',
'dir' : 'app_inspect.dir',
@@ -75,6 +72,9 @@
'range' : 'functional.W_Range',
'enumerate' : 'functional.W_Enumerate',
+ 'map' : 'functional.W_Map',
+ 'filter' : 'functional.W_Filter',
+ 'zip' : 'functional.W_Zip',
'min' : 'functional.min',
'max' : 'functional.max',
'reversed' : 'functional.reversed',
diff --git a/pypy/module/__builtin__/app_functional.py
b/pypy/module/__builtin__/app_functional.py
--- a/pypy/module/__builtin__/app_functional.py
+++ b/pypy/module/__builtin__/app_functional.py
@@ -44,55 +44,6 @@
last = last + x
return last
-def map(func, *collections):
- """map(function, sequence[, sequence, ...]) -> list
-
-Return a list of the results of applying the function to the items of
-the argument sequence(s). If more than one sequence is given, the
-function is called with an argument list consisting of the corresponding
-item of each sequence, substituting None for missing values when not all
-sequences have the same length. If the function is None, return a list of
-the items of the sequence (or a list of tuples if more than one sequence)."""
- if not collections:
- raise TypeError("map() requires at least two arguments")
- num_collections = len(collections)
- none_func = func is None
- if num_collections == 1:
- if none_func:
- return list(collections[0])
- else:
- # Special case for the really common case of a single collection,
- # this can be eliminated if we could unroll that loop that creates
- # `args` based on whether or not len(collections) was constant
- result = []
- for item in collections[0]:
- result.append(func(item))
- return result
- result = []
- # Pair of (iterator, has_finished)
- iterators = [(iter(seq), False) for seq in collections]
- while True:
- cont = False
- args = []
- for idx, (iterator, has_finished) in enumerate(iterators):
- val = None
- if not has_finished:
- try:
- val = next(iterator)
- except StopIteration:
- iterators[idx] = (None, True)
- else:
- cont = True
- args.append(val)
- args = tuple(args)
- if cont:
- if none_func:
- result.append(args)
- else:
- result.append(func(*args))
- else:
- return result
-
def filter(func, seq):
"""filter(function or None, sequence) -> list, tuple, or string
@@ -107,20 +58,3 @@
for item in iterator:
if func(item):
yield item
-
-def zip(*sequences):
- """zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
-
-Return a list of tuples, where each tuple contains the i-th element
-from each of the argument sequences. The returned list is truncated
-in length to the length of the shortest argument sequence."""
- if not sequences:
- return []
- result = []
- iterators = [iter(seq) for seq in sequences]
- while True:
- try:
- items = [next(it) for it in iterators]
- except StopIteration:
- return result
- result.append(tuple(items))
diff --git a/pypy/module/__builtin__/functional.py
b/pypy/module/__builtin__/functional.py
--- a/pypy/module/__builtin__/functional.py
+++ b/pypy/module/__builtin__/functional.py
@@ -471,3 +471,142 @@
__next__ = interp2app(W_RangeIterator.descr_next),
__reduce__ = interp2app(W_RangeIterator.descr_reduce),
)
+
+
+class W_Map(Wrappable):
+ _error_name = "map"
+ _immutable_fields_ = ["w_fun", "iterators_w"]
+
+ def __init__(self, space, w_fun, args_w):
+ self.space = space
+ self.w_fun = w_fun
+
+ iterators_w = []
+ i = 0
+ for iterable_w in args_w:
+ try:
+ iterator_w = space.iter(iterable_w)
+ except OperationError, e:
+ if e.match(self.space, self.space.w_TypeError):
+ raise OperationError(space.w_TypeError,
space.wrap(self._error_name + " argument #" + str(i + 1) + " must support
iteration"))
+ else:
+ raise
+ else:
+ iterators_w.append(iterator_w)
+
+ i += 1
+
+ self.iterators_w = iterators_w
+
+ def iter_w(self):
+ return self.space.wrap(self)
+
+ def next_w(self):
+ # common case: 1 or 2 arguments
+ iterators_w = self.iterators_w
+ length = len(iterators_w)
+ if length == 1:
+ objects = [self.space.next(iterators_w[0])]
+ elif length == 2:
+ objects = [self.space.next(iterators_w[0]),
+ self.space.next(iterators_w[1])]
+ else:
+ objects = self._get_objects()
+ w_objects = self.space.newtuple(objects)
+ if self.w_fun is None:
+ return w_objects
+ else:
+ return self.space.call(self.w_fun, w_objects)
+
+ def _get_objects(self):
+ # the loop is out of the way of the JIT
+ return [self.space.next(w_elem) for w_elem in self.iterators_w]
+
+
+def W_Map___new__(space, w_subtype, w_fun, args_w):
+ if len(args_w) == 0:
+ raise OperationError(space.w_TypeError,
+ space.wrap("map() must have at least two arguments"))
+ r = space.allocate_instance(W_Map, w_subtype)
+ r.__init__(space, w_fun, args_w)
+ return space.wrap(r)
+
+W_Map.typedef = TypeDef(
+ 'map',
+ __new__ = interp2app(W_Map___new__),
+ __iter__ = interp2app(W_Map.iter_w),
+ __next__ = interp2app(W_Map.next_w),
+ __doc__ = """\
+Make an iterator that computes the function using arguments from
+each of the iterables. Stops when the shortest iterable is exhausted.""")
+
+class W_Filter(Wrappable):
+ reverse = False
+
+ def __init__(self, space, w_predicate, w_iterable):
+ self.space = space
+ if space.is_w(w_predicate, space.w_None):
+ self.no_predicate = True
+ else:
+ self.no_predicate = False
+ self.w_predicate = w_predicate
+ self.iterable = space.iter(w_iterable)
+
+ def iter_w(self):
+ return self.space.wrap(self)
+
+ def next_w(self):
+ while True:
+ w_obj = self.space.next(self.iterable) # may raise w_StopIteration
+ if self.no_predicate:
+ pred = self.space.is_true(w_obj)
+ else:
+ w_pred = self.space.call_function(self.w_predicate, w_obj)
+ pred = self.space.is_true(w_pred)
+ if pred ^ self.reverse:
+ return w_obj
+
+
+def W_Filter___new__(space, w_subtype, w_predicate, w_iterable):
+ r = space.allocate_instance(W_Filter, w_subtype)
+ r.__init__(space, w_predicate, w_iterable)
+ return space.wrap(r)
+
+W_Filter.typedef = TypeDef(
+ 'filter',
+ __new__ = interp2app(W_Filter___new__),
+ __iter__ = interp2app(W_Filter.iter_w),
+ __next__ = interp2app(W_Filter.next_w),
+ __doc__ = """\
+Return an iterator yielding those items of iterable for which function(item)
+is true. If function is None, return the items that are true.""")
+
+
+class W_Zip(W_Map):
+ _error_name = "zip"
+
+ def next_w(self):
+ # argh. zip(*args) is almost like map(None, *args) except
+ # that the former needs a special case for len(args)==0
+ # while the latter just raises a TypeError in this situation.
+ if len(self.iterators_w) == 0:
+ raise OperationError(self.space.w_StopIteration, self.space.w_None)
+ return W_Map.next_w(self)
+
+def W_Zip___new__(space, w_subtype, args_w):
+ r = space.allocate_instance(W_Zip, w_subtype)
+ r.__init__(space, None, args_w)
+ return space.wrap(r)
+
+W_Zip.typedef = TypeDef(
+ 'zip',
+ __new__ = interp2app(W_Zip___new__),
+ __iter__ = interp2app(W_Zip.iter_w),
+ __next__ = interp2app(W_Zip.next_w),
+ __doc__ = """\
+Return a zip object whose .__next__() method returns a tuple where
+the i-th element comes from the i-th iterable argument. The .__next__()
+method continues until the shortest iterable in the argument sequence
+is exhausted and then it raises StopIteration.""")
+
+
diff --git a/pypy/module/__builtin__/test/test_functional.py
b/pypy/module/__builtin__/test/test_functional.py
--- a/pypy/module/__builtin__/test/test_functional.py
+++ b/pypy/module/__builtin__/test/test_functional.py
@@ -69,6 +69,45 @@
def test_repr(self):
assert repr(map(1, [2])).startswith('<map object ')
+class AppTestMap2:
+
+ def test_map(self):
+ obj_list = [object(), object(), object()]
+ it = map(lambda *x:x, obj_list)
+ for x in obj_list:
+ assert next(it) == (x, )
+ raises(StopIteration, next, it)
+
+ it = map(lambda *x:x, [1, 2, 3], [4], [5, 6])
+ assert next(it) == (1, 4, 5)
+ raises(StopIteration, next, it)
+
+ it = map(lambda *x:x, [], [], [1], [])
+ raises(StopIteration, next, it)
+
+ it = map(str, [0, 1, 0, 1])
+ for x in ['0', '1', '0', '1']:
+ assert next(it) == x
+ raises(StopIteration, next, it)
+
+ import operator
+ it = map(operator.add, [1, 2, 3], [4, 5, 6])
+ for x in [5, 7, 9]:
+ assert next(it) == x
+ raises(StopIteration, next, it)
+
+ def test_map_wrongargs(self):
+ # Duplicate python 2.4 behaviour for invalid arguments
+ it = map(0, [])
+ raises(StopIteration, next, it)
+ it = map(0, [0])
+ raises(TypeError, next, it)
+ raises(TypeError, map, None, 0)
+
+ raises(TypeError, map, None)
+ raises(TypeError, map, bool)
+ raises(TypeError, map, 42)
+
class AppTestZip:
def test_one_list(self):
assert list(zip([1,2,3])) == [(1,), (2,), (3,)]
@@ -93,6 +132,33 @@
assert list(filter(lambda x: x != "a", "a small text")) == list(" smll
text")
assert list(filter(lambda x: x < 20, [3, 33, 5, 55])) == [3, 5]
+class AppTestFilter2:
+ def test_filter(self):
+ it = filter(None, [])
+ raises(StopIteration, next, it)
+
+ it = filter(None, [1, 0, 2, 3, 0])
+ for x in [1, 2, 3]:
+ assert next(it) == x
+ raises(StopIteration, next, it)
+
+ def is_odd(arg):
+ return (arg % 2 == 1)
+
+ it = filter(is_odd, [1, 2, 3, 4, 5, 6])
+ for x in [1, 3, 5]:
+ assert next(it) == x
+ raises(StopIteration, next, it)
+
+ def test_filter_wrongargs(self):
+ import itertools
+
+ it = filter(0, [1])
+ raises(TypeError, next, it)
+
+ raises(TypeError, filter, bool, None)
+
+
class AppTestRange:
def test_range(self):
x = range(2, 9, 3)
diff --git a/pypy/module/__builtin__/test/test_zip.py
b/pypy/module/__builtin__/test/test_zip.py
--- a/pypy/module/__builtin__/test/test_zip.py
+++ b/pypy/module/__builtin__/test/test_zip.py
@@ -7,32 +7,79 @@
if sys.version_info < (2,4):
# Test 2.3 behaviour
raises(TypeError, zip)
- return
- # Test 2.4 behaviour
- assert zip() == []
- assert zip(*[]) == []
+ else:
+ # Test 2.4 behaviour
+ assert list(zip()) == []
+ assert list(zip(*[])) == []
def test_one_list(self):
- assert zip([1, 2, 3]) == [(1,), (2,), (3,)]
+ assert list(zip([1, 2, 3])) == [(1,), (2,), (3,)]
def test_three_lists_same_size(self):
- assert zip([1, 2, 3], [3, 4, 5], [6, 7, 8]) == (
+ assert list(zip([1, 2, 3], [3, 4, 5], [6, 7, 8])) == (
[(1, 3, 6), (2, 4, 7), (3, 5, 8)])
def test_three_lists_different_sizes(self):
- assert zip([1, 2], [3, 4, 5, 6], [6, 7, 8]) == (
+ assert list(zip([1, 2], [3, 4, 5, 6], [6, 7, 8])) == (
[(1, 3, 6), (2, 4, 7)])
def test_tuples(self):
- assert zip((1, 2, 3)) == [(1,), (2,), (3,)]
+ assert list(zip((1, 2, 3))) == [(1,), (2,), (3,)]
def test_string(self):
- assert zip('hello') == [('h',), ('e',), ('l',), ('l',), ('o',)]
+ assert list(zip('hello')) == [('h',), ('e',), ('l',), ('l',), ('o',)]
def test_strings(self):
- assert zip('hello', 'bye') == (
+ assert list(zip('hello', 'bye')) == (
[('h', 'b'), ('e', 'y'), ('l', 'e')])
def test_mixed_types(self):
- assert zip('hello', [1,2,3,4], (7,8,9,10)) == (
+ assert list(zip('hello', [1,2,3,4], (7,8,9,10))) == (
[('h', 1, 7), ('e', 2, 8), ('l', 3, 9), ('l', 4, 10)])
+
+class AppTestZip2:
+ def test_zip(self):
+ it = zip()
+ raises(StopIteration, next, it)
+
+ obj_list = [object(), object(), object()]
+ it = zip(obj_list)
+ for x in obj_list:
+ assert next(it) == (x, )
+ raises(StopIteration, next, it)
+
+ it = zip([1, 2, 3], [4], [5, 6])
+ assert next(it) == (1, 4, 5)
+ raises(StopIteration, next, it)
+
+ it = zip([], [], [1], [])
+ raises(StopIteration, next, it)
+
+ # Up to one additional item may be consumed per iterable, as per
python docs
+ it1 = iter([1, 2, 3, 4, 5, 6])
+ it2 = iter([5, 6])
+ it = zip(it1, it2)
+ for x in [(1, 5), (2, 6)]:
+ assert next(it) == x
+ raises(StopIteration, next, it)
+ assert next(it1) in [3, 4]
+ #---does not work in CPython 2.5
+ #raises(StopIteration, it.next)
+ #assert it1.next() in [4, 5]
+
+ def test_zip_wrongargs(self):
+ import re
+
+ # Duplicate python 2.4 behaviour for invalid arguments
+ raises(TypeError, zip, None, 0)
+
+ # The error message should indicate which argument was dodgy
+ for x in range(10):
+ args = [()] * x + [None] + [()] * (9 - x)
+ try:
+ zip(*args)
+ except TypeError as e:
+ assert str(e).find("#" + str(x + 1) + " ") >= 0
+ else:
+ fail("TypeError expected")
+
diff --git a/pypy/module/itertools/__init__.py
b/pypy/module/itertools/__init__.py
--- a/pypy/module/itertools/__init__.py
+++ b/pypy/module/itertools/__init__.py
@@ -14,7 +14,6 @@
ifilterfalse(pred, seq) --> elements of seq where pred(elem) is False
islice(seq, [start,] stop [, step]) --> elements from
seq[start:stop:step]
- imap(fun, p, q, ...) --> fun(p0, q0), fun(p1, q1), ...
starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...
tee(it, n=2) --> (it1, it2 , ... itn) splits one iterator into n
chain(p, q, ...) --> p0, p1, ... plast, q0, q1, ...
@@ -32,9 +31,7 @@
'cycle' : 'interp_itertools.W_Cycle',
'dropwhile' : 'interp_itertools.W_DropWhile',
'groupby' : 'interp_itertools.W_GroupBy',
- 'ifilter' : 'interp_itertools.W_IFilter',
- 'ifilterfalse' : 'interp_itertools.W_IFilterFalse',
- 'imap' : 'interp_itertools.W_IMap',
+ 'filterfalse' : 'interp_itertools.W_FilterFalse',
'islice' : 'interp_itertools.W_ISlice',
'permutations' : 'interp_itertools.W_Permutations',
'product' : 'interp_itertools.W_Product',
diff --git a/pypy/module/itertools/interp_itertools.py
b/pypy/module/itertools/interp_itertools.py
--- a/pypy/module/itertools/interp_itertools.py
+++ b/pypy/module/itertools/interp_itertools.py
@@ -3,6 +3,8 @@
from pypy.interpreter.typedef import TypeDef, make_weakref_descr
from pypy.interpreter.gateway import interp2app, unwrap_spec
+from pypy.module.__builtin__.functional import W_Filter, W_Map
+
class W_Count(Wrappable):
def __init__(self, space, w_firstval, w_step):
@@ -240,81 +242,27 @@
yield x
""")
-class _IFilterBase(Wrappable):
+class W_FilterFalse(W_Filter):
+ reverse = True
- def __init__(self, space, w_predicate, w_iterable):
- self.space = space
- if space.is_w(w_predicate, space.w_None):
- self.no_predicate = True
- else:
- self.no_predicate = False
- self.w_predicate = w_predicate
- self.iterable = space.iter(w_iterable)
-
- def iter_w(self):
- return self.space.wrap(self)
-
- def next_w(self):
- while True:
- w_obj = self.space.next(self.iterable) # may raise w_StopIteration
- if self.no_predicate:
- pred = self.space.is_true(w_obj)
- else:
- w_pred = self.space.call_function(self.w_predicate, w_obj)
- pred = self.space.is_true(w_pred)
- if pred ^ self.reverse:
- return w_obj
-
-
-class W_IFilter(_IFilterBase):
- reverse = False
-
-def W_IFilter___new__(space, w_subtype, w_predicate, w_iterable):
- r = space.allocate_instance(W_IFilter, w_subtype)
+def W_FilterFalse___new__(space, w_subtype, w_predicate, w_iterable):
+ r = space.allocate_instance(W_FilterFalse, w_subtype)
r.__init__(space, w_predicate, w_iterable)
return space.wrap(r)
-W_IFilter.typedef = TypeDef(
- 'ifilter',
- __module__ = 'itertools',
- __new__ = interp2app(W_IFilter___new__),
- __iter__ = interp2app(W_IFilter.iter_w),
- __next__ = interp2app(W_IFilter.next_w),
- __doc__ = """Make an iterator that filters elements from iterable
returning
- only those for which the predicate is True. If predicate is
- None, return the items that are true.
-
- Equivalent to :
-
- def ifilter:
- if predicate is None:
- predicate = bool
- for x in iterable:
- if predicate(x):
- yield x
- """)
-
-class W_IFilterFalse(_IFilterBase):
- reverse = True
-
-def W_IFilterFalse___new__(space, w_subtype, w_predicate, w_iterable):
- r = space.allocate_instance(W_IFilterFalse, w_subtype)
- r.__init__(space, w_predicate, w_iterable)
- return space.wrap(r)
-
-W_IFilterFalse.typedef = TypeDef(
+W_FilterFalse.typedef = TypeDef(
'ifilterfalse',
__module__ = 'itertools',
- __new__ = interp2app(W_IFilterFalse___new__),
- __iter__ = interp2app(W_IFilterFalse.iter_w),
- __next__ = interp2app(W_IFilterFalse.next_w),
+ __new__ = interp2app(W_FilterFalse___new__),
+ __iter__ = interp2app(W_FilterFalse.iter_w),
+ __next__ = interp2app(W_FilterFalse.next_w),
__doc__ = """Make an iterator that filters elements from iterable
returning
only those for which the predicate is False. If predicate is
None, return the items that are false.
Equivalent to :
- def ifilterfalse(predicate, iterable):
+ def filterfalse(predicate, iterable):
if predicate is None:
predicate = bool
for x in iterable:
@@ -489,97 +437,8 @@
yield element
""")
-class W_IMap(Wrappable):
- _error_name = "imap"
- _immutable_fields_ = ["w_fun", "iterators_w"]
- def __init__(self, space, w_fun, args_w):
- self.space = space
- if self.space.is_w(w_fun, space.w_None):
- self.w_fun = None
- else:
- self.w_fun = w_fun
-
- iterators_w = []
- i = 0
- for iterable_w in args_w:
- try:
- iterator_w = space.iter(iterable_w)
- except OperationError, e:
- if e.match(self.space, self.space.w_TypeError):
- raise OperationError(space.w_TypeError,
space.wrap(self._error_name + " argument #" + str(i + 1) + " must support
iteration"))
- else:
- raise
- else:
- iterators_w.append(iterator_w)
-
- i += 1
-
- self.iterators_w = iterators_w
-
- def iter_w(self):
- return self.space.wrap(self)
-
- def next_w(self):
- # common case: 1 or 2 arguments
- iterators_w = self.iterators_w
- length = len(iterators_w)
- if length == 1:
- objects = [self.space.next(iterators_w[0])]
- elif length == 2:
- objects = [self.space.next(iterators_w[0]),
- self.space.next(iterators_w[1])]
- else:
- objects = self._get_objects()
- w_objects = self.space.newtuple(objects)
- if self.w_fun is None:
- return w_objects
- else:
- return self.space.call(self.w_fun, w_objects)
-
- def _get_objects(self):
- # the loop is out of the way of the JIT
- return [self.space.next(w_elem) for w_elem in self.iterators_w]
-
-
-def W_IMap___new__(space, w_subtype, w_fun, args_w):
- if len(args_w) == 0:
- raise OperationError(space.w_TypeError,
- space.wrap("imap() must have at least two arguments"))
- r = space.allocate_instance(W_IMap, w_subtype)
- r.__init__(space, w_fun, args_w)
- return space.wrap(r)
-
-W_IMap.typedef = TypeDef(
- 'imap',
- __module__ = 'itertools',
- __new__ = interp2app(W_IMap___new__),
- __iter__ = interp2app(W_IMap.iter_w),
- __next__ = interp2app(W_IMap.next_w),
- __doc__ = """Make an iterator that computes the function using
arguments
- from each of the iterables. If function is set to None, then
- imap() returns the arguments as a tuple. Like map() but stops
- when the shortest iterable is exhausted instead of filling in
- None for shorter iterables. The reason for the difference is that
- infinite iterator arguments are typically an error for map()
- (because the output is fully evaluated) but represent a common
- and useful way of supplying arguments to imap().
-
- Equivalent to :
-
- def imap(function, *iterables):
- iterables = map(iter, iterables)
- while True:
- args = [i.next() for i in iterables]
- if function is None:
- yield tuple(args)
- else:
- yield function(*args)
-
- """)
-
-
-class W_ZipLongest(W_IMap):
+class W_ZipLongest(W_Map):
_error_name = "zip_longest"
def next_w(self):
diff --git a/pypy/module/itertools/test/test_itertools.py
b/pypy/module/itertools/test/test_itertools.py
--- a/pypy/module/itertools/test/test_itertools.py
+++ b/pypy/module/itertools/test/test_itertools.py
@@ -71,7 +71,7 @@
import itertools
import sys
- raises(OverflowError, itertools.repeat, None, sys.maxint + 1)
+ raises(OverflowError, itertools.repeat, None, sys.maxsize + 1)
def test_repeat_repr(self):
import itertools
@@ -144,40 +144,13 @@
raises(TypeError, itertools.dropwhile, bool, None)
- def test_ifilter(self):
+ def test_filterfalse(self):
import itertools
- it = itertools.ifilter(None, [])
+ it = itertools.filterfalse(None, [])
raises(StopIteration, next, it)
- it = itertools.ifilter(None, [1, 0, 2, 3, 0])
- for x in [1, 2, 3]:
- assert next(it) == x
- raises(StopIteration, next, it)
-
- def is_odd(arg):
- return (arg % 2 == 1)
-
- it = itertools.ifilter(is_odd, [1, 2, 3, 4, 5, 6])
- for x in [1, 3, 5]:
- assert next(it) == x
- raises(StopIteration, next, it)
-
- def test_ifilter_wrongargs(self):
- import itertools
-
- it = itertools.ifilter(0, [1])
- raises(TypeError, next, it)
-
- raises(TypeError, itertools.ifilter, bool, None)
-
- def test_ifilterfalse(self):
- import itertools
-
- it = itertools.ifilterfalse(None, [])
- raises(StopIteration, next, it)
-
- it = itertools.ifilterfalse(None, [1, 0, 2, 3, 0])
+ it = itertools.filterfalse(None, [1, 0, 2, 3, 0])
for x in [0, 0]:
assert next(it) == x
raises(StopIteration, next, it)
@@ -185,18 +158,18 @@
def is_odd(arg):
return (arg % 2 == 1)
- it = itertools.ifilterfalse(is_odd, [1, 2, 3, 4, 5, 6])
+ it = itertools.filterfalse(is_odd, [1, 2, 3, 4, 5, 6])
for x in [2, 4, 6]:
assert next(it) == x
raises(StopIteration, next, it)
- def test_ifilterfalse_wrongargs(self):
+ def test_filterfalse_wrongargs(self):
import itertools
- it = itertools.ifilterfalse(0, [1])
+ it = itertools.filterfalse(0, [1])
raises(TypeError, next, it)
- raises(TypeError, itertools.ifilterfalse, bool, None)
+ raises(TypeError, itertools.filterfalse, bool, None)
def test_islice(self):
import itertools
@@ -284,7 +257,7 @@
import itertools
import sys
raises((OverflowError, ValueError), # ValueError on top of CPython
- itertools.islice, [], sys.maxint + 1)
+ itertools.islice, [], sys.maxsize + 1)
def test_islice_wrongargs(self):
import itertools
@@ -322,47 +295,6 @@
assert next(it) == 1
raises(StopIteration, next, it)
- def test_imap(self):
- import itertools
-
- obj_list = [object(), object(), object()]
- it = itertools.imap(None, obj_list)
- for x in obj_list:
- assert next(it) == (x, )
- raises(StopIteration, next, it)
-
- it = itertools.imap(None, [1, 2, 3], [4], [5, 6])
- assert next(it) == (1, 4, 5)
- raises(StopIteration, next, it)
-
- it = itertools.imap(None, [], [], [1], [])
- raises(StopIteration, next, it)
-
- it = itertools.imap(str, [0, 1, 0, 1])
- for x in ['0', '1', '0', '1']:
- assert next(it) == x
- raises(StopIteration, next, it)
-
- import operator
- it = itertools.imap(operator.add, [1, 2, 3], [4, 5, 6])
- for x in [5, 7, 9]:
- assert next(it) == x
- raises(StopIteration, next, it)
-
- def test_imap_wrongargs(self):
- import itertools
-
- # Duplicate python 2.4 behaviour for invalid arguments
- it = itertools.imap(0, [])
- raises(StopIteration, next, it)
- it = itertools.imap(0, [0])
- raises(TypeError, next, it)
- raises(TypeError, itertools.imap, None, 0)
-
- raises(TypeError, itertools.imap, None)
- raises(TypeError, itertools.imap, bool)
- raises(TypeError, itertools.imap, 42)
-
def test_cycle(self):
import itertools
@@ -569,9 +501,7 @@
itertools.cycle([]),
itertools.dropwhile(bool, []),
itertools.groupby([]),
- itertools.ifilter(None, []),
- itertools.ifilterfalse(None, []),
- itertools.imap(None, []),
+ itertools.filterfalse(None, []),
itertools.islice([], 0),
itertools.repeat(None),
itertools.starmap(bool, []),
@@ -596,9 +526,7 @@
itertools.cycle,
itertools.dropwhile,
itertools.groupby,
- itertools.ifilter,
- itertools.ifilterfalse,
- itertools.imap,
+ itertools.filterfalse,
itertools.islice,
itertools.repeat,
itertools.starmap,
@@ -637,26 +565,26 @@
def test_count_overflow(self):
import itertools, sys
- it = itertools.count(sys.maxint - 1)
- assert next(it) == sys.maxint - 1
- assert next(it) == sys.maxint
- assert next(it) == sys.maxint + 1
- it = itertools.count(sys.maxint + 1)
- assert next(it) == sys.maxint + 1
- assert next(it) == sys.maxint + 2
- it = itertools.count(-sys.maxint-2)
- assert next(it) == -sys.maxint - 2
- assert next(it) == -sys.maxint - 1
- assert next(it) == -sys.maxint
- assert next(it) == -sys.maxint + 1
- it = itertools.count(0, sys.maxint)
- assert next(it) == sys.maxint * 0
- assert next(it) == sys.maxint * 1
- assert next(it) == sys.maxint * 2
- it = itertools.count(0, sys.maxint + 1)
- assert next(it) == (sys.maxint + 1) * 0
- assert next(it) == (sys.maxint + 1) * 1
- assert next(it) == (sys.maxint + 1) * 2
+ it = itertools.count(sys.maxsize - 1)
+ assert next(it) == sys.maxsize - 1
+ assert next(it) == sys.maxsize
+ assert next(it) == sys.maxsize + 1
+ it = itertools.count(sys.maxsize + 1)
+ assert next(it) == sys.maxsize + 1
+ assert next(it) == sys.maxsize + 2
+ it = itertools.count(-sys.maxsize-2)
+ assert next(it) == -sys.maxsize - 2
+ assert next(it) == -sys.maxsize - 1
+ assert next(it) == -sys.maxsize
+ assert next(it) == -sys.maxsize + 1
+ it = itertools.count(0, sys.maxsize)
+ assert next(it) == sys.maxsize * 0
+ assert next(it) == sys.maxsize * 1
+ assert next(it) == sys.maxsize * 2
+ it = itertools.count(0, sys.maxsize + 1)
+ assert next(it) == (sys.maxsize + 1) * 0
+ assert next(it) == (sys.maxsize + 1) * 1
+ assert next(it) == (sys.maxsize + 1) * 2
def test_chain_fromiterable(self):
import itertools
@@ -695,14 +623,14 @@
# take 3 from infinite input
assert (list(islice(zip_longest('abcdef', count()),3)) ==
- zip('abcdef', range(3)))
+ list(zip('abcdef', range(3))))
- assert list(zip_longest()) == zip()
- assert list(zip_longest([])) == zip([])
- assert list(zip_longest('abcdef')) == zip('abcdef')
+ assert list(zip_longest()) == list(zip())
+ assert list(zip_longest([])) == list(zip([]))
+ assert list(zip_longest('abcdef')) == list(zip('abcdef'))
assert (list(zip_longest('abc', 'defg', **{})) ==
- zip(list('abc') + [None], 'defg')) # empty keyword dict
+ list(zip(list('abc') + [None], 'defg'))) # empty keyword dict
raises(TypeError, zip_longest, 3)
raises(TypeError, zip_longest, range(3), 3)
@@ -945,17 +873,13 @@
assert type(A(lambda x: True, [])) is A
class A(itertools.dropwhile): pass
assert type(A(lambda x: True, [])) is A
- class A(itertools.ifilter): pass
- assert type(A(lambda x: True, [])) is A
- class A(itertools.ifilterfalse): pass
+ class A(itertools.filterfalse): pass
assert type(A(lambda x: True, [])) is A
class A(itertools.islice): pass
assert type(A([], 0)) is A
class A(itertools.chain): pass
assert type(A([], [])) is A
assert type(A.from_iterable([])) is A
- class A(itertools.imap): pass
- assert type(A(lambda: 5, [])) is A
class A(itertools.zip_longest): pass
assert type(A([], [])) is A
class A(itertools.cycle): pass
@@ -975,8 +899,8 @@
def test_copy_pickle(self):
import itertools, copy, pickle, sys
- for value in [42, -sys.maxint*99]:
- for step in [1, sys.maxint*42, 5.5]:
+ for value in [42, -sys.maxsize*99]:
+ for step in [1, sys.maxsize*42, 5.5]:
expected = [value, value+step, value+2*step]
c = itertools.count(value, step)
assert list(itertools.islice(c, 3)) == expected
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit