[pypy-commit] pypy py3k: make sure filter(x, non_callable) raises immediately, not on the first call to __next__

alex_gaynor noreply at buildbot.pypy.org
Thu Dec 22 05:09:16 CET 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: py3k
Changeset: r50821:141ccdc2c4fb
Date: 2011-12-21 22:09 -0600
http://bitbucket.org/pypy/pypy/changeset/141ccdc2c4fb/

Log:	make sure filter(x, non_callable) raises immediately, not on the
	first call to __next__

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,7 +129,10 @@
 or string, return the same type, else return a list."""
     if func is None:
         func = bool
-    for item in seq:
+    return _filter(func, iter(seq))
+
+def _filter(func, iterator):
+    for item in iterator:
         if func(item):
             yield item
 
diff --git a/pypy/module/__builtin__/test/test_filter.py b/pypy/module/__builtin__/test/test_filter.py
--- a/pypy/module/__builtin__/test/test_filter.py
+++ b/pypy/module/__builtin__/test/test_filter.py
@@ -1,11 +1,11 @@
 import autopath
 
-# trivial functions for testing 
+# trivial functions for testing
 
 class AppTestFilter:
     def test_filter_no_arguments(self):
         raises(TypeError, filter)
-      
+
     def test_filter_no_function_no_seq(self):
         raises(TypeError, filter, None)
 
@@ -16,10 +16,14 @@
         raises(TypeError, filter, lambda x: x>3, [1], [2])
 
     def test_filter_no_function_list(self):
-      assert list(filter(None, [1, 2, 3])) == [1, 2, 3]
+        assert list(filter(None, [1, 2, 3])) == [1, 2, 3]
 
     def test_filter_no_function_with_bools(self):
-      assert tuple(filter(None, (True, False, True))) == (True, True)
-      
+        assert tuple(filter(None, (True, False, True))) == (True, True)
+
     def test_filter_list(self):
-      assert list(filter(lambda x: x>3, [1, 2, 3, 4, 5])) == [4, 5]
+        assert list(filter(lambda x: x>3, [1, 2, 3, 4, 5])) == [4, 5]
+
+    def test_filter_non_iterable(self):
+        raises(TypeError, filter, None, 42)
+        raises(TypeError, filter, callable, list)
\ No newline at end of file


More information about the pypy-commit mailing list