[LNT] r373141 - [LNT] Python 3 support: get rid of calls to cmp builtin

Thomas Preud'homme via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 28 00:12:58 PDT 2019


Author: thopre
Date: Sat Sep 28 00:12:58 2019
New Revision: 373141

URL: http://llvm.org/viewvc/llvm-project?rev=373141&view=rev
Log:
[LNT] Python 3 support: get rid of calls to cmp builtin

Get rid of calls to cmp builtins since it was removed in Python 3:
- for the tests, the assignment to sortTestMethodsUsing is removed
  altogether since the default is the built-in ordering for strings;
- for the call in the TestsuiteDB.Order class, we define the rich
  comparison methods that have replaced cmp;
- for the stats module, we use equality expressions since only the
  equality value is needed.

Note that one of the uses of cmp in the stats module is dead but removal
of the dead code is kept for future work to keep a separation of concern
between commits.

Reviewers: cmatthews, hubert.reinterpretcast, kristof.beyls

Reviewed By: hubert.reinterpretcast

Subscribers: mehdi_amini, dexonsmith, llvm-commits

Differential Revision: https://reviews.llvm.org/D67820

Modified:
    lnt/trunk/lnt/external/stats/pstat.py
    lnt/trunk/lnt/server/db/testsuitedb.py
    lnt/trunk/tests/server/ui/test_api_modify.py
    lnt/trunk/tests/server/ui/test_api_roundtrip.py

Modified: lnt/trunk/lnt/external/stats/pstat.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/external/stats/pstat.py?rev=373141&r1=373140&r2=373141&view=diff
==============================================================================
--- lnt/trunk/lnt/external/stats/pstat.py (original)
+++ lnt/trunk/lnt/external/stats/pstat.py Sat Sep 28 00:12:58 2019
@@ -953,7 +953,8 @@ Returns: a version of array a where list
  def arowcompare(row1, row2):
     """
 Compares two rows from an array, regardless of whether it is an
-array of numbers or of python objects (which requires the cmp function).
+array of numbers or of python objects (which requires rich comparison
+method __eq__).
 @@@PURPOSE? 2007-11-26
 
 Usage:   arowcompare(row1,row2)
@@ -962,7 +963,7 @@ Returns: an array of equal length contai
 """
     return 
     if row1.dtype.char=='O' or row2.dtype=='O':
-        cmpvect = N.logical_not(abs(N.array(list(map(cmp, row1, row2))))) # cmp fcn gives -1,0,1
+        cmpvect = N.array([x == y for x, y in zip(row1, row2)])
     else:
         cmpvect = N.equal(row1,row2)
     return cmpvect
