1. Pypy
  2. Untitled project
  3. pypy

Commits

Manuel Jacob  committed 1ae4118

Prevent functools.reduce from binding to TestReduce class.

  • Participants
  • Parent commits 3b672ce
  • Branches py3k

Comments (0)

Files changed (1)

File lib-python/3.2/test/test_functools.py

View file
         self.assertEqual(wrapper.dict_attr, f.dict_attr)
 
 class TestReduce(unittest.TestCase):
-    func = functools.reduce
-
     def test_reduce(self):
         class Squares:
             def __init__(self, max):
                     self.sofar.append(n*n)
                     n += 1
                 return self.sofar[i]
+        reduce = functools.reduce
         def add(x, y):
             return x + y
-        self.assertEqual(self.func(add, ['a', 'b', 'c'], ''), 'abc')
+        self.assertEqual(reduce(add, ['a', 'b', 'c'], ''), 'abc')
         self.assertEqual(
-            self.func(add, [['a', 'c'], [], ['d', 'w']], []),
+            reduce(add, [['a', 'c'], [], ['d', 'w']], []),
             ['a','c','d','w']
         )
-        self.assertEqual(self.func(lambda x, y: x*y, range(2,8), 1), 5040)
+        self.assertEqual(reduce(lambda x, y: x*y, range(2,8), 1), 5040)
         self.assertEqual(
-            self.func(lambda x, y: x*y, range(2,21), 1),
+            reduce(lambda x, y: x*y, range(2,21), 1),
             2432902008176640000
         )
-        self.assertEqual(self.func(add, Squares(10)), 285)
-        self.assertEqual(self.func(add, Squares(10), 0), 285)
-        self.assertEqual(self.func(add, Squares(0), 0), 0)
-        self.assertRaises(TypeError, self.func)
-        self.assertRaises(TypeError, self.func, 42, 42)
-        self.assertRaises(TypeError, self.func, 42, 42, 42)
-        self.assertEqual(self.func(42, "1"), "1") # func is never called with one item
-        self.assertEqual(self.func(42, "", "1"), "1") # func is never called with one item
-        self.assertRaises(TypeError, self.func, 42, (42, 42))
-        self.assertRaises(TypeError, self.func, add, []) # arg 2 must not be empty sequence with no initial value
-        self.assertRaises(TypeError, self.func, add, "")
-        self.assertRaises(TypeError, self.func, add, ())
-        self.assertRaises(TypeError, self.func, add, object())
+        self.assertEqual(reduce(add, Squares(10)), 285)
+        self.assertEqual(reduce(add, Squares(10), 0), 285)
+        self.assertEqual(reduce(add, Squares(0), 0), 0)
+        self.assertRaises(TypeError, reduce)
+        self.assertRaises(TypeError, reduce, 42, 42)
+        self.assertRaises(TypeError, reduce, 42, 42, 42)
+        self.assertEqual(reduce(42, "1"), "1") # func is never called with one item
+        self.assertEqual(reduce(42, "", "1"), "1") # func is never called with one item
+        self.assertRaises(TypeError, reduce, 42, (42, 42))
+        self.assertRaises(TypeError, reduce, add, []) # arg 2 must not be empty sequence with no initial value
+        self.assertRaises(TypeError, reduce, add, "")
+        self.assertRaises(TypeError, reduce, add, ())
+        self.assertRaises(TypeError, reduce, add, object())
 
         class TestFailingIter:
             def __iter__(self):
                 raise RuntimeError
-        self.assertRaises(RuntimeError, self.func, add, TestFailingIter())
+        self.assertRaises(RuntimeError, reduce, add, TestFailingIter())
 
-        self.assertEqual(self.func(add, [], None), None)
-        self.assertEqual(self.func(add, [], 42), 42)
+        self.assertEqual(reduce(add, [], None), None)
+        self.assertEqual(reduce(add, [], 42), 42)
 
         class BadSeq:
             def __getitem__(self, index):
                 raise ValueError
-        self.assertRaises(ValueError, self.func, 42, BadSeq())
+        self.assertRaises(ValueError, reduce, 42, BadSeq())
 
     # Test reduce()'s use of iterators.
     def test_iterator_usage(self):
                 else:
                     raise IndexError
 
+        reduce = functools.reduce
         from operator import add
-        self.assertEqual(self.func(add, SequenceClass(5)), 10)
-        self.assertEqual(self.func(add, SequenceClass(5), 42), 52)
-        self.assertRaises(TypeError, self.func, add, SequenceClass(0))
-        self.assertEqual(self.func(add, SequenceClass(0), 42), 42)
-        self.assertEqual(self.func(add, SequenceClass(1)), 0)
-        self.assertEqual(self.func(add, SequenceClass(1), 42), 42)
+        self.assertEqual(reduce(add, SequenceClass(5)), 10)
+        self.assertEqual(reduce(add, SequenceClass(5), 42), 52)
+        self.assertRaises(TypeError, reduce, add, SequenceClass(0))
+        self.assertEqual(reduce(add, SequenceClass(0), 42), 42)
+        self.assertEqual(reduce(add, SequenceClass(1)), 0)
+        self.assertEqual(reduce(add, SequenceClass(1), 42), 42)
 
         d = {"one": 1, "two": 2, "three": 3}
-        self.assertEqual(self.func(add, d), "".join(d.keys()))
+        self.assertEqual(reduce(add, d), "".join(d.keys()))
 
 class TestCmpToKey(unittest.TestCase):
     def test_cmp_to_key(self):