Browse Source

Fix issue #31 by catching UnicodeDecodeError as well

Matthew Wang 10 years ago
parent
commit
73e8e660b4
2 changed files with 12 additions and 1 deletions
  1. 1
    1
      cdiff.py
  2. 11
    0
      tests/test_cdiff.py

+ 1
- 1
cdiff.py View File

@@ -634,7 +634,7 @@ def decode(line):
634 634
     """Decode UTF-8 if necessary."""
635 635
     try:
636 636
         return line.decode('utf-8')
637
-    except AttributeError:
637
+    except (AttributeError, UnicodeDecodeError):
638 638
         return line
639 639
 
640 640
 

+ 11
- 0
tests/test_cdiff.py View File

@@ -71,6 +71,17 @@ class PatchStreamTest(unittest.TestCase):
71 71
         self.assertEqual(out, items)
72 72
 
73 73
 
74
+class DecodeTest(unittest.TestCase):
75
+
76
+    def test_normal(self):
77
+        utext = 'hello'.encode('utf-8')
78
+        self.assertEqual('hello', cdiff.decode(utext))
79
+
80
+    def test_malformed_utf8(self):
81
+        text = b'\x80\x02q\x01(U'
82
+        self.assertEqual(text, cdiff.decode(text))
83
+
84
+
74 85
 class HunkTest(unittest.TestCase):
75 86
 
76 87
     def test_get_old_text(self):