Repository navigation
gh-161004: Align Python implementation of functools.reduce with C implementation
#161025
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -259,9 +259,13 @@ def reduce(function, sequence, /, initial=_initial_missing): | |||||||||||||||||||
| else: | ||||||||||||||||||||
| value = initial | ||||||||||||||||||||
|
|
||||||||||||||||||||
| for element in it: | ||||||||||||||||||||
| while(True): | ||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
| try: | ||||||||||||||||||||
| element = next(it) | ||||||||||||||||||||
| except StopIteration: | ||||||||||||||||||||
| # Stop when no elements are left | ||||||||||||||||||||
| break | ||||||||||||||||||||
|
Comment on lines
+263
to
+267
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
| value = function(value, element) | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep that new line. |
||||||||||||||||||||
| return value | ||||||||||||||||||||
|
|
||||||||||||||||||||
| try: | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import unittest | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add this test in |
||
| import operator | ||
| import sys | ||
| import functools as c_functools | ||
|
|
||
| class Test_reduce(unittest.TestCase): | ||
| def test_add_func(self): | ||
| del sys.modules['functools'] | ||
| sys.modules['_functools'] = None # block the C accelerator | ||
| import functools as py_functools | ||
|
|
||
| class Iterator: | ||
| def __init__(self): | ||
| self.items = [1, 2, 3, 4, 4] | ||
| def __next__(self): | ||
| if not self.items: | ||
| raise StopIteration | ||
| return self.items.pop(0) | ||
| class Iterable: | ||
| def __iter__(self): | ||
| return Iterator() | ||
| self.assertEqual( | ||
| c_functools.reduce(operator.add, Iterable()), | ||
| py_functools.reduce(operator.add, Iterable()) | ||
| ) | ||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really think it's necessary to change this though as it's a "roughly equivalent" code.