Skip to content

Commit bce45bc

Browse files
donBarbosAA-Turnerpicnixzhugovktim-one
authored
gh-130167: Improve difflib.IS_LINE_JUNK performance by using string methods (#130170)
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>
1 parent b99d970 commit bce45bc

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

Lib/difflib.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,11 +1038,9 @@ def _qformat(self, aline, bline, atags, btags):
10381038
# remaining is that perhaps it was really the case that " volatile"
10391039
# was inserted after "private". I can live with that <wink>.
10401040

1041-
import re
1042-
1043-
def IS_LINE_JUNK(line, pat=re.compile(r"\s*(?:#\s*)?$").match):
1041+
def IS_LINE_JUNK(line, pat=None):
10441042
r"""
1045-
Return True for ignorable line: iff `line` is blank or contains a single '#'.
1043+
Return True for ignorable line: if `line` is blank or contains a single '#'.
10461044
10471045
Examples:
10481046
@@ -1054,6 +1052,11 @@ def IS_LINE_JUNK(line, pat=re.compile(r"\s*(?:#\s*)?$").match):
10541052
False
10551053
"""
10561054

1055+
if pat is None:
1056+
# Default: match '#' or the empty string
1057+
return line.strip() in '#'
1058+
# Previous versions used the undocumented parameter 'pat' as a
1059+
# match function. Retain this behaviour for compatibility.
10571060
return pat(line) is not None
10581061

10591062
def IS_CHARACTER_JUNK(ch, ws=" \t"):
@@ -2027,7 +2030,6 @@ def make_table(self,fromlines,tolines,fromdesc='',todesc='',context=False,
20272030
replace('\1','</span>'). \
20282031
replace('\t','&nbsp;')
20292032

2030-
del re
20312033

20322034
def restore(delta, which):
20332035
r"""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve speed of :func:`difflib.IS_LINE_JUNK`. Patch by Semyon Moroz.

0 commit comments

Comments
 (0)