bt2: let components attach "user data" to ports
authorSimon Marchi <simon.marchi@efficios.com>
Fri, 31 May 2019 12:59:04 +0000 (08:59 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 5 Jun 2019 17:47:34 +0000 (13:47 -0400)
commit2e00bc76cf37af167c45253cfc8f8d197222d6b8
treec80622480a58bb8ae1a62ca511f64ca28e38caea
parentc5f330cd909f5dfbdb519546e875b4427434ba4f
bt2: let components attach "user data" to ports

The C API allows components to attach "user data" to ports when creating
them:

    extern bt_self_component_status
    bt_self_component_source_add_output_port(
                    bt_self_component_source *self_component,
                    const char *name, void *user_data,
                    bt_self_component_port_output **self_component_port);

This is useful to identify the purpose of a given port.  This data can later be
fetched when an iterator is created for that port.

This patch makes the Python API offer a similar facility.  When adding a port,
the user can optionally pass an arbitrary Python object as user data:

    self_port = self._add_output_port('port name', user_data={'foo': 23})

which they can then access using the user_data property of
_UserComponentPort:

    print(self_port.user_data)  # {'foo': 23}

We confine the user data under the user_data property to avoid
clashes with future methods and properties that we might add to
_UserComponentPort or its subclasses.

A new "in" typemap is created for the "add port" functions to pass the
PyObject* representing the user data Python object to the creation
function.  Without the typemap, SWIG complains that the passed value (a
PyObject *) is not of the right type (it expects a void *):

    TypeError: in method 'self_component_source_add_output_port', argument 3 of type 'void *'

If the port is created successfully, it now owns a reference to this
Python object.  We must reflect that in the Python object's refcount,
this is done through an "argout" typemap.  In this typemap, we need to
check the return value of the function we called.

When fetching the user data of a port, we use an "out" typemap to
increment the refcount of the returned value.  This is because a Python
method that returns an object must return a new reference that is
transferred to the caller.

Change-Id: I0b83454a81e71bd7c2fe9449c7fc65c09f18fcf4
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1359
Tested-by: jenkins
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
bindings/python/bt2/bt2/component.py
bindings/python/bt2/bt2/native_bt_component.i
bindings/python/bt2/bt2/native_bt_port.i
bindings/python/bt2/bt2/port.py
tests/bindings/python/bt2/test_message_iterator.py
tests/bindings/python/bt2/test_port.py
This page took 0.026938 seconds and 4 git commands to generate.