This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author marienz
Recipients marienz
Date 2010-09-29.14:32:17
SpamBayes Score 9.756291e-05
Marked as misclassified No
Message-id <1285770739.15.0.805040100323.issue9985@psf.upfronthosting.co.za>
In-reply-to
Content
SequenceMatcher caches the result of get_matching_blocks and get_opcodes. There are some problems with this:

What get_matching_blocks caches is a list of tuples. The first call does not return that list: it returns map(Match._make, self.matching_blocks) (converting the tuples to namedtuples). Subsequent calls just return self.matching_blocks directly. Especially in python 3 and up this is weird, since the first call returns a map object while later calls return a list.

This caching behavior is not documented, so calling code may mutate the returned list. One example of calling code is difflib itself: get_grouped_opcodes mutates the result of get_opcodes (a cached list). I am not sure if the right fix is to have get_grouped_opcodes copy before it mutates or to have get_opcodes return a copy.

Snippet demonstrating both bugs:

matcher = difflib.SequenceMatcher(a='aaaaaaaabc', b='aaaaaaaadc')
print(list(matcher.get_matching_blocks()))
# This should print the same thing, but it does not:
print(list(matcher.get_matching_blocks()))

print(matcher.get_opcodes())
print(list(matcher.get_grouped_opcodes()))
# This should print the same thing as the previous get_opcodes()
# list, but it does not:
print(matcher.get_opcodes())
History
Date User Action Args
2010-09-29 14:32:19marienzsetrecipients: + marienz
2010-09-29 14:32:19marienzsetmessageid: <1285770739.15.0.805040100323.issue9985@psf.upfronthosting.co.za>
2010-09-29 14:32:17marienzlinkissue9985 messages
2010-09-29 14:32:17marienzcreate