diff --git a/Lib/difflib.py b/Lib/difflib.py index 4bba9e7ea5cfa8..e674bb510f4c69 100644 --- a/Lib/difflib.py +++ b/Lib/difflib.py @@ -365,6 +365,12 @@ def find_longest_match(self, alo=0, ahi=None, blo=0, bhi=None): ahi = len(a) if bhi is None: bhi = len(b) + + # Check if both sequences are the same before executing rest of the + # method. + if a[alo:ahi] == b[blo:bhi]: + return Match(alo, blo, len(a[alo:ahi])) + besti, bestj, bestsize = alo, blo, 0 # find longest junk-free match # during an iteration of the loop, j2len[j] = length of longest diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-04-06-17-31-44.gh-issue-132166.tbo9yR.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-06-17-31-44.gh-issue-132166.tbo9yR.rst new file mode 100644 index 00000000000000..23e3eaadcca61b --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-06-17-31-44.gh-issue-132166.tbo9yR.rst @@ -0,0 +1,5 @@ +Add checking if sequences ``a[alo:ahi]`` and ``b[blo:bhi]`` are the same on the +beginning of the method find_longest_match in SequenceMatcher. For identical +sequences there is no reason to run whole logic when simple check can be done. +It appears to fix issue when comparing two slightly different strings ends up +with waiting forever for the result.