summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAlexander Belopolsky <alexander.belopolsky@gmail.com>2010-08-16 18:55:46 (GMT)
committerAlexander Belopolsky <alexander.belopolsky@gmail.com>2010-08-16 18:55:46 (GMT)
commite29e6bffb577233c0e327f831c037ebf92da0157 (patch)
treef3b58faa1e924dbd606f0354b11b7488d520d155 /Lib
parent27354ccec97f9aa79449397cd29f7d70b8e9f445 (diff)
downloadcpython-e29e6bffb577233c0e327f831c037ebf92da0157.zip
cpython-e29e6bffb577233c0e327f831c037ebf92da0157.tar.gz
cpython-e29e6bffb577233c0e327f831c037ebf92da0157.tar.bz2
Issue #665761: functools.reduce() will no longer mask exceptions other
than TypeError raised by the iterator argument. Also added a test to check that zip() already behaves similarly.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_builtin.py1
-rw-r--r--Lib/test/test_functools.py25
2 files changed, 20 insertions, 6 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 3f412b7..b25f3c4 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -1234,6 +1234,7 @@ class BuiltinTest(unittest.TestCase):
class G:
pass
self.assertRaises(TypeError, zip, a, G())
+ self.assertRaises(RuntimeError, zip, a, TestFailingIter())
# Make sure zip doesn't try to allocate a billion elements for the
# result list when one of its arguments doesn't say how long it is.
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index f9e8a97..2b6da64 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -321,10 +321,11 @@ class TestReduce(unittest.TestCase):
self.sofar.append(n*n)
n += 1
return self.sofar[i]
-
- self.assertEqual(self.func(lambda x, y: x+y, ['a', 'b', 'c'], ''), 'abc')
+ def add(x, y):
+ return x + y
+ self.assertEqual(self.func(add, ['a', 'b', 'c'], ''), 'abc')
self.assertEqual(
- self.func(lambda x, y: x+y, [['a', 'c'], [], ['d', 'w']], []),
+ self.func(add, [['a', 'c'], [], ['d', 'w']], []),
['a','c','d','w']
)
self.assertEqual(self.func(lambda x, y: x*y, range(2,8), 1), 5040)
@@ -332,15 +333,27 @@ class TestReduce(unittest.TestCase):
self.func(lambda x, y: x*y, range(2,21), 1),
2432902008176640000
)
- self.assertEqual(self.func(lambda x, y: x+y, Squares(10)), 285)
- self.assertEqual(self.func(lambda x, y: x+y, Squares(10), 0), 285)
- self.assertEqual(self.func(lambda x, y: x+y, Squares(0), 0), 0)
+ 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())
+
+ class TestFailingIter:
+ def __iter__(self):
+ raise RuntimeError
+ self.assertRaises(RuntimeError, self.func, add, TestFailingIter())
+
+ self.assertEqual(self.func(add, [], None), None)
+ self.assertEqual(self.func(add, [], 42), 42)
class BadSeq:
def __getitem__(self, index):