diff options
author | Alexander Belopolsky <alexander.belopolsky@gmail.com> | 2010-08-16 19:46:32 (GMT) |
---|---|---|
committer | Alexander Belopolsky <alexander.belopolsky@gmail.com> | 2010-08-16 19:46:32 (GMT) |
commit | 7a9bdbc1c2214bec2133139dd8830f17e0383eed (patch) | |
tree | a332279d11accbf9106fb82bed646eaff4769773 | |
parent | 5b37ce64c1a8108406cd0ae19e15b9ebd855c2f6 (diff) | |
download | cpython-7a9bdbc1c2214bec2133139dd8830f17e0383eed.zip cpython-7a9bdbc1c2214bec2133139dd8830f17e0383eed.tar.gz cpython-7a9bdbc1c2214bec2133139dd8830f17e0383eed.tar.bz2 |
Merged revisions 84098 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r84098 | alexander.belopolsky | 2010-08-16 14:55:46 -0400 (Mon, 16 Aug 2010) | 4 lines
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.
........
-rw-r--r-- | Lib/test/test_builtin.py | 1 | ||||
-rw-r--r-- | Lib/test/test_functools.py | 25 | ||||
-rw-r--r-- | Misc/NEWS | 3 | ||||
-rw-r--r-- | Modules/_functoolsmodule.c | 5 |
4 files changed, 26 insertions, 8 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 34c2bb1..2a08337 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -1221,6 +1221,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 7b79f28..d20bafe 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -302,10 +302,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) @@ -313,15 +314,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): @@ -435,6 +435,9 @@ Library Extension Modules ----------------- +- Issue #665761: ``functools.reduce()`` will no longer mask exceptions + other than ``TypeError`` raised by the iterator argument. + - Issue #9570: Use PEP 383 decoding in os.mknod and os.mkfifo. - Issue #9324: Add parameter validation to signal.signal on Windows in order diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c index bf2ea3b..3437353 100644 --- a/Modules/_functoolsmodule.c +++ b/Modules/_functoolsmodule.c @@ -302,8 +302,9 @@ functools_reduce(PyObject *self, PyObject *args) it = PyObject_GetIter(seq); if (it == NULL) { - PyErr_SetString(PyExc_TypeError, - "reduce() arg 2 must support iteration"); + if (PyErr_ExceptionMatches(PyExc_TypeError)) + PyErr_SetString(PyExc_TypeError, + "reduce() arg 2 must support iteration"); Py_XDECREF(result); return NULL; } |