bt2: pass custom Python object to Python component's __init__()
Just like you can pass custom data (`void *`) to the initialization
function of a component class written in C with the
bt_graph_add_*_component_with_init_method_data() functions, this patch
makes it possible to pass any Python object to the __init__() method of
a component class written in Python with the Graph.add_component()
method.
This patch installs a mechanism to share Python data between a Python
graph user and the methods of a Python component without relying on
`nonlocal`, global variables, or other such hacks. This data can be as
simple as an integer and as complex as a database connection, for
example.
The __init__() method of a Python component used to look like:
def __init__(self, params):
...
It's now:
def __init__(self, params, obj):
...
When you pass any Python object to Graph.add_component() as its `obj`
parameter, the Python component's __init__() method eventually receives
it.
Graph.add_component() ensures that the component class to instantiate is
a Python component class if `obj` is not `None`.
Internally, `None` gets converted to `NULL` as the `init_method_data`
parameter of the bt_graph_add_*_component_with_init_method_data()
functions, and then `NULL` gets converted back to `Py_None` in
component_class_init().
Now there is the risk that you call
bt_graph_add_*_component_with_init_method_data() in C with a Python
component class and pass a non-`NULL`, non-`PyObject *`
`init_method_data` parameter. However I consider an insignificant
drawback as you're not supposed to use the `init_method_data` with a
component class you don't know.
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ib879ece9e423b3495b9449ca73674082020865c5
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1815
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
19 files changed:
This page took 0.042587 seconds and 4 git commands to generate.