yarp icon indicating copy to clipboard operation
yarp copied to clipboard

Python bindings: call to Port.setReporter() raises TypeError

Open hypothe opened this issue 3 years ago • 1 comments
trafficstars

Describe the bug I'm trying to attach a custom PortReporter to a Port using the python bindings, but a TypeError is raised complaining about the type of the PortReporter.

To Reproduce Minimal script to reproduce:

import yarp

class MyPortReport(yarp.PortReport):
    def __init__(self, *args, **kwargs):
        print("MyPortReport INIT")
    def report(self, info):
        print("MyPortReport REPORT")

port = yarp.Port()
report = MyPortReport()

port.setReporter(report)

The creation goes through smoothly, but the call to setReporter raises:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/src/robot/robotology-superbuild/build/install/lib/python3/dist-packages/yarp.py", line 1532, in setReporter
    return _yarp.Port_setReporter(self, reporter)
TypeError: in method 'Port_setReporter', argument 2 of type 'yarp::os::PortReport &'

Configuration:

  • OS: Ubuntu 20.04
  • yarp version: 3.6.0
  • compiler: ///

hypothe avatar Sep 29 '22 09:09 hypothe

Hello @hypothe ! The callback machinery in SWIG is not immediate, as it requires the use of the "directors" feature, see https://www.swig.org/Doc4.0/SWIGDocumentation.html#SWIGPlus_target_language_callbacks and https://www.swig.org/Doc4.0/SWIGDocumentation.html#Python_nn33 .

At a first glance, it seems that callback in Python are working fine for the useCallback function of yarp::os::BufferedPort, see https://github.com/robotology/yarp/blob/ea6ed180ddb102679d8b8492fc40329719d7b8e4/bindings/python/examples/example_callback.py#L32 . Probably this is due to the lines in yarp.i such as https://github.com/robotology/yarp/blob/77612cb28a35288e38daa3a6c8e1f915d8b05be6/bindings/yarp.i#L375 and the following lines.

Perhaps adding %feature("director") yarp::os::PortReport; in https://github.com/robotology/yarp/blob/77612cb28a35288e38daa3a6c8e1f915d8b05be6/bindings/yarp.i#L66 and recompiling and install YARP and the YARP python bindings could fix the problem. If you are able to test it and it works, feel free to submit a PR, thanks!

traversaro avatar Sep 29 '22 09:09 traversaro