Fix: bt2: do not assign an exception to a local variable
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 18 Sep 2017 19:52:12 +0000 (15:52 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 18 Sep 2017 21:17:49 +0000 (17:17 -0400)
According to <https://docs.python.org/3/reference/compound_stmts.html#try>:

> Exceptions are cleared because with the traceback attached to them,
> they form a reference cycle with the stack frame, keeping all locals
> in that frame alive until the next garbage collection occurs.

According to <http://portingguide.readthedocs.io/en/latest/exceptions.html>:

> As discussed previously, in Python 3, all information about an
> exception, including the traceback, is contained in the exception
> object. Since the traceback holds references to the values of all
> local variables, storing an exception in a local variable usually
> forms a reference cycle, keeping all local variables allocated until
> the next garbage collection pass.

This reference cycle can make some tests fail because, with the
Babeltrace Python bindings, some operations are performed when a
Babeltrace native object is destroyed: this means the Python reference
count must be "stable" and as deterministic as possible so as to avoid
gc.collect() when using the bindings.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
bindings/python/bt2/bt2/utils.py

index 294fadb6edc91a4bc1952dab96f8f08545ed50ed..0c59a4d7aa973ed0e464423a9bc67a0a7a2abf95 100644 (file)
@@ -92,21 +92,17 @@ def _check_alignment(a):
         raise ValueError('{} is not a power of two'.format(a))
 
 
+def _raise_bt2_error(msg):
+    if msg is None:
+        raise bt2.Error
+    else:
+        raise bt2.Error(msg)
+
 def _handle_ret(ret, msg=None):
     if int(ret) < 0:
-        if msg is None:
-            error = bt2.Error()
-        else:
-            error = bt2.Error(msg)
-
-        raise error
+        _raise_bt2_error(msg)
 
 
 def _handle_ptr(ptr, msg=None):
     if ptr is None:
-        if msg is None:
-            error = bt2.Error()
-        else:
-            error = bt2.Error(msg)
-
-        raise error
+        _raise_bt2_error(msg)
This page took 0.026327 seconds and 4 git commands to generate.