-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
gh-130942: Fix path seperator matched in character ranges for glob.translate #130989
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
base: main
Are you sure you want to change the base?
Changes from all commits
be37b54
b874745
6990566
cc03a6d
cea1f5e
5251d75
dd1b155
e8b3559
9f461a5
4820018
c7f6d87
d5748b8
95b4ccf
cdfcf47
3929b06
e5abc80
93c3092
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -262,8 +262,6 @@ def escape(pathname): | |
_special_parts = ('', '.', '..') | ||
_dir_open_flags = os.O_RDONLY | getattr(os, 'O_DIRECTORY', 0) | ||
_no_recurse_symlinks = object() | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert. |
||
def translate(pat, *, recursive=False, include_hidden=False, seps=None): | ||
"""Translate a pathname with shell wildcards to a regular expression. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -456,6 +456,14 @@ def test_translate_matching(self): | |
self.assertIsNone(match(os.path.join('foo', '.bar'))) | ||
self.assertIsNotNone(match(os.path.join('foo', 'bar.txt'))) | ||
self.assertIsNone(match(os.path.join('foo', '.bar.txt'))) | ||
match = re.compile(glob.translate('foo[%-0]bar', recursive=True)).match | ||
self.assertIsNone(match(os.path.join('foo', 'bar'))) | ||
match = re.compile(glob.translate('foo?bar', recursive=True)).match | ||
self.assertIsNone(match('foo/bar')) | ||
match = re.compile(glob.translate('foo.', recursive=True)).match | ||
self.assertIsNone(match('foo/')) | ||
match = re.compile(glob.translate('foo*', recursive=True)).match | ||
self.assertIsNone(match('foo/')) | ||
|
||
def test_translate(self): | ||
def fn(pat): | ||
|
@@ -513,7 +521,17 @@ def fn(pat): | |
return glob.translate(pat, recursive=True, include_hidden=True, seps=['/', '\\']) | ||
self.assertEqual(fn('foo/bar\\baz'), r'(?s:foo[/\\]bar[/\\]baz)\Z') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More generally, can you upodate |
||
self.assertEqual(fn('**/*'), r'(?s:(?:.+[/\\])?[^/\\]+)\Z') | ||
|
||
self.assertEqual(fn('foo[!a]bar'), r'(?s:foo[^/\\^a]bar)\Z') | ||
self.assertEqual(fn('foo[%-0]bar'), r'(?s:foo(?![/\\])[%-0]bar)\Z') | ||
self.assertEqual(fn('foo[%-0][1-9]bar'), r'(?s:foo(?![/\\])[%-0][1-9]bar)\Z') | ||
self.assertEqual(fn('foo[0-%]bar'), r'(?s:foo(?!)bar)\Z') | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertEqual(fn('foo[^-'), r'(?s:foo\[\^\-)\Z') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need also a test case with multiple ranges and incomplete ones, e.g., |
||
self.assertEqual(fn('foo[/-/]bar'), r'(?s:foo\[[/\\]\-[/\\]\]bar)\Z') | ||
self.assertEqual(fn('foo[%-/]bar'), r'(?s:foo\[%\-[/\\]\]bar)\Z') | ||
self.assertEqual(fn('foo[/]bar'), r'(?s:foo\[[/\\]\]bar)\Z') | ||
self.assertEqual(fn('foo[%-0][0-%[%-0]bar'), r'(?s:foo(?![/\\])[%-0](?![/\\])[\[%-0]bar)\Z') | ||
self.assertEqual(fn('foo?'), r'(?s:foo[^/\\])\Z') | ||
self.assertEqual(fn('foo.'), r'(?s:foo\.)\Z') | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.. versionchanged:: next | ||
:func:`glob.translate` now correctly handles ranges implicitly containing path | ||
separators (for instance, ``[%-0]`` contains ``/``) by adding either a negative | ||
lookahead (``(?!/)``) or by not including the path separator (``^/``). In addition, | ||
ranges including path separator literals are now correctly escaped, as specified by | ||
POSIX specifications. | ||
.. versionchanged:: next | ||
:func:`fnmatch.translate` does not treat path separator characters as having any | ||
special meaning at all, so it still matches ranges implicitly containing path | ||
separators (for instance, ``[%-0]`` contains ``/``) and ranges explicitly | ||
containing path separators (for instance, ``[/-/]`` contains ``/``). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.