You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
gh-161004: Align Python implementation of functools.reduce with C implementation - #161025
Fixes#161004
This PR fix functools.reduce.The C functools_reduce calls PyObject_GetIter(seq) once and then only PyIter_Next(it). The Python version calls iter(sequence) and then for element in it:, which calls iter() again on the iterator.
this PR fix this behavior by change for loop for element in it: to while loop. While loop is terminated next(element) throw a StopIteration exception and error handle it and break the loop
this PR also add a test and change the documentation
If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.
NotorandXor256
changed the title
Fixed py_functools.reduce and added test_reduce.py as test (issue #161004)
gh-161004: Fixed py_functools.reduce and added test_reduce.py as test (issue #161004)
Oct 31, 2026
picnixz
changed the title
gh-161004: Fixed py_functools.reduce and added test_reduce.py as test (issue #161004)
gh-161004: Align Python implementation of functools.reduce with C implementation
Oct 31, 2026
I wouldn't make this edit. As noted in the issue, the iterator protocol specifically allows this. Also, this code is a "rough equivalent" intended to communicate the concept of what reduce does.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #161004
This PR fix functools.reduce.The C functools_reduce calls PyObject_GetIter(seq) once and then only PyIter_Next(it). The Python version calls iter(sequence) and then for element in it:, which calls iter() again on the iterator.
this PR fix this behavior by change for loop for element in it: to while loop. While loop is terminated next(element) throw a StopIteration exception and error handle it and break the loop
this PR also add a test and change the documentation
functools.reducecallsiter()twice, unlike the C implementation #161004