diff mbox

[ovs-dev] windows, python: Remove code duplication in send/recv functions

Message ID 1503398837-2498-2-git-send-email-abalutoiu@cloudbasesolutions.com
State Superseded
Headers show

Commit Message

Alin Balutoiu Aug. 22, 2017, 10:47 a.m. UTC
Move the return value at the end of the function
regardless of the pending/non-pending operation.

Signed-off-by: Alin Balutoiu <abalutoiu@cloudbasesolutions.com>
---
 python/ovs/stream.py | 78 ++++++++++++++++++++++++----------------------------
 1 file changed, 36 insertions(+), 42 deletions(-)

Comments

Russell Bryant Aug. 22, 2017, 12:32 p.m. UTC | #1
On Tue, Aug 22, 2017 at 6:47 AM, Alin Balutoiu
<abalutoiu@cloudbasesolutions.com> wrote:
> Move the return value at the end of the function
> regardless of the pending/non-pending operation.
>
> Signed-off-by: Alin Balutoiu <abalutoiu@cloudbasesolutions.com>
> ---
>  python/ovs/stream.py | 78 ++++++++++++++++++++++++----------------------------
>  1 file changed, 36 insertions(+), 42 deletions(-)

Acked-by: Russell Bryant <russell@ovn.org>
Alin-Gabriel Serdean Aug. 23, 2017, 10:15 a.m. UTC | #2
Acked-by: Alin Gabriel Serdean <aserdean@ovn.org>


