bt2: add `__hash__()` method on hashable fields
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Wed, 11 Sep 2019 21:07:31 +0000 (17:07 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 3 Oct 2019 14:45:10 +0000 (10:45 -0400)
commit04068900f641f9797316de2500a98abe644202fd
treed734b7331b5fcc4b9abc07dd8dcf721d473ef8c4
parent58f3596b78ad80a8c47e5f04ded46c37b5a5a38d
bt2: add `__hash__()` method on hashable fields

Implement the `__hash__()` method of selected fields so that they can be
used as keys in dictionaries. This commit only implements the
`__hash__()` method of _NumericFieldConst and _StringFieldConst fields
after considering the following facts:
  1. "[...] the implementation of hashable collections requires that a key’s
    hash value is immutable (if the object’s hash value changes, it will be
    in the wrong hash bucket)." [1],
  2. Python built-in types bool, int, float, and string are hashable,
      and sequences and dictionaries are not.

So I suggest that only const fields should be hashable as they are
immutable, and that Container fields (variant, static array, etc.) should not
be hashable (even if they are const) to be consistent with Python
built-in types.

[1]: https://docs.python.org/3/reference/datamodel.html?highlight=__hash__#object.__hash__

Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Change-Id: I3dc246802d628603a3f06ecd3a87b7e38b3f1e27
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2032
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
src/bindings/python/bt2/bt2/field.py
tests/bindings/python/bt2/test_field.py
This page took 0.026223 seconds and 4 git commands to generate.