Skip to content

Commit 6a57a9d

Browse files
committed
wip
1 parent 5e26697 commit 6a57a9d

File tree

2 files changed

+45
-35
lines changed

2 files changed

+45
-35
lines changed

mig/shared/base.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -490,12 +490,28 @@ def verify_local_url(configuration, req_url):
490490
return False
491491

492492

493+
def is_bytes_type(thetype):
494+
"""Return boolean indicating if val is a unicode string. We avoid
495+
`isinstance(val, unicode)`
496+
and the like since it breaks when combined with python-future and futurize.
497+
"""
498+
return (thetype == bytes)
499+
500+
493501
def is_unicode(val):
494502
"""Return boolean indicating if val is a unicode string. We avoid
495503
`isinstance(val, unicode)`
496504
and the like since it breaks when combined with python-future and futurize.
497505
"""
498-
return (type(u"") == type(val))
506+
return is_unicode_type(type(val))
507+
508+
509+
def is_unicode_type(thetype):
510+
"""Return boolean indicating if val is a unicode string. We avoid
511+
`isinstance(val, unicode)`
512+
and the like since it breaks when combined with python-future and futurize.
513+
"""
514+
return (thetype == type(u""))
499515

500516

501517
def force_utf8(val, highlight=''):
@@ -510,14 +526,14 @@ def force_utf8(val, highlight=''):
510526
return codecs.encode("%s%s%s" % (highlight, val, highlight), 'utf8')
511527

512528

513-
def _walk_and_convert_recursive(input_obj, highlight='', _as_bytes=False, _force_primitive=None, _force_recursive=None):
529+
def _walk_and_convert_recursive(input_obj, highlight='', _is_primitive=None, _force_primitive=None, _force_recursive=None):
514530
thetype = type(input_obj)
515531
if issubclass(thetype, dict):
516532
return {_force_recursive(i, highlight): _force_recursive(j, highlight) for (i, j) in
517533
input_obj.items()}
518534
elif issubclass(thetype, (list, tuple)):
519535
return thetype((_force_recursive(i, highlight) for i in input_obj))
520-
elif is_unicode(input_obj):
536+
elif not _is_primitive(thetype):
521537
return _force_primitive(input_obj, highlight)
522538
else:
523539
return input_obj
@@ -528,7 +544,7 @@ def force_utf8_rec(input_obj, highlight=''):
528544
dictionaries with nested unicode strings to a pure utf8 version. Actual
529545
changes are marked out with the highlight string if given.
530546
"""
531-
return _walk_and_convert_recursive(input_obj, highlight, _force_primitive=force_utf8, _force_recursive=force_utf8_rec)
547+
return _walk_and_convert_recursive(input_obj, highlight, _is_primitive=is_bytes_type, _force_primitive=force_utf8, _force_recursive=force_utf8_rec)
532548

533549

534550
def force_unicode(val, highlight=''):
@@ -548,7 +564,7 @@ def force_unicode_rec(input_obj, highlight=''):
548564
dictionaries with nested utf8 strings to a pure unicode version. Actual
549565
changes are marked out with the highlight string if given.
550566
"""
551-
return _walk_and_convert_recursive(input_obj, highlight, _force_primitive=force_unicode, _force_recursive=force_unicode_rec)
567+
return _walk_and_convert_recursive(input_obj, highlight, _is_primitive=is_unicode_type, _force_primitive=force_unicode, _force_recursive=force_unicode_rec)
552568

553569

554570
def _force_default_coding(input_obj, kind, highlight=''):

tests/test_mig_shared_base.py

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -58,35 +58,22 @@ def test_encode_within_a_dict(self):
5858
def test_encode_within_a_list(self):
5959
output = force_default_fs_coding_rec(['foo', 'bar', 'baz'])
6060

61-
truth = [force_default_str_coding('foo'),
62-
force_default_str_coding('bar'),
63-
force_default_str_coding('baz')]
61+
self.assertEqual(output, [b'foo', b'bar', b'baz'])
6462

65-
self.assertEqual(output, truth)
66-
67-
def test_encode_within_a_tuple(self):
63+
def test_encode_within_a_tuple_string(self):
6864
output = force_default_fs_coding_rec(('foo', 'bar', 'baz'))
69-
truth = (force_default_str_coding('foo'),
70-
force_default_str_coding('bar'),
71-
force_default_str_coding('baz'))
7265

73-
self.assertEqual(output, truth)
66+
self.assertEqual(output, (b'foo', b'bar', b'baz'))
7467

7568
def test_encode_within_a_tuple_bytes(self):
76-
output = force_default_fs_coding_rec(('foo', 'bar', 'baz'))
77-
truth = (force_default_str_coding('foo'),
78-
force_default_str_coding('bar'),
79-
force_default_str_coding('baz'))
69+
output = force_default_fs_coding_rec((b'foo', b'bar', b'baz'))
8070

81-
self.assertEqual(output, truth)
71+
self.assertEqual(output, (b'foo', b'bar', b'baz'))
8272

8373
def test_encode_within_a_tuple_unicode(self):
84-
output = force_default_fs_coding_rec(('foo', 'bar', 'baz'))
85-
truth = (force_default_str_coding('foo'),
86-
force_default_str_coding('bar'),
87-
force_default_str_coding('baz'))
74+
output = force_default_fs_coding_rec((u'foo', u'bar', u'baz'))
8875

89-
self.assertEqual(output, truth)
76+
self.assertEqual(output, (b'foo', b'bar', b'baz'))
9077

9178

9279
class MigSharedBase__force_utf8(MigTestCase):
@@ -95,29 +82,36 @@ class MigSharedBase__force_utf8(MigTestCase):
9582
def test_encode_string(self):
9683
output = force_utf8('foobar')
9784

98-
self.assertEqual(output, truth)
85+
self.assertEqual(output, b'foobar')
9986

10087
def test_encode_bytes(self):
10188
output = force_utf8(b'foobar')
10289

103-
self.assertEqual(output, truth)
90+
self.assertEqual(output, b'foobar')
10491

10592
def test_encode_unicode(self):
10693
output = force_utf8(u'foobar')
10794

108-
self.assertEqual(output, truth)
95+
self.assertEqual(output, b'foobar')
10996

11097

11198
class MigSharedBase__force_unicode(MigTestCase):
11299
"""Unit tests of mig.shared.base force_unicode()"""
113100

114-
def test_encode_a_string(self):
115-
# input_vals = (b'foobar', u'foobar')
116-
# truth = u'foobar'
117-
# for val in input_vals:
118-
# output = force_unicode(val)
119-
# self.assertEqual(output, truth)
120-
pass
101+
def test_encode_string(self):
102+
output = force_unicode('foobar')
103+
104+
self.assertEqual(output, u'foobar')
105+
106+
def test_encode_bytes(self):
107+
output = force_unicode(b'foobar')
108+
109+
self.assertEqual(output, u'foobar')
110+
111+
def test_encode_unicode(self):
112+
output = force_unicode(u'foobar')
113+
114+
self.assertEqual(output, u'foobar')
121115

122116
if __name__ == '__main__':
123117
testmain(failfast=True)

0 commit comments

Comments
 (0)