2
0
mirror of https://github.com/openvswitch/ovs synced 2025-10-23 14:57:06 +00:00

python: Drop unicode type.

Python 2 had str and unicode.  Python 3 only has str, which is always a
unicode string.  Drop use of unicode with the help of six.text_type
(unicode in py2 and str in py3) and six.string_types ([str, unicode] in
py2 and [str] in py3).

Signed-off-by: Russell Bryant <russell@ovn.org>
Acked-by: Ben Pfaff <blp@ovn.org>
This commit is contained in:
Russell Bryant
2015-12-14 17:01:11 -05:00
parent 8f808842a0
commit 25f599fbd3
10 changed files with 66 additions and 42 deletions

View File

@@ -61,10 +61,13 @@ class _Serializer(object):
self.stream.write(u"%d" % obj)
elif isinstance(obj, float):
self.stream.write("%.15g" % obj)
elif isinstance(obj, unicode):
elif isinstance(obj, six.text_type):
# unicode() on Python 2, or str() in Python 3 (always unicode)
self.__serialize_string(obj)
elif isinstance(obj, str):
self.__serialize_string(unicode(obj))
# This is for Python 2, where this comes out to unicode(str()).
# For Python 3, it's str(str()), but it's harmless.
self.__serialize_string(six.text_type(obj))
elif isinstance(obj, dict):
self.stream.write(u"{")
@@ -79,7 +82,7 @@ class _Serializer(object):
if i > 0:
self.stream.write(u",")
self.__indent_line()
self.__serialize_string(unicode(key))
self.__serialize_string(six.text_type(key))
self.stream.write(u":")
if self.pretty:
self.stream.write(u' ')
@@ -144,12 +147,16 @@ def from_file(name):
def from_string(s):
try:
s = unicode(s, 'utf-8')
except UnicodeDecodeError as e:
seq = ' '.join(["0x%2x" % ord(c)
for c in e.object[e.start:e.end] if ord(c) >= 0x80])
return ("not a valid UTF-8 string: invalid UTF-8 sequence %s" % seq)
if not isinstance(s, six.text_type):
# We assume the input is a string. We will only hit this case for a
# str in Python 2 which is not unicode, so we need to go ahead and
# decode it.
try:
s = six.text_type(s, 'utf-8')
except UnicodeDecodeError as e:
seq = ' '.join(["0x%2x" % ord(c)
for c in e.object[e.start:e.end] if ord(c) >= 0x80])
return "not a valid UTF-8 string: invalid UTF-8 sequence %s" % seq
p = Parser(check_trailer=True)
p.feed(s)
return p.finish()