Index: Lib/unittest/case.py =================================================================== --- Lib/unittest/case.py (revision 77910) +++ Lib/unittest/case.py (working copy) @@ -734,27 +734,25 @@ Raises with an error message listing which elements of expected_seq are missing from actual_seq and vice versa if any. """ - try: - expected = set(expected_seq) - actual = set(actual_seq) - missing = list(expected.difference(actual)) - unexpected = list(actual.difference(expected)) - missing.sort() - unexpected.sort() - except TypeError: - # Fall back to slower list-compare if any of the objects are - # not hashable. - expected = list(expected_seq) - actual = list(actual_seq) - with warnings.catch_warnings(): - if sys.py3kwarning: - # Silence Py3k warning - warnings.filterwarnings("ignore", - "dict inequality comparisons " - "not supported", DeprecationWarning) - expected.sort() - actual.sort() - missing, unexpected = util.sorted_list_difference(expected, actual) + with warnings.catch_warnings(): + if sys.py3kwarning: + # Silence Py3k warning raised during the sorting + for errmsg in ["dict inequality comparisons", + "builtin_function_or_method order comparisons", + "comparing unequal types"]: + warnings.filterwarnings("ignore", errmsg, DeprecationWarning) + try: + expected = set(expected_seq) + actual = set(actual_seq) + missing = sorted(expected.difference(actual)) + unexpected = sorted(actual.difference(expected)) + except TypeError: + # Fall back to slower list-compare if any of the objects are + # not hashable. + expected = sorted(expected_seq) + actual = sorted(actual_seq) + missing, unexpected = util.sorted_list_difference(expected, + actual) errors = [] if missing: errors.append('Expected, but missing:\n %r' % missing) Index: Lib/test/test_unittest.py =================================================================== --- Lib/test/test_unittest.py (revision 77910) +++ Lib/test/test_unittest.py (working copy) @@ -2656,11 +2656,23 @@ # Test that sequences of unhashable objects can be tested for sameness: self.assertSameElements([[1, 2], [3, 4]], [[3, 4], [1, 2]]) - - self.assertSameElements([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}]) self.assertRaises(self.failureException, self.assertSameElements, [[1]], [[2]]) + # check that py3k warnings are silenced while sorting and comparing + with test_support.check_warnings() as w: + # with sequences of dicts (non-hashable) + self.assertSameElements([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}]) + # with heterogenous non-hashable sequences + self.assertSameElements([1, 'x', divmod, []], [divmod, [], 'x', 1]) + # with hashable sequences with builtin functions or methods + self.assertRaises(self.failureException, self.assertSameElements, + [all, 3, abs, id], [3]) + # fail the test if warnings have been raised + if w.warnings: + self.fail('assertSameElements raised a warning: ' + + str(w.warnings[0])) + def testAssertSetEqual(self): set1 = set() set2 = set()