8000 Fix support for newlines · python/cpython@a846279 · GitHub
[go: up one dir, main page]

Skip to content

Commit a846279

Browse files
committed
Fix support for newlines
1 parent 9a43c7f commit a846279

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

Lib/pathlib.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,23 @@ def _is_wildcard_pattern(pat):
6464
# Globbing helpers
6565
#
6666

67+
68+
_SWAP_SLASH_AND_NEWLINE = str.maketrans({'/': '\n', '\n': '/'})
69+
70+
6771
@functools.lru_cache()
6872
def _make_matcher(path_cls, pattern, recursive):
6973
pattern = path_cls(pattern)
7074
if not pattern._parts:
7175
raise ValueError("empty pattern")
7276
result = [r'\A' if pattern._drv or pattern._root else '^']
73 8000 -
for line in pattern._lines_normcase:
77+
for line in pattern._lines_normcase.splitlines(keepends=True):
7478
if recursive:
7579
if line == '**\n':
76-
result.append('(.*\n)*')
80+
result.append(r'[\S\s]*^')
81+
continue
82+
elif line == '**':
83+
result.append(r'[\S\s]*')
7784
continue
7885
elif '**' in line:
7986
raise ValueError("Invalid pattern: '**' can only be an entire path component")
@@ -659,14 +666,16 @@ def is_reserved(self):
659666

660667
@property
661668
def _lines_normcase(self):
662-
return [f'{part}\n' for part in self._parts_normcase]
669+
path = self._flavour.normcase(self.as_posix())
670+
return path.translate(_SWAP_SLASH_AND_NEWLINE)
663671

664672
def match(self, path_pattern, recursive=False):
665673
"""
666674
Return True if this path matches the given pattern.
667675
"""
668676
matcher = _make_matcher(type(self), path_pattern, recursive)
669-
return matcher.search(''.join(self._lines_normcase)) is not None
677+
return matcher.search(self._lines_normcase) is not None
678+
670679

671680
# Can't subclass os.PathLike from PurePath and keep the constructor
672681
# optimizations in PurePath._parse_args().

Lib/test/test_pathlib.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ def test_match_common(self):
331331
self.assertTrue(P('/a/b/c.py').match('/**/*.py', recursive=True))
332332
self.assertTrue(P('/a/b/c.py').match('/a/**/*.py', recursive=True))
333333
self.assertTrue(P('/a/b/c.py').match('/a/b/**/*.py', recursive=True))
334-
self.assertTrue(P('/a/b/c.py').match('**/a/b/c.py/**', recursive=True))
335334
self.assertTrue(P('/a/b/c.py').match('/**/**/**/**/*.py', recursive=True))
336335
self.assertFalse(P('c.py').match('**/a.py', recursive=True))
337336
self.assertFalse(P('c.py').match('c/**', recursive=True))

0 commit comments

Comments
 (0)
0