Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,22 @@ def test_sum(self):

self.assertEqual(sum(range(10), 1000), 1045)
self.assertEqual(sum(range(10), start=1000), 1045)
self.assertEqual(sum(range(10), 2**31-5), 2**31+40)
self.assertEqual(sum(range(10), 2**63-5), 2**63+40)

self.assertEqual(sum(i % 2 != 0 for i in range(10)), 5)
self.assertEqual(sum((i % 2 != 0 for i in range(10)), 2**31-3),
2**31+2)
self.assertEqual(sum((i % 2 != 0 for i in range(10)), 2**63-3),
2**63+2)

self.assertEqual(sum(i / 2 for i in range(10)), 22.5)
self.assertEqual(sum((i / 2 for i in range(10)), 1000), 1022.5)
self.assertEqual(sum((i / 2 for i in range(10)), 1000.25), 1022.75)
self.assertEqual(sum([0.5, 1]), 1.5)
self.assertEqual(sum([1, 0.5]), 1.5)
self.assertEqual(repr(sum([-0.0])), '0.0')
self.assertEqual(repr(sum([-0.0], -0.0)), '-0.0')

self.assertRaises(TypeError, sum)
self.assertRaises(TypeError, sum, 42)
Expand Down
6 changes: 3 additions & 3 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2357,7 +2357,7 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
Assumes all inputs are the same type. If the assumption fails, default
to the more general routine.
*/
if (PyLong_CheckExact(result)) {
if (PyLong_CheckExact(result) || PyBool_Check(result)) {
int overflow;
long i_result = PyLong_AsLongAndOverflow(result, &overflow);
/* If this already overflowed, don't even enter the loop. */
Expand All @@ -2373,7 +2373,7 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
return NULL;
return PyLong_FromLong(i_result);
}
if (PyLong_CheckExact(item)) {
if (PyLong_CheckExact(item) || PyBool_Check(item)) {
long b = PyLong_AsLongAndOverflow(item, &overflow);
long x = i_result + b;
if (overflow == 0 && ((x^i_result) >= 0 || (x^b) >= 0)) {
Expand Down Expand Up @@ -2419,7 +2419,7 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
Py_DECREF(item);
continue;
}
if (PyLong_CheckExact(item)) {
if (PyLong_Check(item)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this not PyLong_CheckExact(item) || PyBool_Check(item) like the one above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because why not? This extends the optimization to other int subclasses.

There is a reason why PyLong_Check(item) is not used above: because __radd__ method in the int subclass takes precedence. But for float+int it is not used.

long value;
int overflow;
value = PyLong_AsLongAndOverflow(item, &overflow);
Expand Down