[ovs-dev] [PATCH v2 1/2] python: fix python3 encode/decode on Windows

Alin Balutoiu abalutoiu at cloudbasesolutions.com
Wed Aug 16 15:01:39 UTC 2017


Fix double encoding/decoding on data, caused by
'get_decoded_buffer' and 'get_encoded_buffer'.

The functions 'get_decoded_buffer' and 'get_encoded_buffer'
from winutils have been removed. They are no longer
necessary since the buffers received/returned are already
in the right form.

The necessary encoding has been moved before any sending
function (this also includes named pipes send on Windows).

Co-authored-by: Alin Serdean <aserdean at cloudbasesolutions.com>
Signed-off-by: Alin Balutoiu <abalutoiu at cloudbasesolutions.com>
Signed-off-by: Alin Serdean <aserdean at cloudbasesolutions.com>
Acked-by: Lance Richardson <lrichard at redhat.com>
---
v2: Removed unused import six.
---
 python/ovs/stream.py   | 26 +++++++++++++++-----------
 python/ovs/winutils.py | 20 --------------------
 2 files changed, 15 insertions(+), 31 deletions(-)

diff --git a/python/ovs/stream.py b/python/ovs/stream.py
index 57e7a6e..f82a449 100644
--- a/python/ovs/stream.py
+++ b/python/ovs/stream.py
@@ -322,8 +322,10 @@ class Stream(object):
                                                             False)
                 self._read_pending = False
                 recvBuffer = self._read_buffer[:nBytesRead]
-
-                return (0, winutils.get_decoded_buffer(recvBuffer))
+                # recvBuffer will have the type memoryview in Python3.
+                # We can use bytes to convert it to type bytes which works on
+                # both Python2 and Python3.
+                return (0, bytes(recvBuffer))
             except pywintypes.error as e:
                 if e.winerror == winutils.winerror.ERROR_IO_INCOMPLETE:
                     # The operation is still pending, try again
@@ -334,7 +336,6 @@ class Stream(object):
                     return (0, "")
                 else:
                     return (errno.EINVAL, "")
-
         (errCode, self._read_buffer) = winutils.read_file(self.pipe,
                                                           n,
                                                           self._read)
@@ -361,7 +362,10 @@ class Stream(object):
                 return (e.winerror, "")
 
         recvBuffer = self._read_buffer[:nBytesRead]
-        return (0, winutils.get_decoded_buffer(recvBuffer))
+        # recvBuffer will have the type memoryview in Python3.
+        # We can use bytes to convert it to type bytes which works on
+        # both Python2 and Python3.
+        return (0, bytes(recvBuffer))
 
     def send(self, buf):
         """Tries to send 'buf' on this stream.
@@ -380,16 +384,17 @@ class Stream(object):
         elif len(buf) == 0:
             return 0
 
+        # Python 3 has separate types for strings and bytes.  We must have
+        # bytes here.
+        if six.PY3 and not isinstance(buf, bytes):
+            buf = bytes(buf, 'utf-8')
+        elif six.PY2:
+            buf = buf.encode('utf-8')
+
         if sys.platform == 'win32' and self.socket is None:
             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, 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)
@@ -413,7 +418,6 @@ class Stream(object):
                 else:
                     return -errno.EINVAL
 
-        buf = winutils.get_encoded_buffer(buf)
         self._write_pending = False
         (errCode, nBytesWritten) = winutils.write_file(self.pipe,
                                                        buf,
diff --git a/python/ovs/winutils.py b/python/ovs/winutils.py
index a3627ff..89e28e1 100644
--- a/python/ovs/winutils.py
+++ b/python/ovs/winutils.py
@@ -14,8 +14,6 @@
 
 import sys
 
-import six
-
 if sys.platform != 'win32':
     raise Exception("Intended to use only on Windows")
 else:
@@ -198,24 +196,6 @@ def get_overlapped_result(handle, overlapped=None, bWait=False):
         raise
 
 
-def get_decoded_buffer(recvBuffer):
-    if six.PY3:
-        return bytes(recvBuffer).decode("utf-8")
-    else:
-        return str(recvBuffer)
-
-
-def get_encoded_buffer(buff):
-    # Python 3 has separate types for strings and bytes.
-    # We must have bytes here.
-    if not isinstance(buff, six.binary_type):
-        if six.PY3:
-            buff = six.binary_type(buff, 'utf-8')
-        else:
-            buff = six.binary_type(buff)
-    return buff
-
-
 def get_new_event(sa=None, bManualReset=True, bInitialState=True,
                   objectName=None):
     return win32event.CreateEvent(sa, bManualReset, bInitialState, objectName)
-- 
2.10.0.windows.1


More information about the dev mailing list