Skip to content

Commit ec9a5ed

Browse files
author
Thomas Preud'homme
committed
[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 llvm-svn: 373141
1 parent 1be2140 commit ec9a5ed

File tree

4 files changed

+59
-17
lines changed

4 files changed

+59
-17
lines changed

lnt/external/stats/pstat.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,8 @@ def arecode (a,listmap,col='all'):
953953
def arowcompare(row1, row2):
954954
"""
955955
Compares two rows from an array, regardless of whether it is an
956-
array of numbers or of python objects (which requires the cmp function).
956+
array of numbers or of python objects (which requires rich comparison
957+
method __eq__).
957958
@@@PURPOSE? 2007-11-26
958959
959960
Usage: arowcompare(row1,row2)
@@ -962,7 +963,7 @@ def arowcompare(row1, row2):
962963
"""
963964
return
964965
if row1.dtype.char=='O' or row2.dtype=='O':
965-
cmpvect = N.logical_not(abs(N.array(list(map(cmp, row1, row2))))) # cmp fcn gives -1,0,1
966+
cmpvect = N.array([x == y for x, y in zip(row1, row2)])
966967
else:
967968
cmpvect = N.equal(row1,row2)
968969
return cmpvect
@@ -971,7 +972,8 @@ def arowcompare(row1, row2):
971972
def arowsame(row1, row2):
972973
"""
973974
Compares two rows from an array, regardless of whether it is an
974-
array of numbers or of python objects (which requires the cmp function).
975+
array of numbers or of python objects (which requires rich comparison
976+
method __eq__).
975977
976978
Usage: arowsame(row1,row2)
977979
Returns: 1 if the two rows are identical, 0 otherwise.
@@ -1021,8 +1023,8 @@ def aunique(inarray):
10211023
else: # must be an Object array, alltrue/equal functions don't work
10221024
for item in inarray[1:]:
10231025
newflag = 1
1024-
for unq in uniques: # NOTE: cmp --> 0=same, -1=<, 1=>
1025-
test = N.sum(abs(N.array(list(map(cmp, item, unq)))))
1026+
for unq in uniques:
1027+
test = N.sum(N.array([x == y for x, y in zip(item, unq)]))
10261028
if test == 0: # if item identical to any 1 row in uniques
10271029
newflag = 0 # then not a novel item to add
10281030
break

lnt/server/db/testsuitedb.py

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import datetime
1010
import json
1111
import os
12+
import itertools
1213

1314
import aniso8601
1415
import sqlalchemy
@@ -208,8 +209,8 @@ class Order(self.base, ParameterizedMixin):
208209
__tablename__ = db_key_name + '_Order'
209210

210211
# We guarantee that our fields are stored in the order they are
211-
# supposed to be lexicographically compared, the __cmp__ method
212-
# relies on this.
212+
# supposed to be lexicographically compared, the rich comparison
213+
# methods rely on this.
213214
fields = sorted(self.order_fields,
214215
key=lambda of: of.ordinal)
215216

@@ -274,17 +275,58 @@ def as_ordered_string(self):
274275
def name(self):
275276
return self.as_ordered_string()
276277

277-
def __cmp__(self, b):
278+
def _get_comparison_discriminant(self, b):
279+
"""Return a representative pair of converted revision from self
280+
and b. Order of the element on this pair is the same as the
281+
order of self relative to b.
282+
"""
278283
# SA occasionally uses comparison to check model instances
279-
# verse some sentinels, so we ensure we support comparison
284+
# versus some sentinels, so we ensure we support comparison
280285
# against non-instances.
281286
if self.__class__ is not b.__class__:
282-
return -1
283-
# Compare every field in lexicographic order.
284-
return cmp([convert_revision(self.get_field(item), cache=Order.order_name_cache)
285-
for item in self.fields],
286-
[convert_revision(b.get_field(item), cache=Order.order_name_cache)
287-
for item in self.fields])
287+
return (0, 1)
288+
289+
# Pair converted revision from self and b.
290+
converted_revisions = map(
291+
lambda item: (
292+
convert_revision(
293+
self.get_field(item), cache=Order.order_name_cache
294+
),
295+
convert_revision(
296+
b.get_field(item), cache=Order.order_name_cache
297+
),
298+
),
299+
self.fields,
300+
)
301+
# Return the first unequal pair, or (0, 0) otherwise.
302+
return next(
303+
itertools.dropwhile(lambda x: x[0] == x[1], converted_revisions),
304+
(0, 0),
305+
)
306+
307+
def __eq__(self, b):
308+
discriminant = self._get_comparison_discriminant(b)
309+
return discriminant[0] == discriminant[1]
310+
311+
def __ne__(self, b):
312+
discriminant = self._get_comparison_discriminant(b)
313+
return discriminant[0] != discriminant[1]
314+
315+
def __lt__(self, b):
316+
discriminant = self._get_comparison_discriminant(b)
317+
return discriminant[0] < discriminant[1]
318+
319+
def __le__(self, b):
320+
discriminant = self._get_comparison_discriminant(b)
321+
return discriminant[0] <= discriminant[1]
322+
323+
def __gt__(self, b):
324+
discriminant = self._get_comparison_discriminant(b)
325+
return discriminant[0] > discriminant[1]
326+
327+
def __ge__(self, b):
328+
discriminant = self._get_comparison_discriminant(b)
329+
return discriminant[0] >= discriminant[1]
288330

289331
def __json__(self, include_id=True):
290332
result = {}

tests/server/ui/test_api_modify.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,5 +226,4 @@ def test_04_merge_into(self):
226226

227227

228228
if __name__ == '__main__':
229-
unittest.TestLoader.sortTestMethodsUsing = lambda _, x, y: cmp(x, y)
230229
unittest.main(argv=[sys.argv[0], ])

tests/server/ui/test_api_roundtrip.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,4 @@ def test_roundtrip(self):
5757

5858

5959
if __name__ == '__main__':
60-
unittest.TestLoader.sortTestMethodsUsing = lambda _, x, y: cmp(x, y)
6160
unittest.main(argv=[sys.argv[0], ])

0 commit comments

Comments
 (0)