Fix safe_encode(): return bytes on Python 3

safe_decode() decodes bytes to text. safe_encode() should be the reverse
operation: encode text to bytes. Currently, it returns text on Python 3
which is wrong in my opinion.

The patch fixes also test_encode() on Python 3.

Change-Id: If91a866d864a22d28a352152beff4c7406a27b7b
This commit is contained in:
Victor Stinner 2014-04-07 12:09:24 +02:00
parent f32f2d90a2
commit fd18c2886d
2 changed files with 17 additions and 20 deletions

View File

@ -159,19 +159,13 @@ def safe_encode(text, incoming=None,
sys.getdefaultencoding())
if isinstance(text, six.text_type):
if six.PY3:
return text.encode(encoding, errors).decode(incoming)
else:
return text.encode(encoding, errors)
return text.encode(encoding, errors)
elif text and encoding != incoming:
# Decode text before encoding it with `encoding`
text = safe_decode(text, incoming, errors)
if six.PY3:
return text.encode(encoding, errors).decode(incoming)
else:
return text.encode(encoding, errors)
return text
return text.encode(encoding, errors)
else:
return text
def string_to_bytes(text, unit_system='IEC', return_int=False):

View File

@ -166,18 +166,21 @@ class StrUtilsTest(test.BaseTestCase):
def test_safe_encode(self):
safe_encode = strutils.safe_encode
self.assertRaises(TypeError, safe_encode, True)
self.assertEqual("ni\xc3\xb1o", safe_encode(six.u('ni\xf1o'),
encoding="utf-8"))
self.assertEqual("dGVzdA==\n", safe_encode("test",
encoding='base64'))
self.assertEqual('ni\xf1o', safe_encode("ni\xc3\xb1o",
encoding="iso-8859-1",
incoming="utf-8"))
self.assertEqual(six.b("ni\xc3\xb1o"), safe_encode(six.u('ni\xf1o'),
encoding="utf-8"))
if six.PY2:
# In Python 3, str.encode() doesn't support anymore
# text => text encodings like base64
self.assertEqual(six.b("dGVzdA==\n"),
safe_encode("test", encoding='base64'))
self.assertEqual(six.b('ni\xf1o'), safe_encode(six.b("ni\xc3\xb1o"),
encoding="iso-8859-1",
incoming="utf-8"))
# Forcing incoming to ascii so it falls back to utf-8
self.assertEqual('ni\xc3\xb1o', safe_encode('ni\xc3\xb1o',
incoming='ascii'))
self.assertEqual('foo', safe_encode(b'foo'))
self.assertEqual(six.b('ni\xc3\xb1o'),
safe_encode(six.b('ni\xc3\xb1o'), incoming='ascii'))
self.assertEqual(six.b('foo'), safe_encode(six.u('foo')))
def test_slugify(self):
to_slug = strutils.to_slug