bpo-32321: Add pure Python fallback for functools.reduce#8548
Conversation
|
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA). Unfortunately our records indicate you have not signed the CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. When your account is ready, please add a comment in this pull request You can check yourself Thanks again for your contribution, we look forward to reviewing it! |
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Please test both implementations.
There was a problem hiding this comment.
This test tests only C implementation of reduce() if it is available. Please make both implementations be tested. See TestPartial and TestCmpToKey for example.
There was a problem hiding this comment.
Add a sentence ending period and "Path by yourname."
There was a problem hiding this comment.
Sorry, what do you mean?
There was a problem hiding this comment.
Usually changes from non-core contributors are attributed by adding "Path by contributorname." In your case it will be "Patch by Robert Wright."
There was a problem hiding this comment.
Thanks, the typo confused me.
There was a problem hiding this comment.
Oh, sorry. I just noticed it!
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
|
I have made the requested changes; please review again |
|
Thanks for making the requested changes! @serhiy-storchaka, @zooba: please review the changes made to this pull request. |
|
Why do you think we might need this? IMO, this just adds unnecessary weight to the code base, resulting in a net degradation of maintainability. |
|
Consistency. If the In my opinion, either all of |
|
Oh, and the CLA's now been received, so the label can be removed. |
| try: | ||
| value = next(it) | ||
| except StopIteration: | ||
| raise TypeError("reduce() of empty sequence with no initial value") |
…exception context
| ### reduce() sequence to a single item | ||
| ################################################################################ | ||
|
|
||
| _initial_missing = object() |
There was a problem hiding this comment.
functools.py already has a sentinel singleton, _NOT_FOUND used by cached_property. Would it be possible to reuse it, instead of adding a second sentinel value?
There was a problem hiding this comment.
Not sure how I feel about this suggestion. On the one hand, there would be a (slight) overhead reduction by reusing the same sentinel. On the other, semantically, this isn't really a _NOT_FOUND, so much as a "not given".
As these sentinels aren't part of the public interface, we could rename them into an object that semantically makes sense - but I'm loathe to touch another function, if I can help it.
| if c_functools: | ||
| func = c_functools.reduce | ||
|
|
||
| class TestReduce: |
There was a problem hiding this comment.
Can you please rename it to BaseTestReduce to make it more obvious that it's not a regular test case?
There was a problem hiding this comment.
Done this way to be consistent with the other tests.
I have no objection to this suggestion, but as it would also need to be done for TestPartial and TestCmpToKey, I feel it is out of scope for this change.
|
|
||
|
|
||
| @unittest.skipUnless(c_functools, 'requires the C _functools module') | ||
| class TestReduceC(TestReduce, unittest.TestCase): |
There was a problem hiding this comment.
nitpick: I suggest to move the C implementation after the Python implementation, since the Python impl is always present, whereas the other one is optional :-)
There was a problem hiding this comment.
As with my other comment, I have no objection to this suggestion, but as it would also need to be done for TestPartial and TestCmpToKey, I feel it is out of scope for this change.
Yes. For me, it helps me to understand more easily the behavior and subtle cases, without having to read C code which is harder to decipher. It's also common that we maintain a Python a C implementation of the same function. It helps other Python implementations to quickly get new features without having to reimplement them in their own "faster language" (like RPython for PyPy or .NET for IronPython). The datetime module has been fully rewritten in Python recently :-) For this function, it's just 12 lines of Python (if you ignore doc and empty lines). It wouldn't call it a huge maintenance burden :-) |
|
Thanks @madman-bob for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7. |
|
GH-10092 is a backport of this pull request to the 3.7 branch. |
(cherry picked from commit e25d5fc) Co-authored-by: madman-bob <madman.bob@hotmail.co.uk>
|
@madman-bob: Hi, I merged your PR. You wrote that you care about consistency in the test. I agree that it matters: would you mind to propose a PR to implement my proposed changes? |
Add a Python fallback for
functools.reduce, and enable the appropriate tests to run onpy_functools.https://bugs.python.org/issue32321
https://bugs.python.org/issue32321