From 665658c2f8c04a8616e0f95ffa5d56e8c12f9668 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Fri, 28 Jun 2019 01:16:21 -0400 Subject: [PATCH] bt2: create_value(): check `numbers` and `collections.abc` types This is just like what's done within _NumericValue._extract_value(), _IntegerValue._value_to_int(), RealValue._value_to_float(), StringValue._value_to_str(), ArrayValue.__eq__(), and MapValue.__eq__(). It is more versatile than checking for the specific built-in types. Signed-off-by: Philippe Proulx Change-Id: I93902e58b6adf3f9cd5c03c7c422148d577b5f0f Reviewed-on: https://review.lttng.org/c/babeltrace/+/1571 Tested-by: jenkins Reviewed-by: Francis Deslauriers Reviewed-by: Simon Marchi --- src/bindings/python/bt2/bt2/value.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/bindings/python/bt2/bt2/value.py b/src/bindings/python/bt2/bt2/value.py index 81a72ffa..6cea6cd5 100644 --- a/src/bindings/python/bt2/bt2/value.py +++ b/src/bindings/python/bt2/bt2/value.py @@ -63,24 +63,20 @@ def create_value(value): if isinstance(value, bool): return BoolValue(value) - if isinstance(value, int): + if isinstance(value, numbers.Integral): return SignedIntegerValue(value) - if isinstance(value, float): + if isinstance(value, numbers.Real): return RealValue(value) if isinstance(value, str): return StringValue(value) - try: - return MapValue(value) - except: - pass - - try: + if isinstance(value, collections.abc.Sequence): return ArrayValue(value) - except: - pass + + if isinstance(value, collections.abc.Mapping): + return MapValue(value) raise TypeError("cannot create value object from '{}' object".format(value.__class__.__name__)) -- 2.34.1