python: Deal with str and byte differences.
[cascardo/ovs.git] / python / ovs / jsonrpc.py
index efe0f6c..e3ef6db 100644 (file)
@@ -264,6 +264,15 @@ 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)):
+                    try:
+                        data = data.decode('utf-8')
+                    except UnicodeError:
+                        error = errno.EILSEQ
                 if error:
                     if error == errno.EAGAIN:
                         return error, None