[Python-checkins] cpython (3.1): #11763: don't use difflib in TestCase.assertMultiLineEqual if the strings are
ezio.melotti
python-checkins at python.org
Wed Apr 27 09:21:37 CEST 2011
http://hg.python.org/cpython/rev/04e64f77c6c7
changeset: 69589:04e64f77c6c7
branch: 3.1
parent: 69569:f799530dbde7
user: Ezio Melotti <ezio.melotti at gmail.com>
date: Wed Apr 27 10:17:34 2011 +0300
summary:
#11763: don't use difflib in TestCase.assertMultiLineEqual if the strings are too long.
files:
Lib/test/test_unittest.py | 35 +++++++++++++++++++++++++++
Lib/unittest.py | 7 +++++
Misc/NEWS | 3 ++
3 files changed, 45 insertions(+), 0 deletions(-)
diff --git a/Lib/test/test_unittest.py b/Lib/test/test_unittest.py
--- a/Lib/test/test_unittest.py
+++ b/Lib/test/test_unittest.py
@@ -2719,6 +2719,41 @@
# no fair testing ourself with ourself, use assertEqual..
self.assertEqual(sample_text_error, str(e))
+ 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)
+ try:
+ self.assertMultiLineEqual(s + 'a', s + 'b')
+ except self.failureException as exc:
+ err_msg = str(exc)
+ else:
+ self.fail('assertEqual unexpectedly succeeded')
+ self.assertIn('^', err_msg)
+ self.assertMultiLineEqual(s + 'a', s + 'a')
+
+ # over the threshold: diff not used and marker (^) not in error message
+ s = 'x' * (2**9)
+ s1, s2 = s + 'a', s + 'b'
+ try:
+ self.assertMultiLineEqual(s1, s2)
+ except self.failureException as exc:
+ err_msg = str(exc)
+ else:
+ self.fail('assertEqual unexpectedly succeeded')
+ self.assertNotIn('^', err_msg)
+ self.assertEqual(err_msg, '%r != %r' % (s1, s2))
+ self.assertMultiLineEqual(s + 'a', s + 'a')
+
def testAssertIsNone(self):
self.assertIsNone(None)
self.assertRaises(self.failureException, self.assertIsNone, False)
diff --git a/Lib/unittest.py b/Lib/unittest.py
--- a/Lib/unittest.py
+++ b/Lib/unittest.py
@@ -346,6 +346,9 @@
longMessage = False
+ # If a string is longer than _diffThreshold, use normal comparison instead
+ # of difflib. See #11763.
+ _diffThreshold = 2**16
def __init__(self, methodName='runTest'):
"""Create an instance of the class that will use the named test
@@ -955,6 +958,10 @@
'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)
standardMsg = '\n' + ''.join(difflib.ndiff(first.splitlines(True), second.splitlines(True)))
self.fail(self._formatMessage(msg, standardMsg))
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -61,6 +61,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 #11768: The signal handler of the signal module only calls
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list