Skip to content

bpo-35606: Fix math.prod tests using 'start' as keyword parameter #28595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions Lib/test/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -1803,16 +1803,22 @@ def test_prod(self):
self.assertRaises(TypeError, prod)
self.assertRaises(TypeError, prod, 42)
self.assertRaises(TypeError, prod, ['a', 'b', 'c'])
self.assertRaises(TypeError, prod, ['a', 'b', 'c'], '')
self.assertRaises(TypeError, prod, [b'a', b'c'], b'')
self.assertRaises(TypeError, prod, ['a', 'b', 'c'], start='')
self.assertRaises(TypeError, prod, [b'a', b'c'], start=b'')
values = [bytearray(b'a'), bytearray(b'b')]
self.assertRaises(TypeError, prod, values, bytearray(b''))
self.assertRaises(TypeError, prod, values, start=bytearray(b''))
self.assertRaises(TypeError, prod, [[1], [2], [3]])
self.assertRaises(TypeError, prod, [{2:3}])
self.assertRaises(TypeError, prod, [{2:3}]*2, {2:3})
self.assertRaises(TypeError, prod, [[1], [2], [3]], [])
self.assertRaises(TypeError, prod, [{2:3}]*2, start={2:3})
self.assertRaises(TypeError, prod, [[1], [2], [3]], start=[])

# Some odd cases
self.assertEqual(prod([2, 3], start='ab'), 'abababababab')
self.assertEqual(prod([2, 3], start=[1, 2]), [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2])
self.assertEqual(prod([], start={2: 3}), {2:3})

with self.assertRaises(TypeError):
prod([10, 20], [30, 40]) # start is a keyword-only argument
prod([10, 20], 1) # start is a keyword-only argument

self.assertEqual(prod([0, 1, 2, 3]), 0)
self.assertEqual(prod([1, 0, 2, 3]), 0)
Expand Down
11 changes: 3 additions & 8 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3082,14 +3082,9 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start)
}

if (result == NULL) {
result = PyLong_FromLong(1);
if (result == NULL) {
Py_DECREF(iter);
return NULL;
}
} else {
Py_INCREF(result);
result = _PyLong_GetOne();
}
Py_INCREF(result);
#ifndef SLOW_PROD
/* Fast paths for integers keeping temporary products in C.
* Assumes all inputs are the same type.
Expand All @@ -3105,7 +3100,7 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start)
}
/* Loop over all the items in the iterable until we finish, we overflow
* or we found a non integer element */
while(result == NULL) {
while (result == NULL) {
item = PyIter_Next(iter);
if (item == NULL) {
Py_DECREF(iter);
Expand Down