diff -r f961e9179998 Lib/unittest/case.py --- a/Lib/unittest/case.py Mon Apr 04 01:47:52 2011 +0200 +++ b/Lib/unittest/case.py Tue Apr 05 02:28:32 2011 +0300 @@ -169,6 +169,10 @@ maxDiff = 80*8 + # If a string is longer than _diffThreshold, use normal comparison instead + # of difflib. + _diffThreshold = 2**16 + # Attribute used by TestSuite for classSetUp _classSetupFailed = False @@ -899,7 +903,11 @@ self.assertIsInstance(second, basestring, 'Second argument is not a string') - if first != second: + # don't use difflib if the strings are too long + if (len(first) > self._diffThreshold or + len(second) > self._diffThreshold): + self._baseAssertEqual(first, second, msg) + elif first != second: firstlines = first.splitlines(True) secondlines = second.splitlines(True) if len(firstlines) == 1 and first.strip('\r\n') == first: diff -r f961e9179998 Lib/unittest/test/test_case.py --- a/Lib/unittest/test/test_case.py Mon Apr 04 01:47:52 2011 +0200 +++ b/Lib/unittest/test/test_case.py Tue Apr 05 02:28:32 2011 +0300 @@ -667,6 +667,42 @@ else: self.fail('assertMultiLineEqual did not fail') + def testAssertEqual_diffThreshold(self): + # check threshold value + self.assertEqual(self._diffThreshold, 2**16) + # disable madDiff to get diff markers + self.maxDiff = None + + # set a lower threshold value and add a cleanup to restore it + old_threshold = self._diffThreshold + self._diffThreshold = 2**8 + self.addCleanup(lambda: setattr(self, '_diffThreshold', old_threshold)) + + # under the threshold: diff marker (^) in error message + s = u'x' * (2**7) + with self.assertRaises(self.failureException) as cm: + self.assertEqual(s + 'a', s + 'b') + self.assertIn('^', str(cm.exception)) + self.assertEqual(s + 'a', s + 'a') + + # over the threshold: diff not used and marker (^) not in error message + s = u'x' * (2**9) + # if the path that uses difflib is taken, _truncateMessage will be + # called -- replace it with explodingTruncation to verify that this + # doesn't happen + def explodingTruncation(message, diff): + raise SystemError('this should not be raised') + old_truncate = self._truncateMessage + self._truncateMessage = explodingTruncation + self.addCleanup(lambda: setattr(self, '_truncateMessage', old_truncate)) + + s1, s2 = s + 'a', s + 'b' + with self.assertRaises(self.failureException) as cm: + self.assertEqual(s1, s2) + self.assertNotIn('^', str(cm.exception)) + self.assertEqual(str(cm.exception), '%r != %r' % (s1, s2)) + self.assertEqual(s + 'a', s + 'a') + def testAssertItemsEqual(self): a = object() self.assertItemsEqual([1, 2, 3], [3, 2, 1])