[Piglit] [PATCH 10/12] gen_dispatch.py: remove use of cmp.

Dylan Baker baker.dylan.c at gmail.com
Tue Jan 13 17:48:20 PST 2015


gen_dispatch.py contains a function used to sort Enums, and uses cmp.
This patch moves that logic into the Enum class using rich comparison
methods instead.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 registry/gl.py             | 62 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/util/gen_dispatch.py | 42 ++-----------------------------
 2 files changed, 64 insertions(+), 40 deletions(-)

diff --git a/registry/gl.py b/registry/gl.py
index 99f4a41..fafd42f 100644
--- a/registry/gl.py
+++ b/registry/gl.py
@@ -1076,6 +1076,7 @@ class EnumGroup(object):
             self.type = 'special'
 
 
+ at functools.total_ordering
 class Enum(object):
     """An <enum> XML element.
 
@@ -1122,6 +1123,67 @@ class Enum(object):
                  ' value={self.str_value!r})')
         return templ.format(self=self)
 
+    def __eq__(self, other):
+        if self.num_value != other.num_value:
+            return False
+        elif (self.vendor_namespace is None) != (other.vendor_namespace is None):
+            return False
+        elif (self.vendor_namespace in Extension.RATIFIED_NAMESPACES) != \
+                 (other.vendor_namespace in Extension.RATIFIED_NAMESPACES):
+            return False
+        elif (self.vendor_namespace == 'EXT') != \
+                 (other.vendor_namespace == 'EXT'):
+            return False
+        elif self.name != other.name:
+            return False
+        return self.api == other.api
+
+    def __lt__(self, other):  # pylint: disable=too-many-return-statements
+        """Less than.
+
+        Sort by numerical value, then vendor_namspace (first for ratified, then
+        for EXT), then by full name, and finally by api.
+
+        This sort order ensures that names provided by core specifications
+        precede thos provided by ratified extensions, which procede those
+        provided by unratified extensions.
+
+        For example: GL_RED < GL_RED_EXT < GL_RED_INTEL
+
+        """
+        if self.num_value != other.num_value:
+            if self.num_value < other.num_value:
+                return True
+            return False
+
+        x = self.vendor_namespace is None
+        y = other.vendor_namespace is None
+        if x != y:
+            if x and not y:
+                return True
+            return False
+
+        x = self.vendor_namespace in Extension.RATIFIED_NAMESPACES
+        y = other.vendor_namespace in Extension.RATIFIED_NAMESPACES
+        if x != y:
+            if x and not y:
+                return True
+            return False
+
+        x = self.vendor_namespace == 'EXT'
+        y = other.vendor_namespace == 'EXT'
+        if x != y:
+            if x and not y:
+                return True
+            return False
+
+        if self.name != other.name:
+            if self.name < other.name:
+                return True
+            return False
+
+        return self.api < other.api
+
     @property
     def vendor_namespace(self):
         if self.__vendor_namespace is None:
diff --git a/tests/util/gen_dispatch.py b/tests/util/gen_dispatch.py
index 46869f8..6bfd6c0 100644
--- a/tests/util/gen_dispatch.py
+++ b/tests/util/gen_dispatch.py
@@ -39,7 +39,6 @@ PIGLIT_TOP_DIR = os.path.join(os.path.dirname(__file__), '..', '..')
 sys.path.append(PIGLIT_TOP_DIR)
 
 import registry.gl
-from registry.gl import Extension
 
 
 debug = False
@@ -142,45 +141,8 @@ class EnumCode(object):
 
     @classmethod
     def get_unique_enums_in_default_namespace(cls, gl_registry):
-        def cmp_enums(x, y):
-            # Sort enums by numerical value, then by vendor namespace, then by
-            # full name. Given a set of synonymous names for a given enum
-            # value, this sort order ensures that names provided by core
-            # specifications precede those provided by ratified extensions,
-            # which precede thos provided by unratified extensions.
-            #
-            # For example, GL_RED will precede GL_RED_EXT will precede
-            # GL_RED_INTEL.
-            #
-            c = cmp(x.num_value, y.num_value)
-            if c != 0:
-                return c
-
-            c = cmp(y.vendor_namespace is None,
-                    x.vendor_namespace is None)
-            if c != 0:
-                return c
-
-            c = cmp(y.vendor_namespace in Extension.RATIFIED_NAMESPACES,
-                    x.vendor_namespace in Extension.RATIFIED_NAMESPACES)
-            if c != 0:
-                return c
-
-            c = cmp(y.vendor_namespace == 'EXT',
-                    x.vendor_namespace == 'EXT')
-            if c != 0:
-                return c
-
-            c = cmp(x.name, y.name)
-            if c != 0:
-                return c
-
-            return cmp(x.api, y.api)
-
         def append_enum_if_new_value(enum_list, enum):
-            diff = cmp(enum_list[-1].num_value, enum.num_value)
-            assert(diff <= 0)
-            if diff < 0:
+            if enum_list[-1].num_value < enum.num_value:
                 enum_list.append(enum)
             return enum_list
 
@@ -190,7 +152,7 @@ class EnumCode(object):
             if enum_group.type == 'default_namespace'
             for enum in enum_group.enums
         )
-        enums = sorted(enums, cmp=cmp_enums)
+        enums = sorted(enums)
         enums = reduce(append_enum_if_new_value, enums[1:], [enums[0]])
         return enums
 
-- 
2.2.1



More information about the Piglit mailing list