Fix: bt2: adjust reference counting of value objects
[babeltrace.git] / src / bindings / python / bt2 / bt2 / value.py
index 6cea6cd59a69f88fdfd043179750d48ab5b64f16..2710d90330c8fa0849e055713d569fc8cf9f591f 100644 (file)
@@ -29,15 +29,15 @@ import abc
 import bt2
 
 
-def _handle_status(status, obj_name):
-    if status >= 0:
+def _create_from_ptr(ptr):
+    if ptr is None:
         return
-    else:
-        raise RuntimeError('unexpected error')
-
 
-def _create_from_ptr(ptr):
-    if ptr is None or ptr == native_bt.value_null:
+    # bt_value_null is translated to None.  However, we are given a reference
+    # to it that we are not going to manage anymore, since we don't create a
+    # Python wrapper for it.  Therefore put that reference immediately.
+    if ptr == native_bt.value_null:
+        bt2.value._Value._put_ref(ptr)
         return
 
     typeid = native_bt.value_get_type(ptr)
@@ -88,9 +88,6 @@ class _Value(object._SharedObject, metaclass=abc.ABCMeta):
     def __ne__(self, other):
         return not (self == other)
 
-    def _handle_status(self, status):
-        _handle_status(status, self._NAME)
-
     def _check_create_status(self, ptr):
         if ptr is None:
             raise bt2.CreationError(
@@ -370,7 +367,7 @@ class StringValue(collections.abc.Sequence, _Value):
 
     def _set_value(self, value):
         status = native_bt.value_string_set(self._ptr, self._value_to_str(value))
-        self._handle_status(status)
+        utils._handle_func_status(status)
 
     value = property(fset=_set_value)
 
@@ -473,7 +470,7 @@ class ArrayValue(_Container, collections.abc.MutableSequence, _Value):
 
         status = native_bt.value_array_set_element_by_index(
             self._ptr, index, ptr)
-        self._handle_status(status)
+        utils._handle_func_status(status)
 
     def append(self, value):
         value = create_value(value)
@@ -484,7 +481,7 @@ class ArrayValue(_Container, collections.abc.MutableSequence, _Value):
             ptr = value._ptr
 
         status = native_bt.value_array_append_element(self._ptr, ptr)
-        self._handle_status(status)
+        utils._handle_func_status(status)
 
     def __iadd__(self, iterable):
         # Python will raise a TypeError if there's anything wrong with
@@ -590,7 +587,7 @@ class MapValue(_Container, collections.abc.MutableMapping, _Value):
             ptr = value._ptr
 
         status = native_bt.value_map_insert_entry(self._ptr, key, ptr)
-        self._handle_status(status)
+        utils._handle_func_status(status)
 
     def __repr__(self):
         items = ['{}: {}'.format(repr(k), repr(v)) for k, v in self.items()]
This page took 0.023724 seconds and 4 git commands to generate.