gh-149083: Convert _initial_missing for pure py reduce to sentinel#149536
gh-149083: Convert _initial_missing for pure py reduce to sentinel#149536sobolevn merged 1 commit intopython:mainfrom
_initial_missing for pure py reduce to sentinel#149536Conversation
JelleZijlstra
left a comment
There was a problem hiding this comment.
Maybe ask Hugo how he feels about applying a few more of these in 3.15? It seems fairly low risk when replacing bare object().
|
I agree. It feels like the change should happen together with the addition of |
|
It would be nice to fix the C version (it has broken signature now), but it seems that that's not backed yet by the AC. Or did I miss something? |
|
Yes, AFAIK |
Attached patch "works for me": >>> import inspect, functools
>>> inspect.signature(functools.reduce)
<Signature (function, iterable, /, initial=_initial_missing)>Details(Don't forgot to run AC.) diff --git a/Lib/inspect.py b/Lib/inspect.py
index a96b3dc954e..dc5a6e3be88 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2200,7 +2200,8 @@ def wrap_value(s):
except NameError:
raise ValueError
- if isinstance(value, (str, int, float, bytes, bool, type(None))):
+ if isinstance(value, (str, int, float, bytes, bool, type(None),
+ sentinel)):
return ast.Constant(value)
raise ValueError
diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c
index 19bdf3d47c2..13da7944b61 100644
--- a/Modules/_functoolsmodule.c
+++ b/Modules/_functoolsmodule.c
@@ -9,6 +9,7 @@
#include "pycore_tuple.h" // _PyTuple_ITEMS()
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()
+PyObject *_initial_missing;
#include "clinic/_functoolsmodule.c.h"
/*[clinic input]
@@ -1066,7 +1067,7 @@ _functools.reduce
function as func: object
iterable as seq: object
/
- initial as result: object = NULL
+ initial as result: object(c_default="NULL") = _functools._initial_missing
Apply a function of two arguments cumulatively to the items of an iterable, from left to right.
@@ -1081,7 +1082,7 @@ calculates ((((1 + 2) + 3) + 4) + 5).
static PyObject *
_functools_reduce_impl(PyObject *module, PyObject *func, PyObject *seq,
PyObject *result)
-/*[clinic end generated code: output=30d898fe1267c79d input=4ccfb74548ce5170]*/
+/*[clinic end generated code: output=30d898fe1267c79d input=7e5eaeb4f8a7a78d]*/
{
PyObject *args, *it;
@@ -1982,6 +1983,14 @@ _functools_exec(PyObject *module)
// lru_list_elem is used only in _lru_cache_wrapper.
// So we don't expose it in module namespace.
+ _initial_missing = PySentinel_New("_initial_missing", "_functools");
+ if (_initial_missing == NULL) {
+ return -1;
+ }
+ if (PyModule_Add(module, "_initial_missing", _initial_missing) < 0) {
+ Py_DECREF(_initial_missing);
+ return -1;
+ }
return 0;
}
|
|
|
||
| def reduce(function, sequence, initial=_initial_missing): | ||
| """ | ||
| reduce(function, iterable, /[, initial]) -> value |
There was a problem hiding this comment.
This signature line now looks redundant.
There was a problem hiding this comment.
I change the signature in another PR :)
Yes, that's fine. |
|
Thanks @sobolevn for the PR 🌮🎉.. I'm working now to backport this PR to: 3.15. |
|
GH-149592 is a backport of this pull request to the 3.15 branch. |
This is not covered in #149084
Difference is not big, but noticable. Before:

Notice the default parameter repr, which is rather unpretty.
After:

skip news, because there should be no user facing changes.