bt2: create_value(): check `numbers` and `collections.abc` types
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 28 Jun 2019 05:16:21 +0000 (01:16 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 3 Jul 2019 01:31:27 +0000 (21:31 -0400)
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 <eeppeliteloop@gmail.com>
Change-Id: I93902e58b6adf3f9cd5c03c7c422148d577b5f0f
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1571
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
src/bindings/python/bt2/bt2/value.py

index 81a72ffa785e7d735736e839714c30db69dfd315..6cea6cd59a69f88fdfd043179750d48ab5b64f16 100644 (file)
@@ -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__))
 
This page took 0.027903 seconds and 4 git commands to generate.