diff options
author | Dylan Baker <baker.dylan.c@gmail.com> | 2015-01-13 16:28:51 -0800 |
---|---|---|
committer | Dylan Baker <baker.dylan.c@gmail.com> | 2015-01-28 12:08:10 -0800 |
commit | d57e9116ff6e80de96bdf54ef81277111cc7631b (patch) | |
tree | 4956513f53faaf9e2f7ee7e17b8ed6dad8f9993f | |
parent | 5fda6adfbaebe74962337c193ace342f352d24dc (diff) |
gen_dispatch.py: remove use of cmp.
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@intel.com>
-rw-r--r-- | registry/gl.py | 62 | ||||
-rw-r--r-- | tests/util/gen_dispatch.py | 42 |
2 files changed, 64 insertions, 40 deletions
diff --git a/registry/gl.py b/registry/gl.py index 99f4a410a..2f8cda0e3 100644 --- a/registry/gl.py +++ b/registry/gl.py @@ -1076,6 +1076,7 @@ class EnumGroup(object): self.type = 'special' +@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 (ratified first, then + EXT), then by full name, and finally by api. + + This sort order ensures that names provided by core specifications + precede those provided by ratified extensions, which proceed 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 34fd83f63..c3cbd2aa8 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 @@ -141,45 +140,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 @@ -189,7 +151,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 |