diff --git a/twisted/python/test/test_util.py b/twisted/python/test/test_util.py index e36ef2b..19b79cd 100644 --- a/twisted/python/test/test_util.py +++ b/twisted/python/test/test_util.py @@ -895,14 +895,31 @@ class SlowStringCompareTests(unittest.TestCase): C{unicode} object. """ def _compare(s1, s2): - expected = s1 == s2 + if sys.version_info >= (2, 5): + expected = s1 == s2 + + ws = self.flushWarnings( + [SlowStringCompareTests.test_unicodeComparison]) + for w in ws: + self.assertEquals(w['category'], UnicodeWarning) + + result = util.slowStringCompare(s1, s2) + else: + # When Python 2.4 cannot decode the non-unicode side of a string + # comparion, it raises UnicodeDecodeError instead of giving a + # UnicodeWarning and returning False. + try: + expected = s1 == s2 + except UnicodeDecodeError: + # Use the exception class itself as a placeholder to represent + # the raising of the exception. + expected = UnicodeDecodeError + + try: + result = util.slowStringCompare(s1, s2) + except UnicodeDecodeError: + result = UnicodeDecodeError - ws = self.flushWarnings( - [SlowStringCompareTests.test_unicodeComparison]) - for w in ws: - self.assertEquals(w['category'], UnicodeWarning) - - result = util.slowStringCompare(s1, s2) self.assertEquals(result, expected) [w] = self.flushWarnings( diff --git a/twisted/python/util.py b/twisted/python/util.py index 0e9cc12..e15ef89 100644 --- a/twisted/python/util.py +++ b/twisted/python/util.py @@ -1001,11 +1001,19 @@ def slowStringCompare(s1, s2): try: s2 = unicode(s2) except UnicodeDecodeError: + if sys.version_info < (2, 5): + # When Python 2.4 cannot decode the non-unicode side of a + # string comparion, it raises UnicodeDecodeError instead of + # giving a UnicodeWarning and returning False. Match this + # behavior and re-raise the exception. + raise return False elif not isinstance(s1, unicode) and isinstance(s2, unicode): try: s1 = unicode(s1) except UnicodeDecodeError: + if sys.version_info < (2, 5): + raise return False if len(s1) != len(s2):