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

2017-08-16 Thread Lance Richardson
> From: "Alin Balutoiu" <abalut...@cloudbasesolutions.com>
> To: d...@openvswitch.org
> Sent: Monday, August 14, 2017 9:47:06 PM
> Subject: [ovs-dev] [PATCH 1/2] 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 <aserd...@cloudbasesolutions.com>
> Signed-off-by: Alin Balutoiu <abalut...@cloudbasesolutions.com>
> Signed-off-by: Alin Serdean <aserd...@cloudbasesolutions.com>
> ---
>  python/ovs/stream.py   | 26 +++---
>  python/ovs/winutils.py | 18 --
>  2 files changed, 15 insertions(+), 29 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..c29226f 100644
> --- a/python/ovs/winutils.py
> +++ b/python/ovs/winutils.py

I'm seeing a flake8 error with these changes, looks like "import six" needs to 
be
removed:

python/ovs/winutils.py:17:1: F401 'six' imported but unused


> @@ -198,24 +198,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&qu

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

2017-08-14 Thread Alin Balutoiu
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 
Signed-off-by: Alin Balutoiu 
Signed-off-by: Alin Serdean 
---
 python/ovs/stream.py   | 26 +++---
 python/ovs/winutils.py | 18 --
 2 files changed, 15 insertions(+), 29 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..c29226f 100644
--- a/python/ovs/winutils.py
+++ b/python/ovs/winutils.py
@@ -198,24 +198,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
___
dev mailing list
d...@openvswitch.org
https://mail.openvswitch.org/mailman/listinfo/ovs-dev