mirror of
https://github.com/python/cpython.git
synced 2025-09-27 18:59:43 +00:00
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.
This commit is contained in:
parent
27354ccec9
commit
e29e6bffb5
4 changed files with 26 additions and 8 deletions
|
@ -1234,6 +1234,7 @@ class BuiltinTest(unittest.TestCase):
|
||||||
class G:
|
class G:
|
||||||
pass
|
pass
|
||||||
self.assertRaises(TypeError, zip, a, G())
|
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
|
# 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.
|
# result list when one of its arguments doesn't say how long it is.
|
||||||
|
|
|
@ -321,10 +321,11 @@ class TestReduce(unittest.TestCase):
|
||||||
self.sofar.append(n*n)
|
self.sofar.append(n*n)
|
||||||
n += 1
|
n += 1
|
||||||
return self.sofar[i]
|
return self.sofar[i]
|
||||||
|
def add(x, y):
|
||||||
self.assertEqual(self.func(lambda x, y: x+y, ['a', 'b', 'c'], ''), 'abc')
|
return x + y
|
||||||
|
self.assertEqual(self.func(add, ['a', 'b', 'c'], ''), 'abc')
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.func(lambda x, y: x+y, [['a', 'c'], [], ['d', 'w']], []),
|
self.func(add, [['a', 'c'], [], ['d', 'w']], []),
|
||||||
['a','c','d','w']
|
['a','c','d','w']
|
||||||
)
|
)
|
||||||
self.assertEqual(self.func(lambda x, y: x*y, range(2,8), 1), 5040)
|
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),
|
self.func(lambda x, y: x*y, range(2,21), 1),
|
||||||
2432902008176640000
|
2432902008176640000
|
||||||
)
|
)
|
||||||
self.assertEqual(self.func(lambda x, y: x+y, Squares(10)), 285)
|
self.assertEqual(self.func(add, Squares(10)), 285)
|
||||||
self.assertEqual(self.func(lambda x, y: x+y, Squares(10), 0), 285)
|
self.assertEqual(self.func(add, Squares(10), 0), 285)
|
||||||
self.assertEqual(self.func(lambda x, y: x+y, Squares(0), 0), 0)
|
self.assertEqual(self.func(add, Squares(0), 0), 0)
|
||||||
self.assertRaises(TypeError, self.func)
|
self.assertRaises(TypeError, self.func)
|
||||||
self.assertRaises(TypeError, self.func, 42, 42)
|
self.assertRaises(TypeError, self.func, 42, 42)
|
||||||
self.assertRaises(TypeError, self.func, 42, 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.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, 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:
|
class BadSeq:
|
||||||
def __getitem__(self, index):
|
def __getitem__(self, index):
|
||||||
|
|
|
@ -45,6 +45,9 @@ Core and Builtins
|
||||||
Extensions
|
Extensions
|
||||||
----------
|
----------
|
||||||
|
|
||||||
|
- 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 #9570: Use PEP 383 decoding in os.mknod and os.mkfifo.
|
||||||
|
|
||||||
- Issue #6915: Under Windows, os.listdir() didn't release the Global
|
- Issue #6915: Under Windows, os.listdir() didn't release the Global
|
||||||
|
|
|
@ -302,8 +302,9 @@ functools_reduce(PyObject *self, PyObject *args)
|
||||||
|
|
||||||
it = PyObject_GetIter(seq);
|
it = PyObject_GetIter(seq);
|
||||||
if (it == NULL) {
|
if (it == NULL) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
if (PyErr_ExceptionMatches(PyExc_TypeError))
|
||||||
"reduce() arg 2 must support iteration");
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"reduce() arg 2 must support iteration");
|
||||||
Py_XDECREF(result);
|
Py_XDECREF(result);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue