Skip to content

pathlib.Path.glob() does not list dangling symlink when pattern is the exact filename #89769

Description

@raek
mannequin
BPO 45606
Nosy @Fidget-Spinner, @akulakov, @raek
PRs
  • GH-89769: Pathlib - do not follow links when checking for precise glob match #29655
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/akulakov'
    closed_at = None
    created_at = <Date 2021-10-25.19:20:55.106>
    labels = ['type-bug', 'library', '3.9', '3.10', '3.11']
    title = 'pathlib.Path.glob() does not list dangling symlink when pattern is the exact filename'
    updated_at = <Date 2022-01-23.06:57:12.913>
    user = 'https://github.com/raek'

    bugs.python.org fields:

    activity = <Date 2022-01-23.06:57:12.913>
    actor = 'andrei.avk'
    assignee = 'andrei.avk'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2021-10-25.19:20:55.106>
    creator = 'raek'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45606
    keywords = ['patch']
    message_count = 4.0
    messages = ['404996', '406643', '406644', '406645']
    nosy_count = 3.0
    nosy_names = ['kj', 'andrei.avk', 'raek']
    pr_nums = ['29655']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue45606'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    Activity

    1. raek commented on Oct 25, 2021

      raekmannequin
      MannequinAuthor

      Create a symlink that points to file that doesn't exist:

      ln -s /nonexisting_file my_symlink
      

      Then try to glob for that symlink from Python using pathlib:

          python3
          >>> import pathlib
          >>> list(pathlib.Path(".").glob("my_symlink"))
          []
          >>> list(pathlib.Path(".").glob("my_symlink*"))
          [PosixPath('my_symlink')]

      I'm a bit surprised that these two globs do not return the same results. Personally I would expect both to find the symlink.

      Is this behaviour a bug or is it intentional?

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Oct 25, 2021
    3. akulakov commented on Nov 20, 2021

      @akulakov
      Contributor

      The issue is that _PreciseSelector follows the symlink when it checks if a path exists before yielding it as a result.

      I've put up a PR with a fix; I've also added a follow_symlinks arg to exists() method because it seems more logical to be able to test if a path exists via the same method rather than having to also remember and to check for it being a symlink.

      I will add docs and news a bit later today or tomorrow.

    4. akulakov commented on Nov 20, 2021

      @akulakov
      Contributor

      Rasmus: thanks for the report, it does seem like a bug to me.

    5. 3 remaining items

    6. changed the title [-]pathlib.Path.glob() does not list dangling symlink when pattern is the exact filenane[/-] [+]pathlib.Path.glob() does not list dangling symlink when pattern is the exact filename[/+] on Jan 23, 2022
    7. added
      3.11only security fixes
      and removed on Jan 23, 2022
    8. changed the title [-]pathlib.Path.glob() does not list dangling symlink when pattern is the exact filenane[/-] [+]pathlib.Path.glob() does not list dangling symlink when pattern is the exact filename[/+] on Jan 23, 2022
    9. transferred this issue fromon Apr 10, 2022
    10. added a commit that references this issue on May 3, 2023
    11. barneygale commented on May 3, 2023

      @barneygale
      Contributor

      Fixed in 3.12: #29655 / af886ff

    12. added
      3.12only security fixes
      and removed
      3.11only security fixes
      on May 3, 2023
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    3.12only security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-pathlibtype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions