bt2: pass custom Python object to Python component's __init__()
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Sat, 3 Aug 2019 18:36:42 +0000 (14:36 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 7 Aug 2019 18:05:21 +0000 (14:05 -0400)
commitb20382e2b6f7f373c838a1eed82c2635d606a6b3
treebd5ba34adba38c9c880898eab41a3a760f28f415
parent774b7f288e268259639df192409a348c32d1f57a
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:
src/bindings/python/bt2/bt2/component.py
src/bindings/python/bt2/bt2/graph.py
src/bindings/python/bt2/bt2/native_bt_component_class.i
src/bindings/python/bt2/bt2/native_bt_component_class.i.h
src/bindings/python/bt2/bt2/native_bt_graph.i
src/bindings/python/bt2/bt2/native_bt_graph.i.h
src/bindings/python/bt2/bt2/trace_collection_message_iterator.py
tests/bindings/python/bt2/test_clock_class.py
tests/bindings/python/bt2/test_component.py
tests/bindings/python/bt2/test_connection.py
tests/bindings/python/bt2/test_error.py
tests/bindings/python/bt2/test_event.py
tests/bindings/python/bt2/test_graph.py
tests/bindings/python/bt2/test_message.py
tests/bindings/python/bt2/test_message_iterator.py
tests/bindings/python/bt2/test_port.py
tests/bindings/python/bt2/utils.py
tests/data/cli/convert/auto-source-discovery-grouping/bt_plugin_test.py
tests/data/plugins/flt.utils.trimmer/bt_plugin_trimmer_test.py
This page took 0.030103 seconds and 4 git commands to generate.