From patchwork Mon Dec 21 20:47:35 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Russell Bryant X-Patchwork-Id: 559732 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from archives.nicira.com (li376-54.members.linode.com [96.126.127.54]) by ozlabs.org (Postfix) with ESMTP id EEAE4140BB2 for ; Tue, 22 Dec 2015 07:55:17 +1100 (AEDT) Received: from archives.nicira.com (localhost [127.0.0.1]) by archives.nicira.com (Postfix) with ESMTP id 1DDF810899; Mon, 21 Dec 2015 12:48:44 -0800 (PST) X-Original-To: dev@openvswitch.org Delivered-To: dev@openvswitch.org Received: from mx3v3.cudamail.com (mx3.cudamail.com [64.34.241.5]) by archives.nicira.com (Postfix) with ESMTPS id D23CA1075D for ; Mon, 21 Dec 2015 12:48:41 -0800 (PST) Received: from bar4.cudamail.com (localhost [127.0.0.1]) by mx3v3.cudamail.com (Postfix) with ESMTPS id 6A5D31613F9 for ; Mon, 21 Dec 2015 13:48:41 -0700 (MST) X-ASG-Debug-ID: 1450730921-03dc213fef439660001-byXFYA Received: from mx3-pf3.cudamail.com ([192.168.14.3]) by bar4.cudamail.com with ESMTP id Lab148RTIjcNRuR7 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Mon, 21 Dec 2015 13:48:41 -0700 (MST) X-Barracuda-Envelope-From: russell@ovn.org X-Barracuda-RBL-Trusted-Forwarder: 192.168.14.3 Received: from unknown (HELO mx1.redhat.com) (209.132.183.28) by mx3-pf3.cudamail.com with ESMTPS (DHE-RSA-AES256-SHA encrypted); 21 Dec 2015 21:04:12 -0000 Received-SPF: neutral (mx3-pf3.cudamail.com: 209.132.183.28 is neither permitted nor denied by SPF record at ovn.org) X-Barracuda-Apparent-Source-IP: 209.132.183.28 X-Barracuda-RBL-IP: 209.132.183.28 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id 46C078E25A; Mon, 21 Dec 2015 20:48:40 +0000 (UTC) Received: from x1c.redhat.com ([10.3.112.11]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id tBLKm0dw002018; Mon, 21 Dec 2015 15:48:39 -0500 X-CudaMail-Envelope-Sender: russell@ovn.org From: Russell Bryant To: dev@openvswitch.org X-CudaMail-Whitelist-To: dev@openvswitch.org X-CudaMail-MID: CM-V3-1220053458 X-CudaMail-DTE: 122115 X-CudaMail-Originating-IP: 209.132.183.28 Date: Mon, 21 Dec 2015 15:47:35 -0500 X-ASG-Orig-Subj: [##CM-V3-1220053458##][PATCH 35/55] python: Fix object comparisons in Python 3. Message-Id: <1450730875-18083-36-git-send-email-russell@ovn.org> In-Reply-To: <1450730875-18083-1-git-send-email-russell@ovn.org> References: <1450730875-18083-1-git-send-email-russell@ovn.org> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Barracuda-Connect: UNKNOWN[192.168.14.3] X-Barracuda-Start-Time: 1450730921 X-Barracuda-Encrypted: DHE-RSA-AES256-SHA X-Barracuda-URL: https://web.cudamail.com:443/cgi-mod/mark.cgi X-ASG-Whitelist: Header =?UTF-8?B?eFwtY3VkYW1haWxcLXdoaXRlbGlzdFwtdG8=?= X-Virus-Scanned: by bsmtpd at cudamail.com X-Barracuda-BRTS-Status: 1 Subject: [ovs-dev] [PATCH 35/55] python: Fix object comparisons in Python 3. X-BeenThere: dev@openvswitch.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dev-bounces@openvswitch.org Sender: "dev" Python 3 no longer supports __cmp__. Instead, we have to implement the "rich comparison" operators. We implement __eq__ and __lt__ and use functools.total_ordering to implement the rest. In one case, no __cmp__ method was provided and instead relied on the default behavior provided in Python 2. We have to implement the comparisons explicitly for Python 3. Signed-off-by: Russell Bryant --- python/ovs/db/data.py | 23 +++++++++++++++++++++++ python/ovs/db/idl.py | 15 +++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/python/ovs/db/data.py b/python/ovs/db/data.py index 61063b1..52a9100 100644 --- a/python/ovs/db/data.py +++ b/python/ovs/db/data.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import functools import re import uuid @@ -64,6 +65,7 @@ def returnUnchanged(x): return x +@functools.total_ordering class Atom(object): def __init__(self, type_, value=None): self.type = type_ @@ -72,6 +74,16 @@ class Atom(object): else: self.value = type_.default_atom() + def __eq__(self, other): + if not isinstance(other, Atom) or self.type != other.type: + return NotImplemented + return True if self.value == other.value else False + + def __lt__(self, other): + if not isinstance(other, Atom) or self.type != other.type: + return NotImplemented + return True if self.value < other.value else False + def __cmp__(self, other): if not isinstance(other, Atom) or self.type != other.type: return NotImplemented @@ -256,11 +268,22 @@ class Atom(object): return Atom(t, x) +@functools.total_ordering class Datum(object): def __init__(self, type_, values={}): self.type = type_ self.values = values + def __eq__(self, other): + if not isinstance(other, Datum): + return NotImplemented + return True if self.values == other.values else False + + def __lt__(self, other): + if not isinstance(other, Datum): + return NotImplemented + return True if self.values < other.values else False + def __cmp__(self, other): if not isinstance(other, Datum): return NotImplemented diff --git a/python/ovs/db/idl.py b/python/ovs/db/idl.py index 632bab0..eb11051 100644 --- a/python/ovs/db/idl.py +++ b/python/ovs/db/idl.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import functools import uuid import six @@ -504,6 +505,7 @@ def _row_to_uuid(value): return value +@functools.total_ordering class Row(object): """A row within an IDL. @@ -572,6 +574,19 @@ class Row(object): # in the dictionary are all None. self.__dict__["_prereqs"] = {} + def __lt__(self, other): + if not isinstance(other, Row): + return NotImplemented + return bool(self.__dict__['uuid'] < other.__dict__['uuid']) + + def __eq__(self, other): + if not isinstance(other, Row): + return NotImplemented + return bool(self.__dict__['uuid'] == other.__dict__['uuid']) + + def __hash__(self): + return int(self.__dict__['uuid']) + def __getattr__(self, column_name): assert self._changes is not None