@@ -971,7 +972,8 @@ Returns: an array of equal length contai
  def arowsame(row1, row2):
     """
 Compares two rows from an array, regardless of whether it is an
-array of numbers or of python objects (which requires the cmp function).
+array of numbers or of python objects (which requires rich comparison
+method __eq__).
 
 Usage:   arowsame(row1,row2)
 Returns: 1 if the two rows are identical, 0 otherwise.
@@ -1021,8 +1023,8 @@ Usage:   aunique (inarray)
         else:   # must be an Object array, alltrue/equal functions don't work
             for item in inarray[1:]:
                 newflag = 1
-                for unq in uniques:  # NOTE: cmp --> 0=same, -1=<, 1=>
-                    test = N.sum(abs(N.array(list(map(cmp, item, unq)))))
+                for unq in uniques:
+                    test = N.sum(N.array([x == y for x, y in zip(item, unq)]))
                     if test == 0:   # if item identical to any 1 row in uniques
                         newflag = 0 # then not a novel item to add
                         break

Modified: lnt/trunk/lnt/server/db/testsuitedb.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/testsuitedb.py?rev=373141&r1=373140&r2=373141&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/testsuitedb.py (original)
+++ lnt/trunk/lnt/server/db/testsuitedb.py Sat Sep 28 00:12:58 2019
@@ -9,6 +9,7 @@ from __future__ import absolute_import
 import datetime
 import json
 import os
+import itertools
 
 import aniso8601
 import sqlalchemy
@@ -208,8 +209,8 @@ class TestSuiteDB(object):
             __tablename__ = db_key_name + '_Order'
 
             # We guarantee that our fields are stored in the order they are
-            # supposed to be lexicographically compared, the __cmp__ method
-            # relies on this.
+            # supposed to be lexicographically compared, the rich comparison
+            # methods rely on this.
             fields = sorted(self.order_fields,
                             key=lambda of: of.ordinal)
 
@@ -274,17 +275,58 @@ class TestSuiteDB(object):
             def name(self):
                 return self.as_ordered_string()
 
-            def __cmp__(self, b):
+            def _get_comparison_discriminant(self, b):
+                """Return a representative pair of converted revision from self
+                and b. Order of the element on this pair is the same as the
+                order of self relative to b.
+                """
                 # SA occasionally uses comparison to check model instances
-                # verse some sentinels, so we ensure we support comparison
+                # versus some sentinels, so we ensure we support comparison
                 # against non-instances.
                 if self.__class__ is not b.__class__:
-                    return -1
-                # Compare every field in lexicographic order.
-                return cmp([convert_revision(self.get_field(item), cache=Order.order_name_cache)
-                            for item in self.fields],
-                           [convert_revision(b.get_field(item),  cache=Order.order_name_cache)
-                            for item in self.fields])
+                    return (0, 1)
+
+                # Pair converted revision from self and b.
+                converted_revisions = map(
+                    lambda item: (
+                        convert_revision(
+                            self.get_field(item), cache=Order.order_name_cache
+                        ),
+                        convert_revision(
+                            b.get_field(item), cache=Order.order_name_cache
+                        ),
+                    ),
+                    self.fields,
+                )
+                # Return the first unequal pair, or (0, 0) otherwise.
+                return next(
+                    itertools.dropwhile(lambda x: x[0] == x[1], converted_revisions),
+                    (0, 0),
+                )
+
+            def __eq__(self, b):
+                discriminant = self._get_comparison_discriminant(b)
+                return discriminant[0] == discriminant[1]
+
+            def __ne__(self, b):
+                discriminant = self._get_comparison_discriminant(b)
+                return discriminant[0] != discriminant[1]
+
+            def __lt__(self, b):
+                discriminant = self._get_comparison_discriminant(b)
+                return discriminant[0] < discriminant[1]
+
+            def __le__(self, b):
+                discriminant = self._get_comparison_discriminant(b)
+                return discriminant[0] <= discriminant[1]
+
+            def __gt__(self, b):
+                discriminant = self._get_comparison_discriminant(b)
+                return discriminant[0] > discriminant[1]
+
+            def __ge__(self, b):
+                discriminant = self._get_comparison_discriminant(b)
+                return discriminant[0] >= discriminant[1]
 
             def __json__(self, include_id=True):
                 result = {}

Modified: lnt/trunk/tests/server/ui/test_api_modify.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/tests/server/ui/test_api_modify.py?rev=373141&r1=373140&r2=373141&view=diff
==============================================================================
--- lnt/trunk/tests/server/ui/test_api_modify.py (original)
+++ lnt/trunk/tests/server/ui/test_api_modify.py Sat Sep 28 00:12:58 2019
@@ -226,5 +226,4 @@ Deleted machine machine2:2
 
 
 if __name__ == '__main__':
-    unittest.TestLoader.sortTestMethodsUsing = lambda _, x, y: cmp(x, y)
     unittest.main(argv=[sys.argv[0], ])

Modified: lnt/trunk/tests/server/ui/test_api_roundtrip.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/tests/server/ui/test_api_roundtrip.py?rev=373141&r1=373140&r2=373141&view=diff
==============================================================================
--- lnt/trunk/tests/server/ui/test_api_roundtrip.py (original)
+++ lnt/trunk/tests/server/ui/test_api_roundtrip.py Sat Sep 28 00:12:58 2019
@@ -57,5 +57,4 @@ class JSONAPIDeleteTester(unittest.TestC
 
 
 if __name__ == '__main__':
-    unittest.TestLoader.sortTestMethodsUsing = lambda _, x, y: cmp(x, y)
     unittest.main(argv=[sys.argv[0], ])




More information about the llvm-commits mailing list