Skip to content

pathlib.Path.rglob("") raises IndexError in Python 3.11b1 #92550

Closed

Description

@bluetech

Bug report

Python 3.11b1 raises with the following given path.rglob("") where path is a pathlib.Path. This worked in previous versions (and also in whichever previous alphas GitHub Actions used).

    def rglob(self, pattern):
        """Recursively yield all existing files (of any kind, including
        directories) matching the given relative pattern, anywhere in
        this subtree.
        """
        sys.audit("pathlib.Path.rglob", self, pattern)
        drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
        if drv or root:
            raise NotImplementedError("Non-relative patterns are unsupported")
>       if pattern[-1] in (self._flavour.sep, self._flavour.altsep):
E       IndexError: string index out of range

rglob is documented as

This is like calling Path.glob() with “**/” added in front of the given relative pattern

so I think "" is a valid input to rglob().

Pretty sure this was introduced in ea2f5bc/#10349.

Your environment

  • CPython versions tested on: 3.11b1
  • Operating system and architecture: Both Linux and Windows.

Cross-ref: pytest-dev/pytest#9930

Activity

  1. AlexWaygood commented on May 9, 2022

    @AlexWaygood
    Member

    Pretty sure this was introduced in ea2f5bc/https://github.com/python/cpython/pull/10349.

    Yes, looks like it:

    C:\Users\alexw\coding\cpython>git checkout ea2f5bcda1a392804487e6883be89fbad38a01a5
    HEAD is now at ea2f5bcda1 bpo-22276: Change pathlib.Path.glob not to ignore trailing path separator (GH-10349)
    
    C:\Users\alexw\coding\cpython>python
    Running Debug|x64 interpreter...
    Python 3.12.0a0 (main, May  9 2022, 11:32:16) [MSC v.1931 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pathlib
    >>> next(pathlib.Path().rglob(""))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Users\alexw\coding\cpython\Lib\pathlib.py", line 963, in rglob
        if pattern[-1] in (self._flavour.sep, self._flavour.altsep):
           ~~~~~~~^^^^
    IndexError: string index out of range
    >>> exit()
    
    C:\Users\alexw\coding\cpython>git checkout HEAD~1
    Previous HEAD position was ea2f5bcda1 bpo-22276: Change pathlib.Path.glob not to ignore trailing path separator (GH-10349)
    HEAD is now at 0ef8d921f5 gh-91603: Speed up isinstance/issubclass on union types (GH-91631)
    
    C:\Users\alexw\coding\cpython>python
    Running Debug|x64 interpreter...
    Python 3.12.0a0 (main, May  9 2022, 11:32:16) [MSC v.1931 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pathlib
    >>> next(pathlib.Path().rglob(""))
    WindowsPath('.')
    >>>
    

    cc. @brettcannon, @barneygale

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    3.11only security fixes
    3.12only security fixes
    on May 9, 2022
  3. added 3 commits that reference this issue on May 10, 2022
  4. serhiy-storchaka commented on May 10, 2022

    @serhiy-storchaka
    Member

    Why the whole feature was removed instead of applying a simple fix? @brettcannon

  5. added 2 commits that reference this issue on May 10, 2022
  6. added 3 commits that reference this issue on May 10, 2022
  7. added a commit that references this issue on May 11, 2022
  8. added a commit that references this issue on May 11, 2022
  9. added a commit that references this issue on May 11, 2022
  10. added a commit that references this issue on Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

3.11only security fixes3.12only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions