Commit e7e4f177 authored by Raul Tambre's avatar Raul Tambre Committed by Commit Bot

name_style_converter: Define __hash__() method

__hash__() isn't defined by default in Python 3 if __eq__() is
user-defined.
This fixes NameStyleConverter being unhashable in Python 3.

The default __hash__() in Python 2 is defined in terms of id(self),
i.e. identity-based hashing.
However, since the custom __eq__() is defined in terms of value
equality, this is probably unintended and a bug.
Thus this CL also changes behaviour by defining __hash__() in terms of
hash(self.original), i.e. value-based hashing.

Traceback (most recent call last):
  File "../../third_party/blink/renderer/build/scripts/make_feature_policy_helper.py", line 80, in <module>
    json5_generator.Maker(FeaturePolicyFeatureWriter).main()
  File "C:\Google\chromium\src\third_party\blink\renderer\build\scripts\json5_generator.py", line 337, in main
    writer = self._writer_class(args.files, args.output_dir)
  File "../../third_party/blink/renderer/build/scripts/make_feature_policy_helper.py", line 44, in __init__
    fp_origin_trial_dependency_map[feature['name']].append(
TypeError: unhashable type: 'NameStyleConverter'

Bug: 941669
Change-Id: Ie7811c7a5e0620376bf01c0d91a2c14043c85293
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2093491
Commit-Queue: Raul Tambre <raul@tambre.ee>
Reviewed-by: default avatarKent Tamura <tkent@chromium.org>
Auto-Submit: Raul Tambre <raul@tambre.ee>
Cr-Commit-Position: refs/heads/master@{#748123}
parent a3498f26
...@@ -134,6 +134,10 @@ class NameStyleConverter(object): ...@@ -134,6 +134,10 @@ class NameStyleConverter(object):
def __eq__(self, other): def __eq__(self, other):
return self.original == other.original return self.original == other.original
# If __eq__() is defined then a custom __hash__() needs to be defined.
def __hash__(self):
return hash(self.original)
def to_snake_case(self): def to_snake_case(self):
"""Snake case is the file and variable name style per Google C++ Style """Snake case is the file and variable name style per Google C++ Style
Guide: Guide:
......
...@@ -200,5 +200,16 @@ class NameStyleConverterTest(unittest.TestCase): ...@@ -200,5 +200,16 @@ class NameStyleConverterTest(unittest.TestCase):
converter = NameStyleConverter('third_party/blink/renderer/bindings/modules/v8/v8_path_2d.h') converter = NameStyleConverter('third_party/blink/renderer/bindings/modules/v8/v8_path_2d.h')
self.assertEqual(converter.to_header_guard(), 'THIRD_PARTY_BLINK_RENDERER_BINDINGS_MODULES_V8_V8_PATH_2D_H_') self.assertEqual(converter.to_header_guard(), 'THIRD_PARTY_BLINK_RENDERER_BINDINGS_MODULES_V8_V8_PATH_2D_H_')
def test_equality(self):
a_1 = NameStyleConverter('a')
a_2 = NameStyleConverter('a')
c = NameStyleConverter('c')
self.assertEqual(a_1, a_2)
self.assertNotEqual(a_1, c)
self.assertEqual(hash(a_1), hash(a_2))
self.assertNotEqual(hash(a_1), hash(c))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment