Skip to content

Commit 6c65b61

Browse files
committed
gh-149536: use sentinel to fix _functools.reduce() signature
1 parent 4ae1a26 commit 6c65b61

4 files changed

Lines changed: 14 additions & 7 deletions

File tree

Lib/inspect.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2200,7 +2200,8 @@ def wrap_value(s):
22002200
except NameError:
22012201
raise ValueError
22022202

2203-
if isinstance(value, (str, int, float, bytes, bool, type(None))):
2203+
if isinstance(value, (str, int, float, bytes, bool, type(None),
2204+
sentinel)):
22042205
return ast.Constant(value)
22052206
raise ValueError
22062207

Lib/test/test_inspect/test_inspect.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6255,8 +6255,7 @@ def test_faulthandler_module_has_signatures(self):
62556255
self._test_module_has_signatures(faulthandler, unsupported_signature=unsupported_signature)
62566256

62576257
def test_functools_module_has_signatures(self):
6258-
unsupported_signature = {"reduce"}
6259-
self._test_module_has_signatures(functools, unsupported_signature=unsupported_signature)
6258+
self._test_module_has_signatures(functools)
62606259

62616260
def test_gc_module_has_signatures(self):
62626261
import gc

Modules/_functoolsmodule.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "pycore_tuple.h" // _PyTuple_ITEMS()
1010
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()
1111

12+
PyObject *_initial_missing;
1213

1314
#include "clinic/_functoolsmodule.c.h"
1415
/*[clinic input]
@@ -1066,7 +1067,7 @@ _functools.reduce
10661067
function as func: object
10671068
iterable as seq: object
10681069
/
1069-
initial as result: object = NULL
1070+
initial as result: object(c_default="NULL") = _functools._initial_missing
10701071
10711072
Apply a function of two arguments cumulatively to the items of an iterable, from left to right.
10721073
@@ -1081,7 +1082,7 @@ calculates ((((1 + 2) + 3) + 4) + 5).
10811082
static PyObject *
10821083
_functools_reduce_impl(PyObject *module, PyObject *func, PyObject *seq,
10831084
PyObject *result)
1084-
/*[clinic end generated code: output=30d898fe1267c79d input=4ccfb74548ce5170]*/
1085+
/*[clinic end generated code: output=30d898fe1267c79d input=7e5eaeb4f8a7a78d]*/
10851086
{
10861087
PyObject *args, *it;
10871088

@@ -1982,6 +1983,11 @@ _functools_exec(PyObject *module)
19821983
// lru_list_elem is used only in _lru_cache_wrapper.
19831984
// So we don't expose it in module namespace.
19841985

1986+
_initial_missing = PySentinel_New("_initial_missing", "_functools");
1987+
if (PyModule_Add(module, "_initial_missing", _initial_missing) < 0) {
1988+
Py_XDECREF(_initial_missing);
1989+
return -1;
1990+
}
19851991
return 0;
19861992
}
19871993

Modules/clinic/_functoolsmodule.c.h

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)