Fix safe_unicode and string unicode library functions

This commit is contained in:
ZyX 2014-12-06 14:47:53 +03:00
parent 432cc5031f
commit 8707f35bc9
2 changed files with 16 additions and 8 deletions

View File

@ -100,7 +100,10 @@ def safe_unicode(s):
'''
try:
try:
return unicode(s)
if type(s) is bytes:
return unicode(s, 'ascii')
else:
return unicode(s)
except UnicodeDecodeError:
try:
return unicode(s, 'utf-8')
@ -125,11 +128,18 @@ class FailedUnicode(unicode):
pass
def string(s):
if type(s) is not str:
return s.encode('utf-8')
else:
return s
if sys.version_info < (3,):
def string(s):
if type(s) is not str:
return s.encode('utf-8')
else:
return s
else:
def string(s):
if type(s) is not str:
return s.decode('utf-8')
else:
return s
def surrogate_pair_to_character(high, low):

View File

@ -448,7 +448,6 @@ class TestUnicode(TestCase):
self.assertRaises(TypeError, plu.out_u, None)
def test_safe_unicode(self):
raise SkipTest('safe_unicode() function is buggy')
self.assertStringsIdentical('abc', plu.safe_unicode('abc'))
self.assertStringsIdentical('abc', plu.safe_unicode(b'abc'))
self.assertStringsIdentical('«»', plu.safe_unicode(b'\xc2\xab\xc2\xbb'))
@ -467,7 +466,6 @@ class TestUnicode(TestCase):
self.assertEqual('abc', plu.FailedUnicode('abc'))
def test_string(self):
raise SkipTest('string() function is buggy')
self.assertStringsIdentical(str('abc'), plu.string('abc'))
self.assertStringsIdentical(str('abc'), plu.string(b'abc'))