The OVSDB client may send or recv Chinese characters. So we should use UTF-8 encoding data for send or recv by default.
Signed-off-by: Guoshuai Li <[email protected]> --- python/ovs/jsonrpc.py | 9 ++++----- python/ovs/stream.py | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/python/ovs/jsonrpc.py b/python/ovs/jsonrpc.py index 5a11500..5db6080 100644 --- a/python/ovs/jsonrpc.py +++ b/python/ovs/jsonrpc.py @@ -265,11 +265,10 @@ class Connection(object): while True: if not self.input: error, data = self.stream.recv(4096) - # 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)): + # In Python 3, We received bytes from socket, and convert it + # to utf8 string by decode. In Python 2, We received ascii + # string from socket default, and convert it to utf8 by decode. + if data and not error: try: data = data.decode('utf-8') except UnicodeError: diff --git a/python/ovs/stream.py b/python/ovs/stream.py index be69534..d72dddf 100644 --- a/python/ovs/stream.py +++ b/python/ovs/stream.py @@ -384,10 +384,10 @@ class Stream(object): return self.__send_windows(buf) 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') + # The buf type is usually string, we convert it to bytes by encode + # in python 3. and convert it to utf-8 string by encode in Python 2. + if isinstance(buf, six.text_type): + buf = buf.encode('utf-8') return self.socket.send(buf) except socket.error as e: return -ovs.socket_util.get_exception_errno(e) -- 2.10.1.windows.1 This is my Chinese characters UnicodeEncodeError exception: File "/usr/lib/python2.7/site-packages/ovs/jsonrpc.py", line 202, in run retval = self.stream.send(self.output) File "/usr/lib/python2.7/site-packages/ovs/stream.py", line 241, in send return self.socket.send(buf) File "/usr/lib/python2.7/site-packages/eventlet/greenio/base.py", line 379, in send return self._send_loop(self.fd.send, data, flags) File "/usr/lib/python2.7/site-packages/eventlet/greenio/base.py", line 362, in _send_loop return send_method(data, *args) UnicodeEncodeError: 'ascii' codec can't encode characters in position 134-136: ordinal not in range(128) File "/usr/lib/python2.7/site-packages/ovs/jsonrpc.py", line 540, in recv error, msg = self.rpc.recv() File "/usr/lib/python2.7/site-packages/ovs/jsonrpc.py", line 300, in recv self.input = self.input[self.parser.feed(self.input):] File "/usr/lib/python2.7/site-packages/ovs/json.py", line 492, in feed if self.__lex_input(c): File "/usr/lib/python2.7/site-packages/ovs/json.py", line 299, in __lex_finish_string out += inp UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 6: ordinal not in range(128) _______________________________________________ dev mailing list [email protected] https://mail.openvswitch.org/mailman/listinfo/ovs-dev
