From 34af27f540340851aaedea9fb2ed5a8f96af2e63 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Mon, 18 Sep 2017 15:52:12 -0400 Subject: [PATCH] Fix: bt2: do not assign an exception to a local variable MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit According to : > 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 : > 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 Signed-off-by: Jérémie Galarneau --- bindings/python/bt2/bt2/utils.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/bindings/python/bt2/bt2/utils.py b/bindings/python/bt2/bt2/utils.py index 294fadb6..0c59a4d7 100644 --- a/bindings/python/bt2/bt2/utils.py +++ b/bindings/python/bt2/bt2/utils.py @@ -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) -- 2.34.1