Skip to content

Adding a testcase for the bug in find_longest_match #44696

Closed

Description

@rtvd
mannequin
BPO 1678339
Nosy @birkenfeld
Files
  • python-bug-54269-testcase.diff: A testcase for the bug difflib.SequenceMatcher.find_longest_match() wrong result #43714
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2007-03-19.02:01:22.000>
    created_at = <Date 2007-03-11.15:11:23.000>
    labels = ['tests']
    title = 'Adding a testcase for the bug in find_longest_match'
    updated_at = <Date 2007-03-19.02:01:22.000>
    user = 'https://bugs.python.org/rtvd'

    bugs.python.org fields:

    activity = <Date 2007-03-19.02:01:22.000>
    actor = 'jimjjewett'
    assignee = 'none'
    closed = True
    closed_date = None
    closer = None
    components = ['Tests']
    creation = <Date 2007-03-11.15:11:23.000>
    creator = 'rtvd'
    dependencies = []
    files = ['7860']
    hgrepos = []
    issue_num = 1678339
    keywords = ['patch']
    message_count = 4.0
    messages = ['52171', '52172', '52173', '52174']
    nosy_count = 4.0
    nosy_names = ['georg.brandl', 'jimjjewett', 'rtvd', 'paulhankin']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue1678339'
    versions = ['Python 2.6']

    Activity

    1. rtvd commented on Mar 11, 2007

      rtvdmannequin
      MannequinAuthor

      The find_longest_match method in the difflib's SequenceMatcher has a bug.

      The bug is in turn caused by a problem with creating a b2j mapping which should contain a list of indices for each of the list elements in b. However, when the b2j mapping is being created (this is being done in __chain_b method in the SequenceMatcher) the mapping becomes broken. The cause of this is that for the frequently used elements the list of indices is removed and the element is being enlisted in the populardict mapping.

      The test case tries to match two strings like:

      abbbbbb.... and ...bbbbbbc

      The number of b is equal and the find_longest_match should have returned the proper amount. However, in case the number of "b"s is large enough, the method reports that the length of the longest common substring is 0. It simply can't find it.

      A bug was raised some time ago on this matter. It's ID is 1528074.

      I tried to fix this bug but as a result, the performance drops by a factor of 5-10. Perhaps someone more familiar with Python can find a good fix. For the time being I send this test case (which is broken until the bug is fixed) and I'm going to send a fix next (but the fix makes the method run quite slowly).

      The patch diff attached was made against the trunk version of Python.

    2. added
      tests
      on Mar 11, 2007
    3. added
      tests
      on Mar 11, 2007
    4. paulhankin commented on Mar 18, 2007

      paulhankinmannequin
      Mannequin

      The test correctly fails, and looks right.

    5. birkenfeld commented on Mar 18, 2007

      @birkenfeld
      Member

      Added the testcase to Lib/test/outstanding_bugs.py. It will be moved to test_difflib as soon as the bug is fixed.

    6. jimjjewett commented on Mar 19, 2007

      jimjjewettmannequin
      Mannequin

      I think the bug is documentation only.

      According to the comments (but not the docstring) find_longest_match returns the longest _interesting_ match. A single element appearing too often is likely to cause spurious matches, and is therefore not interesting.

      I do agree that this should be noted more prominently, so people don't try things like comparing text strings letter-by-letter (where 1% is far too low a threshhold for a 26-character alphabet).

      And yes, the comments on popular are correct -- it ignores elements which constitute *more* than 1%.

      I recommend closing this set of tracker items. If you could submit changes to the documentation (docstrings and/or help files; maybe even the comments), I would recommend applying them.

    7. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      tests

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions