[Mesa-dev] [PATCH 11/26] python: Fix rich comparisons

Mathieu Bridon bochecha at daitauha.fr
Wed Jul 11 18:01:50 UTC 2018


Python 3 lost the cmp() builtin, and doesn't call objects __cmp__()
methods any more to compare them.

Instead, Python 3 requires implementing the rich comparison methods
explicitly: __eq__(), __ne(), __lt__(), __le__(), __gt__() and __ge__().

Fortunately those are trivial to implement by just calling the existing
__cmp__() method, which makes the code compatible with both Python 2 and
Python 3.

This commit only implements the comparison methods which are actually
used by the build scripts.

In addition, this commit brings back to Python 3 the cmp() builtin
method as required.

Signed-off-by: Mathieu Bridon <bochecha at daitauha.fr>
---
 src/amd/vulkan/radv_extensions.py  |  6 +++++-
 src/intel/vulkan/anv_extensions.py |  6 +++++-
 src/mapi/mapi_abi.py               | 13 +++++++++++++
 3 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/amd/vulkan/radv_extensions.py b/src/amd/vulkan/radv_extensions.py
index c36559f48e..13a05fa21d 100644
--- a/src/amd/vulkan/radv_extensions.py
+++ b/src/amd/vulkan/radv_extensions.py
@@ -151,7 +151,11 @@ class VkVersion:
             other = copy.copy(other)
             other.patch = self.patch
 
-        return self.__int_ver().__cmp__(other.__int_ver())
+        return self.__int_ver() - other.__int_ver()
+
+    def __gt__(self, other):
+        return self.__cmp__(other) > 0
+
 
 MAX_API_VERSION = VkVersion(MAX_API_VERSION)
 
diff --git a/src/intel/vulkan/anv_extensions.py b/src/intel/vulkan/anv_extensions.py
index adc1d75898..47dba164ef 100644
--- a/src/intel/vulkan/anv_extensions.py
+++ b/src/intel/vulkan/anv_extensions.py
@@ -166,7 +166,11 @@ class VkVersion:
             other = copy.copy(other)
             other.patch = self.patch
 
-        return self.__int_ver().__cmp__(other.__int_ver())
+        return self.__int_ver() - other.__int_ver()
+
+    def __gt__(self, other):
+        return self.__cmp__(other) > 0
+
 
 
 MAX_API_VERSION = VkVersion('0.0.0')
diff --git a/src/mapi/mapi_abi.py b/src/mapi/mapi_abi.py
index be1d15d922..67fdb10650 100644
--- a/src/mapi/mapi_abi.py
+++ b/src/mapi/mapi_abi.py
@@ -38,6 +38,15 @@ import gl_XML
 import glX_XML
 
 
+try:
+    cmp
+
+except NameError:
+    # Python 3 does not have cmp()
+    def cmp(a, b):
+        return ((a > b) - (a < b))
+
+
 # number of dynamic entries
 ABI_NUM_DYNAMIC_ENTRIES = 256
 
@@ -135,6 +144,10 @@ class ABIEntry(object):
 
         return res
 
+    def __lt__(self, other):
+        return self.__cmp__(other) < 0
+
+
 def abi_parse_xml(xml):
     """Parse a GLAPI XML file for ABI entries."""
     api = gl_XML.parse_GL_API(xml, glX_XML.glx_item_factory())
-- 
2.17.1



More information about the mesa-dev mailing list