]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-130167: Improve ``difflib.IS_LINE_JUNK`` performance by using string methods ...
authorSemyon Moroz <donbarbos@proton.me>
Thu, 1 May 2025 04:11:36 +0000 (08:11 +0400)
committerGitHub <noreply@github.com>
Thu, 1 May 2025 04:11:36 +0000 (04:11 +0000)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Tim Peters <tim.peters@gmail.com>
Lib/difflib.py
Misc/NEWS.d/next/Library/2025-02-16-06-25-01.gh-issue-130167.kUg7Rc.rst [new file with mode: 0644]

index 4bba9e7ea5cfa828d8a0feb4843ce6f8e01a80db..f1f4e62514a7bda3797d6f8e3d016169b8479bb1 100644 (file)
@@ -1038,11 +1038,9 @@ class Differ:
 # remaining is that perhaps it was really the case that " volatile"
 # was inserted after "private".  I can live with that <wink>.
 
-import re
-
-def IS_LINE_JUNK(line, pat=re.compile(r"\s*(?:#\s*)?$").match):
+def IS_LINE_JUNK(line, pat=None):
     r"""
-    Return True for ignorable line: iff `line` is blank or contains a single '#'.
+    Return True for ignorable line: if `line` is blank or contains a single '#'.
 
     Examples:
 
@@ -1054,6 +1052,11 @@ def IS_LINE_JUNK(line, pat=re.compile(r"\s*(?:#\s*)?$").match):
     False
     """
 
+    if pat is None:
+        # Default: match '#' or the empty string
+        return line.strip() in '#'
+   # Previous versions used the undocumented parameter 'pat' as a
+   # match function. Retain this behaviour for compatibility.
     return pat(line) is not None
 
 def IS_CHARACTER_JUNK(ch, ws=" \t"):
@@ -2027,7 +2030,6 @@ class HtmlDiff(object):
                      replace('\1','</span>'). \
                      replace('\t','&nbsp;')
 
-del re
 
 def restore(delta, which):
     r"""
diff --git a/Misc/NEWS.d/next/Library/2025-02-16-06-25-01.gh-issue-130167.kUg7Rc.rst b/Misc/NEWS.d/next/Library/2025-02-16-06-25-01.gh-issue-130167.kUg7Rc.rst
new file mode 100644 (file)
index 0000000..3d39708
--- /dev/null
@@ -0,0 +1 @@
+Improve speed of :func:`difflib.IS_LINE_JUNK`. Patch by Semyon Moroz.