Skip to content

Commit c9d8405

Browse files
author
Thomas Preud'homme
committed
[LNT] Python 3 support: define hash for Order class
Summary: Method v4_machine in lnt.server.ui.views stores couples of Order and Run instances in a multidict, thus making Order the key. This requires instances of the Order class to be hashable but hashability rules have changed under Python 3. As per the Python's data model documentation regarding the __hash__ method, user-defined classes have a default __hash__ method making their instances hashable. However, under Python 3 if such a user-defined class overrides __eq__ but not __hash__ (as is done by the class lnt.server.db.testsuite.TestSuiteDB.Order) the __hash__ method is set to none making it unhashable. Under Python 2 one would have to explicitely set __hash__ to None in the class definition for such a class to be unhashable. This commit adds a __hash__ method for lnt.server.db.testsuite.TestSuiteDB.Order class to allow it to be used as key in a multidict. Reviewers: cmatthews, hubert.reinterpretcast, kristof.beyls, leandron, PrzemekWirkus Reviewed By: cmatthews Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D69042
1 parent 6505c12 commit c9d8405

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

lnt/server/db/testsuitedb.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,15 @@ def _get_comparison_discriminant(self, b):
304304
(0, 0),
305305
)
306306

307+
def __hash__(self):
308+
converted_fields = map(
309+
lambda item: convert_revision(
310+
self.get_field(item), cache=Order.order_name_cache
311+
),
312+
self.fields,
313+
)
314+
return hash(tuple(converted_fields))
315+
307316
def __eq__(self, b):
308317
discriminant = self._get_comparison_discriminant(b)
309318
return discriminant[0] == discriminant[1]

0 commit comments

Comments
 (0)