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.PureWindowsPath.match() mishandles path anchors #101360

Closed
barneygale opened this issue Jan 26, 2023 · 2 comments
Closed

pathlib.PureWindowsPath.match() mishandles path anchors #101360

barneygale opened this issue Jan 26, 2023 · 2 comments
Labels
3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes OS-windows topic-pathlib type-bug An unexpected behavior, bug, or error

Comments

@barneygale
Copy link
Contributor

barneygale commented Jan 26, 2023

pathlib.PurePath.match() doesn't use fnmatch() to match path anchors, contrary to the documentation. Instead, it considers the drive and root of the pattern separately; if either (or both) is specified in the pattern, it must exactly match the corresponding drive/root in the path.

This results in the following:

>>> from pathlib import PureWindowsPath as P
>>> P('c:/b.py').match('*:/*.py')
False
>>> P('c:/b.py').match('c:*.py')
True
>>> P('c:/b.py').match('/*.py')
True
>>> P('//some/share/a.py').match('//*/*/*.py')
False
>>> P('//some/share/a.py').match('/*.py')
True

All of these results are wrong IMO.

Linked PRs

@barneygale barneygale added type-bug An unexpected behavior, bug, or error 3.11 only security fixes 3.10 only security fixes 3.12 bugs and security fixes topic-pathlib labels Jan 26, 2023
@barneygale
Copy link
Contributor Author

barneygale commented Jan 27, 2023

I think I know why this behaviour is here: pathlib originally wouldn't parse patterns like *:/ because it required drive letters to be alphabetic. match() worked around this by treating empty drives like *.

The alphabetic drive requirement was removed in 2ba0fd5 (3.12).

barneygale added a commit to barneygale/cpython that referenced this issue Jan 27, 2023
Use `fnmatch` to match path and pattern anchors, just as we do for other
path parts. This allows patterns such as `'*:/Users/*'` to be matched.
barneygale added a commit to barneygale/cpython that referenced this issue Jan 27, 2023
Use `fnmatch` to match path and pattern anchors, just as we do for other
path parts. This allows patterns such as `'*:/Users/*'` to be matched.
barneygale added a commit to barneygale/cpython that referenced this issue Jan 29, 2023
zooba pushed a commit that referenced this issue Feb 17, 2023
…-101363)

Use `fnmatch` to match path and pattern anchors, just as we do for other
path parts. This allows patterns such as `'*:/Users/*'` to be matched.
@barneygale
Copy link
Contributor Author

Fixed in #101363 / d401b20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes OS-windows topic-pathlib type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants