Calling C++ method from Python

Hi,

I have worked my way through the Guided Tutorials on gnuradio.org. I
have written OOT blocks in Python and managed to expose some set methods
in the block as callbacks. Now I have written an OOT C++ block
sweeper_cpp_f and I want to expose a couple of setter methods in my
block’s class as callbacks that can be invoked from Python. Initially I
just defined the methods in my sweeper_cpp_f_impl.cc file and added the
following to the matching XML file:

set_direction($direction)
set_trigger_level($trigger_level)

This builds fine and I am able to use the block in a GRC flow-graph. But
at runtime when one of the above methods is invoked as a result of GUI
input I get an error message:

File “/home/wpats/ettus/grc/new_sweep_cpp.py”, line 141, in
set_variable_chooser_direction
self.tutorial_sweeper_cpp_f_0.set_direction(self.variable_chooser_direction)
AttributeError: ‘sweeper_cpp_f_sptr’ object has no attribute
‘set_direction’

I looked at built in module sig_source_X_impl.h.t and
sig_source_X_impl.cc.t (this is the analog signal source block) in
gr-analog as an example for what I am trying to accomplish. I converted
my sources into a similar format and modified the CMakeLists.txt in the
…/gr-tutorial/lib directory to add:

########################################################################

Invoke macro to generate various sources and headers

########################################################################
include(GrMiscUtils)
GR_EXPAND_X_CC_H(tutorial sweeper_cpp_X_impl f)

This again builds fine after I edit the generate_helper.py generated
script to find an import module. I assumed that the above would do the
necessary magic to generate the wrappers for my callback methods but no
such luck. I still get the attribute error when invoking my C++ method.

What is the right way to do this ? Any help/suggestions/pointers would
be appreciated. I can’t be the first one to try to do this seemingly
simple task…

Thanks,

–Patrick

Hi Patrick,

the typical problem with C++ blocks is that people add public methods to
their _impl, but forget to declare these same methods in their public
non-impl class.
Have you done the same expand magic with the header in your
include/CMakeLists.txt?

Best regards,
Marcus

Thanks much, Marcus. That was the issue. I had missed adding the
declarations for the callbacks in the non-impl class. As in the analog
signal source block, just adding pure virtual function declarations for
the callbacks did the trick.

I’m not familiar with SWIG and I don’t know why this works. But I’m
happy it does without having to dig into SWIG details.

Thanks,

–Patrick