diff --git a/Doc/library/functools.rst b/Doc/library/functools.rst index 4eeafad3fbd9156..8f975097a878439 100644 --- a/Doc/library/functools.rst +++ b/Doc/library/functools.rst @@ -476,8 +476,12 @@ The :mod:`!functools` module defines the following functions: value = next(it) else: value = initial - for element in it: - value = function(value, element) + while(True): + try: + element = next(it) + except StopIteration: + break + value = function(value, element) return value See :func:`itertools.accumulate` for an iterator that yields all intermediate diff --git a/Doc/library/os.path.rst b/Doc/library/os.path.rst index f5ddb426c915f5a..4825093ccfe6d12 100644 --- a/Doc/library/os.path.rst +++ b/Doc/library/os.path.rst @@ -415,7 +415,9 @@ the :mod:`glob` module.) Normalize the case of a pathname. On Windows, convert all characters in the pathname to lowercase, and also convert forward slashes to backward slashes. - On other operating systems, return the path unchanged. + On other operating systems (Posix), return String or Bytes converted from + Path-like Object + .. versionchanged:: 3.6 Accepts a :term:`path-like object`. diff --git a/Lib/functools.py b/Lib/functools.py index 2c08791d4d2651b..34185660e104d33 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -259,7 +259,12 @@ def reduce(function, sequence, /, initial=_initial_missing): else: value = initial - for element in it: + while(True): + try: + element = next(it) + except StopIteration: + # Stop when no elements are left + break value = function(value, element) return value diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 8025b063397a038..b1bf3f258a77f1d 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -52,7 +52,7 @@ def _get_sep(path): # (another function should be defined to do that). def normcase(s, /): - """Normalize case of pathname. Has no effect under Posix""" + """Normalize case of pathname. Normalizes path-like Objects into Str or Bytes """ return os.fspath(s) diff --git a/Lib/test/test_reduce.py b/Lib/test/test_reduce.py new file mode 100644 index 000000000000000..5cddee2fb293a2c --- /dev/null +++ b/Lib/test/test_reduce.py @@ -0,0 +1,28 @@ +import unittest +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() diff --git a/Misc/NEWS.d/next/Documentation/2026-10-29-18-57-38.gh-issue-160904.nyxAv_.rst b/Misc/NEWS.d/next/Documentation/2026-10-29-18-57-38.gh-issue-160904.nyxAv_.rst new file mode 100644 index 000000000000000..03dd3581b5fce45 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2026-10-29-18-57-38.gh-issue-160904.nyxAv_.rst @@ -0,0 +1 @@ +FiX: In Function os.path.normcase in Lib/posixpath.py docstring states path remains unchanged on POSIX Systems which is not true. Updated Docstring and documentation