[Python-checkins] cpython (merge 3.2 -> default): #11763: merge with 3.2.
ezio.melotti
python-checkins at python.org
Wed Apr 27 09:21:44 CEST 2011
http://hg.python.org/cpython/rev/df154c872b0c
changeset: 69591:df154c872b0c
parent: 69587:d3af2a2b621b
parent: 69590:b316019638df
user: Ezio Melotti <ezio.melotti at gmail.com>
date: Wed Apr 27 10:21:51 2011 +0300
summary:
#11763: merge with 3.2.
files:
Lib/unittest/case.py | 8 ++++
Lib/unittest/test/test_case.py | 36 ++++++++++++++++++++++
Misc/NEWS | 3 +
3 files changed, 47 insertions(+), 0 deletions(-)
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -263,6 +263,10 @@
maxDiff = 80*8
+ # If a string is longer than _diffThreshold, use normal comparison instead
+ # of difflib. See #11763.
+ _diffThreshold = 2**16
+
# Attribute used by TestSuite for classSetUp
_classSetupFailed = False
@@ -1006,6 +1010,10 @@
self.assertIsInstance(second, str, '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)
firstlines = first.splitlines(True)
secondlines = second.splitlines(True)
if len(firstlines) == 1 and first.strip('\r\n') == first:
diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py
--- a/Lib/unittest/test/test_case.py
+++ b/Lib/unittest/test/test_case.py
@@ -720,6 +720,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 = '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 = '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 testAssertCountEqual(self):
a = object()
self.assertCountEqual([1, 2, 3], [3, 2, 1])
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -123,6 +123,9 @@
Library
-------
+- Issue #11763: don't use difflib in TestCase.assertMultiLineEqual if the
+ strings are too long.
+
- Issue #11236: getpass.getpass responds to ctrl-c or ctrl-z on terminal.
- Issue #11856: Speed up parsing of JSON numbers.
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list