> -----Original Message-----
> From: ovs-dev-bounces@openvswitch.org [mailto:ovs-dev-
> bounces@openvswitch.org] On Behalf Of Alin Balutoiu
> Sent: Tuesday, August 22, 2017 1:47 PM
> To: dev@openvswitch.org
> Subject: [ovs-dev] [PATCH] windows, python: Remove code duplication in
> send/recv functions
> 
> Move the return value at the end of the function regardless of the
> pending/non-pending operation.
> 
> Signed-off-by: Alin Balutoiu <abalutoiu@cloudbasesolutions.com>
> ---
>  python/ovs/stream.py | 78 ++++++++++++++++++++++++---------------------
> -------
>  1 file changed, 36 insertions(+), 42 deletions(-)
> 
> diff --git a/python/ovs/stream.py b/python/ovs/stream.py index
> f82a449..717ea18 100644
> --- a/python/ovs/stream.py
> +++ b/python/ovs/stream.py
> @@ -321,11 +321,6 @@ class Stream(object):
>                                                              self._read,
>                                                              False)
>                  self._read_pending = False
> -                recvBuffer = self._read_buffer[:nBytesRead]
> -                # 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 @@
-336,30 +331,31 @@
> class Stream(object):
>                      return (0, "")
>                  else:
>                      return (errno.EINVAL, "")
> -        (errCode, self._read_buffer) = winutils.read_file(self.pipe,
> -                                                          n,
> -                                                          self._read)
> -        if errCode:
> -            if errCode == winutils.winerror.ERROR_IO_PENDING:
> -                self._read_pending = True
> -                return (errno.EAGAIN, "")
> -            elif errCode in winutils.pipe_disconnected_errors:
> -                # If the pipe was disconnected, return 0.
> -                return (0, "")
> -            else:
> -                return (errCode, "")
> +        else:
> +            (errCode, self._read_buffer) = winutils.read_file(self.pipe,
> +                                                              n,
> +                                                              self._read)
> +            if errCode:
> +                if errCode == winutils.winerror.ERROR_IO_PENDING:
> +                    self._read_pending = True
> +                    return (errno.EAGAIN, "")
> +                elif errCode in winutils.pipe_disconnected_errors:
> +                    # If the pipe was disconnected, return 0.
> +                    return (0, "")
> +                else:
> +                    return (errCode, "")
> 
> -        try:
> -            nBytesRead = winutils.get_overlapped_result(self.pipe,
> -                                                        self._read,
> -                                                        False)
> -            winutils.win32event.SetEvent(self._read.hEvent)
> -        except pywintypes.error as e:
> -            if e.winerror in winutils.pipe_disconnected_errors:
> -                # If the pipe was disconnected, return 0.
> -                return (0, "")
> -            else:
> -                return (e.winerror, "")
> +            try:
> +                nBytesRead = winutils.get_overlapped_result(self.pipe,
> +                                                            self._read,
> +                                                            False)
> +                winutils.win32event.SetEvent(self._read.hEvent)
> +            except pywintypes.error as e:
> +                if e.winerror in winutils.pipe_disconnected_errors:
> +                    # If the pipe was disconnected, return 0.
> +                    return (0, "")
> +                else:
> +                    return (e.winerror, "")
> 
>          recvBuffer = self._read_buffer[:nBytesRead]
>          # recvBuffer will have the type memoryview in Python3.
> @@ -406,7 +402,6 @@ class Stream(object):
>
self._write,
>                                                                 False)
>                  self._write_pending = False
> -                return nBytesWritten
>              except pywintypes.error as e:
>                  if e.winerror == winutils.winerror.ERROR_IO_INCOMPLETE:
>                      # The operation is still pending, try again @@
-417,19 +412,18 @@
> class Stream(object):
>                      return -errno.ECONNRESET
>                  else:
>                      return -errno.EINVAL
> -
> -        self._write_pending = False
> -        (errCode, nBytesWritten) = winutils.write_file(self.pipe,
> -                                                       buf,
> -                                                       self._write)
> -        if errCode:
> -            if errCode == winutils.winerror.ERROR_IO_PENDING:
> -                self._write_pending = True
> -                return -errno.EAGAIN
> -            if (not nBytesWritten and
> -                    errCode in winutils.pipe_disconnected_errors):
> -                # If the pipe was disconnected, return connection reset.
> -                return -errno.ECONNRESET
> +        else:
> +            (errCode, nBytesWritten) = winutils.write_file(self.pipe,
> +                                                           buf,
> +                                                           self._write)
> +            if errCode:
> +                if errCode == winutils.winerror.ERROR_IO_PENDING:
> +                    self._write_pending = True
> +                    return -errno.EAGAIN
> +                if (not nBytesWritten and
> +                        errCode in winutils.pipe_disconnected_errors):
> +                    # If the pipe was disconnected, return connection
reset.
> +                    return -errno.ECONNRESET
>          return nBytesWritten
> 
>      def run(self):
> --
> 2.10.0.windows.1
> _______________________________________________
> dev mailing list
> dev@openvswitch.org
> https://mail.openvswitch.org/mailman/listinfo/ovs-dev
Alin-Gabriel Serdean Aug. 23, 2017, 3:46 p.m. UTC | #3
Thanks Russell and Alin.

I applied this on branch-2.7, branch-2.8 and master.

Thanks,
Alin.

> -----Original Message-----
> From: ovs-dev-bounces@openvswitch.org [mailto:ovs-dev-
> bounces@openvswitch.org] On Behalf Of Russell Bryant
> Sent: Tuesday, August 22, 2017 3:32 PM
> To: Alin Balutoiu <abalutoiu@cloudbasesolutions.com>
> Cc: dev@openvswitch.org
> Subject: Re: [ovs-dev] [PATCH] windows, python: Remove code duplication
> in send/recv functions
> 
> On Tue, Aug 22, 2017 at 6:47 AM, Alin Balutoiu
> <abalutoiu@cloudbasesolutions.com> wrote:
> > Move the return value at the end of the function regardless of the
> > pending/non-pending operation.
> >
> > Signed-off-by: Alin Balutoiu <abalutoiu@cloudbasesolutions.com>
> > ---
> >  python/ovs/stream.py | 78
> > ++++++++++++++++++++++++----------------------------
> >  1 file changed, 36 insertions(+), 42 deletions(-)
> 
> Acked-by: Russell Bryant <russell@ovn.org>
> 
> --
> Russell Bryant
> _______________________________________________
> dev mailing list
> dev@openvswitch.org
> https://mail.openvswitch.org/mailman/listinfo/ovs-dev
diff mbox

