Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
raek mannequin opened this issue Oct 25, 2021 · 5 comments
Closed

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

raek mannequin opened this issue Oct 25, 2021 · 5 comments
Assignees
Labels
3.12 bugs and security fixes stdlib Python modules in the Lib dir topic-pathlib type-bug An unexpected behavior, bug, or error

Comments

@raek
Copy link
Mannequin

raek mannequin commented Oct 25, 2021

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']

    @raek
    Copy link
    Mannequin Author

    raek mannequin commented Oct 25, 2021

    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?

    @raek raek mannequin added 3.8 only security fixes 3.10 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Oct 25, 2021
    @akulakov
    Copy link
    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.

    @akulakov
    Copy link
    Contributor

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

    @akulakov
    Copy link
    Contributor

    By the way note that path.glob('**/my_symlink') also does return the dangling symlink match. And glob.glob('my_symlink') also returns a dangling symlink.

    @akulakov akulakov added 3.11 only security fixes and removed 3.8 only security fixes labels Jan 23, 2022
    @akulakov akulakov 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 Jan 23, 2022
    @akulakov akulakov added 3.11 only security fixes and removed 3.8 only security fixes labels Jan 23, 2022
    @akulakov akulakov 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 Jan 23, 2022
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    barneygale added a commit that referenced this issue May 3, 2023
    … for precise match (GH-29655)
    
    Co-authored-by: Barney Gale <barney.gale@gmail.com>
    @barneygale
    Copy link
    Contributor

    Fixed in 3.12: #29655 / af886ff

    @barneygale barneygale removed 3.11 only security fixes 3.10 only security fixes labels May 3, 2023
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.12 bugs and security fixes stdlib Python modules in the Lib dir topic-pathlib type-bug An unexpected behavior, bug, or error
    Projects
    Development

    No branches or pull requests

    3 participants