bt2: add `__hash__()` method on hashable fields
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Wed, 11 Sep 2019 21:07:31 +0000 (17:07 -0400)
committerFrancis Deslauriers <francis.deslauriers@efficios.com>
Thu, 12 Sep 2019 16:17:29 +0000 (12:17 -0400)
commitdda659b36bc8a3a9f477d6bbc57cb7eb10c614a6
treec9a29d127df997dcf3cd3ac50a393cccef4bc6e6
parent3b75a1f2d5226a056e4e14a08c37edfac7b78336
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.026584 seconds and 4 git commands to generate.