Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r48236:bde154c88f96
Date: 2011-10-19 22:31 +0200
http://bitbucket.org/pypy/pypy/changeset/bde154c88f96/
Log: filter() should return an iterable object, not a list or string or
whatever
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
@@ -129,37 +129,6 @@
or string, return the same type, else return a list."""
if func is None:
func = bool
- if isinstance(seq, str):
- return _filter_string(func, seq, str)
- elif isinstance(seq, unicode):
- return _filter_string(func, seq, unicode)
- elif isinstance(seq, tuple):
- return _filter_tuple(func, seq)
- result = []
for item in seq:
if func(item):
- result.append(item)
- return result
-
-def _filter_string(func, string, str_type):
- if func is bool and type(string) is str_type:
- return string
- result = []
- for i in range(len(string)):
- # You must call __getitem__ on the strings, simply iterating doesn't
- # work :/
- item = string[i]
- if func(item):
- if not isinstance(item, str_type):
- raise TypeError("__getitem__ returned a non-string type")
- result.append(item)
- return str_type().join(result)
-
-def _filter_tuple(func, seq):
- result = []
- for i in range(len(seq)):
- # Again, must call __getitem__, at least there are tests.
- item = seq[i]
- if func(item):
- result.append(item)
- return tuple(result)
+ yield item
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
@@ -101,23 +101,17 @@
class AppTestFilter:
def test_None(self):
- assert filter(None, ['a', 'b', 1, 0, None]) == ['a', 'b', 1]
+ assert list(filter(None, ['a', 'b', 1, 0, None])) == ['a', 'b', 1]
def test_return_type(self):
txt = "This is a test text"
- assert filter(None, txt) == txt
+ assert list(filter(None, txt)) == list(txt)
tup = ("a", None, 0, [], 1)
- assert filter(None, tup) == ("a", 1)
+ assert list(filter(None, tup)) == ["a", 1]
def test_function(self):
- assert filter(lambda x: x != "a", "a small text") == " smll text"
- assert filter(lambda x: x < 20, [3, 33, 5, 55]) == [3, 5]
-
- def test_filter_tuple_calls_getitem(self):
- class T(tuple):
- def __getitem__(self, i):
- return i * 10
- assert filter(lambda x: x != 20, T("abcd")) == (0, 10, 30)
+ 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 AppTestRange:
def test_range(self):
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit