Fix unittest.TestCase.assertDictContainsSubset so it can't die with unicode issues when constructing failure messages. Issue 7956

This commit is contained in:
Michael Foord 2010-02-18 21:37:07 +00:00
parent 225a099fe5
commit c2294dd6ba
2 changed files with 7 additions and 9 deletions

View File

@ -2573,13 +2573,10 @@ class Test_TestCase(TestCase, TestEquality, TestHashing):
with self.assertRaises(self.failureException): with self.assertRaises(self.failureException):
self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1}) self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1})
@unittest.expectedFailure
def test_crazy(self):
one = ''.join(chr(i) for i in range(255)) one = ''.join(chr(i) for i in range(255))
two = u'\uFFFD' # this used to cause a UnicodeDecodeError constructing the failure msg
first = {'foo': one} with self.assertRaises(self.failureException):
second = {'foo': two} self.assertDictContainsSubset({'foo': one}, {'foo': u'\uFFFD'})
self.assertDictContainsSubset(first, second)
def testAssertEqual(self): def testAssertEqual(self):
equal_pairs = [ equal_pairs = [

View File

@ -163,7 +163,7 @@ class TestCase(object):
try: try:
testMethod = getattr(self, methodName) testMethod = getattr(self, methodName)
except AttributeError: except AttributeError:
raise ValueError("no such test method in %s: %s" % \ raise ValueError("no such test method in %s: %s" %
(self.__class__, methodName)) (self.__class__, methodName))
self._testMethodDoc = testMethod.__doc__ self._testMethodDoc = testMethod.__doc__
self._cleanups = [] self._cleanups = []
@ -725,7 +725,8 @@ class TestCase(object):
missing.append(key) missing.append(key)
elif value != actual[key]: elif value != actual[key]:
mismatched.append('%s, expected: %s, actual: %s' % mismatched.append('%s, expected: %s, actual: %s' %
(key, value, actual[key])) (safe_repr(key), safe_repr(value),
safe_repr(actual[key])))
if not (missing or mismatched): if not (missing or mismatched):
return return