[pypy-svn] r23703 - in pypy/dist/pypy: module/__builtin__ module/__builtin__/test translator

blais at codespeak.net blais at codespeak.net
Mon Feb 27 20:03:38 CET 2006


Author: blais
Date: Mon Feb 27 20:03:32 2006
New Revision: 23703

Modified:
   pypy/dist/pypy/module/__builtin__/__init__.py
   pypy/dist/pypy/module/__builtin__/app_functional.py
   pypy/dist/pypy/module/__builtin__/test/test_functional.py
   pypy/dist/pypy/translator/interactive.py
Log:
(blais, stuart)

Implemented the 2.5 builtin functions all() and any().
(Implemented at app level, near min() and max())

Copied tests over from Python 2.5.



Modified: pypy/dist/pypy/module/__builtin__/__init__.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__/__init__.py	(original)
+++ pypy/dist/pypy/module/__builtin__/__init__.py	Mon Feb 27 20:03:32 2006
@@ -27,6 +27,8 @@
         # is still needed and should stay where it is.
         'min'           : 'app_functional.min',
         'max'           : 'app_functional.max',
+        'all'           : 'app_functional.all',
+        'any'           : 'app_functional.any',
         'enumerate'     : 'app_functional.enumerate',
         'xrange'        : 'app_functional.xrange',
         'sorted'        : 'app_functional.sorted',

Modified: pypy/dist/pypy/module/__builtin__/app_functional.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__/app_functional.py	(original)
+++ pypy/dist/pypy/module/__builtin__/app_functional.py	Mon Feb 27 20:03:32 2006
@@ -258,6 +258,26 @@
     def __iter__(self):
         return self
 
+# ____________________________________________________________
+
+def all( it ):
+    """
+    Implementation of the all() builtin function from 2.5.
+    """
+    for i in it:
+        if not i:
+            return False
+    return True
+    
+def any( it ):
+    """
+    Implementation of the all() builtin function from 2.5.
+    """
+    for i in it:
+        if i:
+            return True
+    return False
+
 
 # ____________________________________________________________
 

Modified: pypy/dist/pypy/module/__builtin__/test/test_functional.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__/test/test_functional.py	(original)
+++ pypy/dist/pypy/module/__builtin__/test/test_functional.py	Mon Feb 27 20:03:32 2006
@@ -7,7 +7,7 @@
       assert map(lambda x: x+2, [1, 2, 3, 4]) == [3, 4, 5, 6]
 
    def test_trivial_map_two_seq(self):
-      assert map(lambda x,y: x+y, 
+      assert map(lambda x,y: x+y,
                            [1, 2, 3, 4],[1, 2, 3, 4]) == (
                        [2, 4, 6, 8])
 
@@ -16,23 +16,23 @@
 
    def test_trivial_map_no_arguments(self):
       raises(TypeError, map)
-      
+
    def test_trivial_map_no_function_no_seq(self):
       raises(TypeError, map, None)
 
    def test_trivial_map_no_fuction_one_seq(self):
       assert map(None, [1, 2, 3]) == [1, 2, 3]
-      
+
    def test_trivial_map_no_function(self):
       assert map(None, [1,2,3], [4,5,6], [7,8], [1]) == (
                        [(1, 4, 7, 1), (2, 5, 8, None), (3, 6, None, None)])
-                       
+
    def test_map_identity1(self):
       a = ['1', 2, 3, 'b', None]
       b = a[:]
       assert map(lambda x: x, a) == a
       assert a == b
- 
+
    def test_map_None(self):
       a = ['1', 2, 3, 'b', None]
       b = a[:]
@@ -72,7 +72,7 @@
    def test_sum(self):
        assert reduce(lambda x, y: x+y, [1,2,3,4], 0) == 10
        assert reduce(lambda x, y: x+y, [1,2,3,4]) == 10
-   
+
    def test_minus(self):
        assert reduce(lambda x, y: x-y, [10, 2, 8]) == 0
        assert reduce(lambda x, y: x-y, [2, 8], 10) == 0
@@ -86,7 +86,7 @@
        assert filter(None, txt) == txt
        tup = ("a", None, 0, [], 1)
        assert filter(None, tup) == ("a", 1)
-       
+
    def test_function(self):
        assert filter(lambda x: x != "a", "a small text") == " smll text"
 
@@ -131,3 +131,54 @@
       assert len(r) == 0
       assert list(reversed(list(reversed("hello")))) == ['h','e','l','l','o']
       raises(TypeError, reversed, reversed("hello"))
+
+
+class AppTestAllAny:
+    """
+    These are copied directly and replicated from the Python 2.5 source code.
+    """
+
+    def test_all(self):
+
+        class TestFailingBool:
+            def __nonzero__(self):
+                raise RuntimeError
+        class TestFailingIter:
+            def __iter__(self):
+                raise RuntimeError
+
+        assert all([2, 4, 6]) == True
+        assert all([2, None, 6]) == False
+        raises(RuntimeError, all, [2, TestFailingBool(), 6])
+        raises(RuntimeError, all, TestFailingIter())
+        raises(TypeError, all, 10)               # Non-iterable
+        raises(TypeError, all)                   # No args
+        raises(TypeError, all, [2, 4, 6], [])    # Too many args
+        assert all([]) == True                   # Empty iterator
+        S = [50, 60]
+        assert all(x > 42 for x in S) == True
+        S = [50, 40, 60]
+        assert all(x > 42 for x in S) == False
+
+    def test_any(self):
+
+        class TestFailingBool:
+            def __nonzero__(self):
+                raise RuntimeError
+        class TestFailingIter:
+            def __iter__(self):
+                raise RuntimeError
+
+        assert any([None, None, None]) == False
+        assert any([None, 4, None]) == True
+        raises(RuntimeError, any, [None, TestFailingBool(), 6])
+        raises(RuntimeError, all, TestFailingIter())
+        raises(TypeError, any, 10)               # Non-iterable
+        raises(TypeError, any)                   # No args
+        raises(TypeError, any, [2, 4, 6], [])    # Too many args
+        assert any([]) == False                  # Empty iterator
+        S = [40, 60, 30]
+        assert any(x > 42 for x in S) == True
+        S = [10, 20, 30]
+        assert any(x > 42 for x in S) == False
+

Modified: pypy/dist/pypy/translator/interactive.py
==============================================================================
--- pypy/dist/pypy/translator/interactive.py	(original)
+++ pypy/dist/pypy/translator/interactive.py	Mon Feb 27 20:03:32 2006
@@ -81,9 +81,9 @@
         else:
             # check consistency
             if argtypes is not None and argtypes != self.ann_argtypes:
-                raise Exception("incosistent argtype supplied")
+                raise Exception("inconsistent argtype supplied")
             if policy is not None and policy != self.ann_policy:
-                raise Exception("incosistent annotation polish supplied")
+                raise Exception("inconsistent annotation polish supplied")
 
     def update_options(self, argtypes, kwds):
         if argtypes or kwds.get('policy'):
@@ -91,7 +91,7 @@
         for optname, value in kwds.iteritems():
             if optname in self.frozen_options:
                 if getattr(self.driver.options, optname) != value:
-                     raise Exception("incosistent option supplied: %s" % optname)
+                     raise Exception("inconsistent option supplied: %s" % optname)
             else:
                 setattr(self.driver.options, optname, value)
                 self.frozen_options[optname] = True



More information about the Pypy-commit mailing list