Patch

diff --git a/python/ovs/stream.py b/python/ovs/stream.py
index f82a449..717ea18 100644
--- a/python/ovs/stream.py
+++ b/python/ovs/stream.py
@@ -321,11 +321,6 @@  class Stream(object):
                                                             self._read,
                                                             False)
                 self._read_pending = False
-                recvBuffer = self._read_buffer[:nBytesRead]
-                # 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
@@ -336,30 +331,31 @@  class Stream(object):
                     return (0, "")
                 else:
                     return (errno.EINVAL, "")
-        (errCode, self._read_buffer) = winutils.read_file(self.pipe,
-                                                          n,
-                                                          self._read)
-        if errCode:
-            if errCode == winutils.winerror.ERROR_IO_PENDING:
-                self._read_pending = True
-                return (errno.EAGAIN, "")
-            elif errCode in winutils.pipe_disconnected_errors:
-                # If the pipe was disconnected, return 0.
-                return (0, "")
-            else:
-                return (errCode, "")
+        else:
+            (errCode, self._read_buffer) = winutils.read_file(self.pipe,
+                                                              n,
+                                                              self._read)
+            if errCode:
+                if errCode == winutils.winerror.ERROR_IO_PENDING:
+                    self._read_pending = True
+                    return (errno.EAGAIN, "")
+                elif errCode in winutils.pipe_disconnected_errors:
+                    # If the pipe was disconnected, return 0.
+                    return (0, "")
+                else:
+                    return (errCode, "")
 
-        try:
-            nBytesRead = winutils.get_overlapped_result(self.pipe,
-                                                        self._read,
-                                                        False)
-            winutils.win32event.SetEvent(self._read.hEvent)
-        except pywintypes.error as e:
-            if e.winerror in winutils.pipe_disconnected_errors:
-                # If the pipe was disconnected, return 0.
-                return (0, "")
-            else:
-                return (e.winerror, "")
+            try:
+                nBytesRead = winutils.get_overlapped_result(self.pipe,
+                                                            self._read,
+                                                            False)
+                winutils.win32event.SetEvent(self._read.hEvent)
+            except pywintypes.error as e:
+                if e.winerror in winutils.pipe_disconnected_errors:
+                    # If the pipe was disconnected, return 0.
+                    return (0, "")
+                else:
+                    return (e.winerror, "")
 
         recvBuffer = self._read_buffer[:nBytesRead]
         # recvBuffer will have the type memoryview in Python3.
@@ -406,7 +402,6 @@  class Stream(object):
                                                                self._write,
                                                                False)
                 self._write_pending = False
-                return nBytesWritten
             except pywintypes.error as e:
                 if e.winerror == winutils.winerror.ERROR_IO_INCOMPLETE:
                     # The operation is still pending, try again
@@ -417,19 +412,18 @@  class Stream(object):
                     return -errno.ECONNRESET
                 else:
                     return -errno.EINVAL
-
-        self._write_pending = False
-        (errCode, nBytesWritten) = winutils.write_file(self.pipe,
-                                                       buf,
-                                                       self._write)
-        if errCode:
-            if errCode == winutils.winerror.ERROR_IO_PENDING:
-                self._write_pending = True
-                return -errno.EAGAIN
-            if (not nBytesWritten and
-                    errCode in winutils.pipe_disconnected_errors):
-                # If the pipe was disconnected, return connection reset.
-                return -errno.ECONNRESET
+        else:
+            (errCode, nBytesWritten) = winutils.write_file(self.pipe,
+                                                           buf,
+                                                           self._write)
+            if errCode:
+                if errCode == winutils.winerror.ERROR_IO_PENDING:
+                    self._write_pending = True
+                    return -errno.EAGAIN
+                if (not nBytesWritten and
+                        errCode in winutils.pipe_disconnected_errors):
+                    # If the pipe was disconnected, return connection reset.
+                    return -errno.ECONNRESET
         return nBytesWritten
 
     def run(self):