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>
This page took 0.025543 seconds and 4 git commands to generate.