[PATCH] D69042: [LNT] Python 3 support: define hash for Order class

Thomas Preud'homme via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 16 08:18:16 PDT 2019


thopre created this revision.
thopre added reviewers: cmatthews, hubert.reinterpretcast, kristof.beyls.
thopre added a parent revision: D68989: [LNT] Python 3 support: don't assume URL params order.

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.


https://reviews.llvm.org/D69042

Files:
  lnt/server/db/testsuitedb.py


Index: lnt/server/db/testsuitedb.py
===================================================================
--- lnt/server/db/testsuitedb.py
+++ lnt/server/db/testsuitedb.py
@@ -304,6 +304,15 @@
                     (0, 0),
                 )
 
+            def __hash__(self):
+                converted_fields = map(
+                    lambda item: convert_revision(
+                        self.get_field(item), cache=Order.order_name_cache
+                    ),
+                    self.fields,
+                )
+                return hash(tuple(converted_fields))
+
             def __eq__(self, b):
                 discriminant = self._get_comparison_discriminant(b)
                 return discriminant[0] == discriminant[1]


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69042.225229.patch
Type: text/x-patch
Size: 738 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191016/93ecf3dd/attachment.bin>


More information about the llvm-commits mailing list