Skip to content

Commit

Permalink
windows, python: Remove code duplication in send/recv functions
Browse files Browse the repository at this point in the history
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>
Acked-by: Russell Bryant <russell@ovn.org>
Acked-by: Alin Gabriel Serdean <aserdean@ovn.org>
Signed-off-by: Alin Gabriel Serdean <aserdean@ovn.org>
  • Loading branch information
alinbalutoiu authored and aserdean committed Aug 23, 2017
1 parent e2e31d1 commit ba953e1
Showing 1 changed file with 36 additions and 42 deletions.
78 changes: 36 additions & 42 deletions python/ovs/stream.py
Expand Up @@ -321,11 +321,6 @@ def __recv_windows(self, n):
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
Expand All @@ -336,30 +331,31 @@ def __recv_windows(self, n):
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.
Expand Down Expand Up @@ -406,7 +402,6 @@ def __send_windows(self, buf):
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
Expand All @@ -417,19 +412,18 @@ def __send_windows(self, buf):
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):
Expand Down

0 comments on commit ba953e1

Please sign in to comment.