diff mbox

[ovs-dev] python: Fix Chinese characters UnicodeEncodeError exception

Message ID 20170220113825.3464-1-ligs@dtdream.com
State Changes Requested
Headers show

Commit Message

Guoshuai Li Feb. 20, 2017, 11:38 a.m. UTC
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 <ligs@dtdream.com>
---
 python/ovs/jsonrpc.py | 9 ++++-----
 python/ovs/stream.py  | 8 ++++----
 2 files changed, 8 insertions(+), 9 deletions(-)

Comments

Ben Pfaff March 9, 2017, midnight UTC | #1
On Mon, Feb 20, 2017 at 07:38:25PM +0800, Guoshuai Li wrote:
> 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 <ligs@dtdream.com>

Thanks for the patch!

This fails to build with the following error:

../python/ovs/stream.py:388:80: E501 line too long (80 > 79 characters)

Would you mind adding a test?

Thanks,

Ben.
diff mbox

Patch

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)