mirror of
https://github.com/openvswitch/ovs
synced 2025-08-31 06:15:47 +00:00
python: make python idl unicode-tolerant
Ensure that JSON is utf-8 encoded and that bytes sent/received on the stream sockets are in utf-8 form. Add a test case to verify that unicode data can be sent/received successfully using Python IDL module. Co-authored-by: Terry Wilson <twilson@redhat.com> Signed-off-by: Terry Wilson <twilson@redhat.com> Signed-off-by: Lance Richardson <lrichard@redhat.com> Signed-off-by: Russell Bryant <russell@ovn.org>
This commit is contained in:
committed by
Russell Bryant
parent
f88ee3a599
commit
e7164d96bc
@@ -29,8 +29,13 @@ except ImportError:
|
||||
__pychecker__ = 'no-stringiter'
|
||||
|
||||
SPACES_PER_LEVEL = 2
|
||||
dumper = functools.partial(json.dumps, separators=(",", ":"),
|
||||
ensure_ascii=False)
|
||||
_dumper = functools.partial(json.dumps, separators=(",", ":"))
|
||||
|
||||
if six.PY2:
|
||||
def dumper(*args, **kwargs):
|
||||
return _dumper(*args, **kwargs).decode('raw-unicode-escape')
|
||||
else:
|
||||
dumper = _dumper
|
||||
|
||||
|
||||
def to_stream(obj, stream, pretty=False, sort_keys=True):
|
||||
|
@@ -268,8 +268,7 @@ class Connection(object):
|
||||
# Python 3 has separate types for strings and bytes. We
|
||||
# received bytes from a socket. We expect it to be string
|
||||
# data, so we convert it here as soon as possible.
|
||||
if (data and not error
|
||||
and not isinstance(data, six.string_types)):
|
||||
if data and not error:
|
||||
try:
|
||||
data = data.decode('utf-8')
|
||||
except UnicodeError:
|
||||
|
@@ -386,8 +386,10 @@ class Stream(object):
|
||||
try:
|
||||
# Python 3 has separate types for strings and bytes. We must have
|
||||
# bytes here.
|
||||
if six.PY3 and not isinstance(buf, six.binary_type):
|
||||
buf = six.binary_type(buf, 'utf-8')
|
||||
if six.PY3 and not isinstance(buf, bytes):
|
||||
buf = bytes(buf, 'utf-8')
|
||||
elif six.PY2:
|
||||
buf = buf.encode('utf-8')
|
||||
return self.socket.send(buf)
|
||||
except socket.error as e:
|
||||
return -ovs.socket_util.get_exception_errno(e)
|
||||
|
Reference in New Issue
Block a user