From patchwork Tue Aug 22 10:47:24 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alin Balutoiu X-Patchwork-Id: 804385 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3xc6j022QWz9s83 for ; Tue, 22 Aug 2017 20:48:04 +1000 (AEST) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 291E9AAB; Tue, 22 Aug 2017 10:47:28 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 6600EA80 for ; Tue, 22 Aug 2017 10:47:26 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail.cloudbasesolutions.com (mail.cloudbasesolutions.com [91.232.152.5]) by smtp1.linuxfoundation.org (Postfix) with ESMTP id D1E7A171 for ; Tue, 22 Aug 2017 10:47:25 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by mail.cloudbasesolutions.com (Postfix) with ESMTP id 165B144338 for ; Tue, 22 Aug 2017 13:47:25 +0300 (EEST) X-Virus-Scanned: amavisd-new at cloudbasesolutions.com Received: from mail.cloudbasesolutions.com ([127.0.0.1]) by localhost (mail.cloudbasesolutions.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id xZz2JBIJQuA4 for ; Tue, 22 Aug 2017 13:47:24 +0300 (EEST) Received: from mail.cloudbasesolutions.com (unknown [10.77.78.3]) by mail.cloudbasesolutions.com (Postfix) with ESMTP id 8FB6C441FA for ; Tue, 22 Aug 2017 13:47:24 +0300 (EEST) Received: from CBSEX1.cloudbase.local ([10.77.78.3]) by CBSEX1.cloudbase.local ([10.77.78.3]) with mapi id 14.03.0361.001; Tue, 22 Aug 2017 12:47:24 +0200 From: Alin Balutoiu To: "dev@openvswitch.org" Thread-Topic: [PATCH] windows,python: Remove code duplication in send/recv functions Thread-Index: AQHTGzQJ17XWlVCtC0uUqwMjQjbEEw== Date: Tue, 22 Aug 2017 10:47:24 +0000 Message-ID: <1503398837-2498-2-git-send-email-abalutoiu@cloudbasesolutions.com> References: <1503398837-2498-1-git-send-email-abalutoiu@cloudbasesolutions.com> In-Reply-To: <1503398837-2498-1-git-send-email-abalutoiu@cloudbasesolutions.com> Accept-Language: en-US, it-IT Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.77.78.1] MIME-Version: 1.0 Subject: [ovs-dev] [PATCH] windows, python: Remove code duplication in send/recv functions X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org Move the return value at the end of the function regardless of the pending/non-pending operation. Signed-off-by: Alin Balutoiu Acked-by: Russell Bryant Acked-by: Alin Gabriel Serdean --- 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):