Skip to content

Commit cd1387c

Browse files
committed
Merge branch 'master' of github.com:seperman/deepdiff
2 parents f0c462f + 25f7e71 commit cd1387c

File tree

3 files changed

+85
-9
lines changed

3 files changed

+85
-9
lines changed

deepdiff/diff.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from collections import Mapping
2020
from collections import Iterable
2121

22-
from deepdiff.helper import py3, strings, numbers, ListItemRemovedOrAdded, notpresent, IndexedHash, Verbose
22+
from deepdiff.helper import py3, strings, bytes_type, numbers, ListItemRemovedOrAdded, notpresent, IndexedHash, Verbose
2323
from deepdiff.model import RemapDict, ResultDict, TextResult, TreeResult, DiffLevel
2424
from deepdiff.model import DictRelationship, AttributeRelationship # , REPORT_KEYS
2525
from deepdiff.model import SubscriptableIterableRelationship, NonSubscriptableIterableRelationship, SetRelationship
@@ -901,14 +901,25 @@ def __diff_str(self, level):
901901
"""Compare strings"""
902902
if level.t1 == level.t2:
903903
return
904-
904+
905905
# do we add a diff for convenience?
906-
if '\n' in level.t1 or '\n' in level.t2:
907-
diff = difflib.unified_diff(
908-
level.t1.splitlines(), level.t2.splitlines(), lineterm='')
909-
diff = list(diff)
910-
if diff:
911-
level.additional['diff'] = '\n'.join(diff)
906+
do_diff = True
907+
if isinstance(level.t1, bytes_type):
908+
try:
909+
t1_str = level.t1.decode('ascii')
910+
t2_str = level.t2.decode('ascii')
911+
except UnicodeDecodeError:
912+
do_diff = False
913+
else:
914+
t1_str = level.t1
915+
t2_str = level.t2
916+
if do_diff:
917+
if u'\n' in t1_str or u'\n' in t2_str:
918+
diff = difflib.unified_diff(
919+
t1_str.splitlines(), t2_str.splitlines(), lineterm='')
920+
diff = list(diff)
921+
if diff:
922+
level.additional['diff'] = u'\n'.join(diff)
912923

913924
self.__report_result('values_changed', level)
914925

deepdiff/helper.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@
2020
if py3: # pragma: no cover
2121
from builtins import int
2222
strings = (str, bytes) # which are both basestring
23+
unicode_type = str
24+
bytes_type = bytes
2325
numbers = (int, float, complex, datetime.datetime, datetime.date, Decimal)
2426
items = 'items'
2527
else: # pragma: no cover
2628
int = int
2729
strings = (str, unicode)
30+
unicode_type = unicode
31+
bytes_type = str
2832
numbers = (int, float, long, complex, datetime.datetime, datetime.date,
2933
Decimal)
3034

tests/test_diff_text.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import datetime
2424
from decimal import Decimal
2525
from deepdiff import DeepDiff
26-
from deepdiff.helper import py3
26+
from deepdiff.helper import py3, bytes_type
2727
from tests import CustomClass
2828
if py3:
2929
from unittest import mock
@@ -177,6 +177,67 @@ def test_string_difference2(self):
177177
}
178178
}
179179
self.assertEqual(ddiff, result)
180+
181+
def test_bytes(self):
182+
t1 = {
183+
1: 1,
184+
2: 2,
185+
3: 3,
186+
4: {
187+
"a": b"hello",
188+
"b": b"world!\nGoodbye!\n1\n2\nEnd",
189+
"c": b"\x80",
190+
}
191+
}
192+
t2 = {1: 1,
193+
2: 2,
194+
3: 3,
195+
4: {
196+
"a": b"hello",
197+
"b": b"world\n1\n2\nEnd",
198+
"c": b'\x81',
199+
}
200+
}
201+
ddiff = DeepDiff(t1, t2)
202+
result = {
203+
'values_changed': {
204+
"root[4]['b']": {
205+
'diff':
206+
'--- \n+++ \n@@ -1,5 +1,4 @@\n-world!\n-Goodbye!\n+world\n 1\n 2\n End',
207+
'new_value': b'world\n1\n2\nEnd',
208+
'old_value': b'world!\nGoodbye!\n1\n2\nEnd'
209+
},
210+
"root[4]['c']": {
211+
'new_value': b'\x81',
212+
'old_value': b'\x80'
213+
}
214+
}
215+
}
216+
self.assertEqual(ddiff, result)
217+
218+
def test_unicode(self):
219+
t1 = {
220+
1: 1,
221+
2: 2,
222+
3: 3,
223+
4: {
224+
"a": u"hello",
225+
"b": u"world!\nGoodbye!\n1\n2\nEnd"
226+
}
227+
}
228+
t2 = {1: 1, 2: 2, 3: 3, 4: {"a": u"hello", "b": u"world\n1\n2\nEnd"}}
229+
ddiff = DeepDiff(t1, t2)
230+
result = {
231+
'values_changed': {
232+
"root[4]['b']": {
233+
'diff':
234+
'--- \n+++ \n@@ -1,5 +1,4 @@\n-world!\n-Goodbye!\n+world\n 1\n 2\n End',
235+
'new_value': u'world\n1\n2\nEnd',
236+
'old_value': u'world!\nGoodbye!\n1\n2\nEnd'
237+
}
238+
}
239+
}
240+
self.assertEqual(ddiff, result)
180241

181242
def test_type_change(self):
182243
t1 = {1: 1, 2: 2, 3: 3, 4: {"a": "hello", "b": [1, 2, 3]}}

0 commit comments

Comments
 (0)