]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-111259: Optimize recursive wildcards in pathlib (GH-111303)
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 26 Oct 2023 15:07:06 +0000 (18:07 +0300)
committerGitHub <noreply@github.com>
Thu, 26 Oct 2023 15:07:06 +0000 (18:07 +0300)
Regular expression pattern `(?s:.)` is much faster than `[\s\S]`.

Lib/pathlib.py
Misc/NEWS.d/next/Library/2023-10-25-11-13-35.gh-issue-111259.z7ndeA.rst [new file with mode: 0644]

index 5c1c71ecec280532b280d9e5db3102d4e6c45453..e3eecc3b6d73e3efaefa70898ed42b4fde0135c5 100644 (file)
@@ -124,13 +124,13 @@ def _compile_pattern_lines(pattern_lines, case_sensitive):
         elif part == '*':
             part = r'.+'
         elif part == '**\n':
-            # '**/' component: we use '[\s\S]' rather than '.' so that path
+            # '**/' component: we use '(?s:.)' rather than '.' so that path
             # separators (i.e. newlines) are matched. The trailing '^' ensures
             # we terminate after a path separator (i.e. on a new line).
-            part = r'[\s\S]*^'
+            part = r'(?s:.)*^'
         elif part == '**':
             # '**' component.
-            part = r'[\s\S]*'
+            part = r'(?s:.)*'
         elif '**' in part:
             raise ValueError("Invalid pattern: '**' can only be an entire path component")
         else:
diff --git a/Misc/NEWS.d/next/Library/2023-10-25-11-13-35.gh-issue-111259.z7ndeA.rst b/Misc/NEWS.d/next/Library/2023-10-25-11-13-35.gh-issue-111259.z7ndeA.rst
new file mode 100644 (file)
index 0000000..4b597f5
--- /dev/null
@@ -0,0 +1 @@
+Optimize recursive wildcards in :mod:`pathlib`.