mirror of
https://github.com/openvswitch/ovs
synced 2025-08-31 06:15:47 +00:00
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>
This commit is contained in:
committed by
Russell Bryant
parent
cf14254d2b
commit
2254074e30
@@ -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,
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user