Commit | Line | Data |
---|---|---|
d2d857a8 MJ |
1 | # |
2 | # Copyright (C) 2019 EfficiOS Inc. | |
3 | # | |
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; only version 2 | |
7 | # of the License. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. | |
13 | # | |
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 | # | |
18 | ||
fbbe9302 SM |
19 | import bt2 |
20 | ||
21 | # Run callable `func` in the context of a component's __init__ method. The | |
22 | # callable is passed the Component being instantiated. | |
23 | # | |
24 | # The value returned by the callable is returned by run_in_component_init. | |
25 | ||
cfbd7cf3 | 26 | |
fbbe9302 SM |
27 | def run_in_component_init(func): |
28 | class MySink(bt2._UserSinkComponent): | |
29 | def __init__(self, params): | |
30 | nonlocal res_bound | |
31 | res_bound = func(self) | |
32 | ||
33 | def _consume(self): | |
34 | pass | |
35 | ||
a01b452b SM |
36 | def _graph_is_configured(self): |
37 | pass | |
38 | ||
fbbe9302 SM |
39 | g = bt2.Graph() |
40 | res_bound = None | |
41 | g.add_component(MySink, 'comp') | |
42 | ||
43 | # We deliberately use a different variable for returning the result than | |
44 | # the variable bound to the MySink.__init__ context and delete res_bound. | |
45 | # The MySink.__init__ context stays alive until the end of the program, so | |
46 | # if res_bound were to still point to our result, it would contribute an | |
47 | # unexpected reference to the refcount of the result, from the point of view | |
48 | # of the user of this function. It would then affect destruction tests, | |
49 | # for example, which want to test what happens when the refcount of a Python | |
50 | # object reaches 0. | |
51 | ||
52 | res = res_bound | |
53 | del res_bound | |
54 | return res | |
55 | ||
cfbd7cf3 | 56 | |
fbbe9302 SM |
57 | # Create an empty trace class with default values. |
58 | ||
cfbd7cf3 | 59 | |
fbbe9302 SM |
60 | def get_default_trace_class(): |
61 | def f(comp_self): | |
62 | return comp_self._create_trace_class() | |
63 | ||
64 | return run_in_component_init(f) |