diff options
author | Alin Balutoiu <abalutoiu@cloudbasesolutions.com> | 2017-08-16 15:01:39 +0000 |
---|---|---|
committer | Russell Bryant <russell@ovn.org> | 2017-08-18 16:12:50 -0400 |
commit | 2254074e3067fd306c37ac80347ef75075b40a89 (patch) | |
tree | 03c6e50618702188684f95fdeaf99296d82b529d /python | |
parent | cf14254d2b8767813c08f753c99c4c571e4c9aaa (diff) | |
download | openvswitch-2254074e3067fd306c37ac80347ef75075b40a89.tar.gz |
python: fix python3 encode/decode on Windows
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@cloudbasesolutions.com>
Signed-off-by: Alin Balutoiu <abalutoiu@cloudbasesolutions.com>
Signed-off-by: Alin Serdean <aserdean@cloudbasesolutions.com>
Acked-by: Lance Richardson <lrichard@redhat.com>
Signed-off-by: Russell Bryant <russell@ovn.org>
Diffstat (limited to 'python')
-rw-r--r-- | python/ovs/stream.py | 26 | ||||
-rw-r--r-- | 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 57e7a6eef..f82a44903 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 a3627ff43..89e28e107 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) |