Title: pathlib.Path.glob() does not list dangling symlink when pattern is the exact filenane
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: andrei.avk Nosy List: andrei.avk, kj, raek
Priority: normal Keywords: patch

Created on 2021-10-25 19:20 by raek, last changed 2021-11-20 04:39 by andrei.avk.

Pull Requests
URL Status Linked Edit
PR 29655 open andrei.avk, 2021-11-20 03:49
Messages (4)
msg404996 - (view) Author: Rasmus Bondesson (raek) Date: 2021-10-25 19:20
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:

    >>> import pathlib
    >>> list(pathlib.Path(".").glob("my_symlink"))
    >>> list(pathlib.Path(".").glob("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?
msg406643 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-11-20 03:59
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.
msg406644 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-11-20 04:02
Rasmus: thanks for the report, it does seem like a bug to me.
msg406645 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-11-20 04:39
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.
Date User Action Args
2021-11-20 04:39:55andrei.avksetmessages: + msg406645
2021-11-20 04:02:48andrei.avksetmessages: + msg406644
2021-11-20 03:59:36andrei.avksetassignee: andrei.avk

messages: + msg406643
nosy: + kj
2021-11-20 03:49:48andrei.avksetkeywords: + patch
nosy: + andrei.avk

pull_requests: + pull_request27897
stage: patch review
2021-10-25 19:20:55raekcreate