From: discuss-gnuradio-bounces+sean.nowlan=gtri.gatech....@gnu.org 
[mailto:discuss-gnuradio-bounces+sean.nowlan=gtri.gatech....@gnu.org] On Behalf 
Of Nowlan, Sean
Sent: Monday, February 23, 2015 3:10 PM
To: discuss-gnuradio@gnu.org
Subject: [Discuss-gnuradio] Trouble with SWIG for packet_formatter_default 
child class

Hi all -

I'm working on Tom's packet_handling branch (github.com/trondeau/gnuradio.git) 
and building a custom packet formatter. Everything works in C++ land, including 
QA code. However, SWIG is complaining about undeclared things. I duplicated the 
CMake and SWIG structures of gnuradio/gr-digital/swig/ for building derived 
classes such as gr::digital::packet_formatter_counter. Any ideas why this would 
not work? Thanks!

---- first few lines of make errors ----

/home/me/code/gr-myoot/build/swig/myoot_swigPYTHON_wrap.cxx: In function 
'PyObject* _wrap_packet_formatter_custom_make(PyObject*, PyObject*, PyObject*)':
/home/me/code/gr-myoot/build/swig/myoot_swigPYTHON_wrap.cxx:5030:3: error: 
'sptr' was not declared in this scope
   sptr result;
   ^
/home/me/code/gr-myoot/build/swig/myoot_swigPYTHON_wrap.cxx:5030:8: error: 
expected ';' before 'result'
   sptr result;
        ^
/home/me/code/gr-myoot/build/swig/myoot_swigPYTHON_wrap.cxx:5062:7: error: 
'result' was not declared in this scope
       result = gr::myoot::packet_formatter_custom::make((std::string const 
&)*arg1,(std::string const &)*arg2,arg3);
       ^

---- gr-myoot/swig/myoot_swig.i ----

/* -*- c++ -*- */

#define MYOOT_API

%include "gnuradio.i"                                    // the common stuff
//load generated python docstrings
%include "myoot_swig_doc.i"
%{
#include "myoot/packet_formatter_custom.h"
%}
%include "myoot/packet_formatter_custom.h"
GR_SWIG_BLOCK_MAGIC2(myoot, packet_formatter_custom);

// Properly package up non-block objects
%include "packet_formatter_custom.i"


---- gr-myoot/swig/packet_formatter_custom.i ----

%template(packet_formatter_custom_sptr) 
boost::shared_ptr<gr::myoot::packet_formatter_custom>;
%pythoncode %{
packet_formatter_custom_sptr.__repr__ = lambda self: "<packet_formatter_custom>"
packet_formatter_custom = packet_formatter_custom .make;
%}

------------------------------------------

To get it to build, I added the following typedef to 
gr-myoot/include/myoot/packet_formatter_custom.h :
                ...
                public:
                                typedef 
boost:shared_ptr<packet_formatter_custom> sptr;
                ...

Now importing SWIG-generated stuff in Python fails:

>>> import myoot
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/home/me/target/lib/python2.7/dist-packages/myoot/__init__.py", line 
29, in <module>
    from myoot_swig import *
  File "/home/me/target/lib/python2.7/dist-packages/myoot/myoot_swig.py", line 
265, in <module>
    packet_formatter_custom = packet_formatter_custom .make;
AttributeError: 'function' object has no attribute 'make'

This is probably due to a conflict between the definition of sptr: one is being 
autogenerated by SWIG based on the typedef in 
include/myoot/packet_formatter_custom.h; the other is the template in 
swig/packet_formatter_custom.i.

Now if I build without the special sauce in swig/packet_formatter_custom.i, I 
can get "import myoot" in Python to work. However, there's still some strange 
behavior if I don't actually assign to a variable.

>>> import myoot
>>> from gnuradio import digital
>>> formatter1 = myoot.packet_formatter_custom("1", "1", 1)
>>> formatter2 = digital.packet_formatter_counter("1", 1)
>>> myoot.packet_formatter_custom("1", "1", 1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/home/me/target/lib/python2.7/dist-packages/myoot/myoot_swig.py", line 
261, in <lambda>
    packet_formatter_custom_sptr.__repr__ = lambda self: "<gr_block %s (%d)>" % 
(self.name(), self.unique_id())
AttributeError: 'packet_formatter_custom_sptr' object has no attribute 'name'
>>> digital.packet_formatter_counter("1", 1)
<packet_formatter_default>

Note that the sptr in gr-digital is of type packet_formatter_default, meaning 
it inherited the definition of the typedef from packet_formatter_default. In my 
case it didn't work because I used GR_SWIG_BLOCK_MAGIC2 on a non block, so the 
__repr__ definition (above) breaks.

What should I do? I can rely on GR_SWIG_BLOCK_MAGIC2, which is not really the 
right way to generate SWIG templates for a non block, or I can try to get 
things to work without redefining the sptr typedef in the child class. In that 
case, I need to include the proper header file (namely, 
packet_formatter_default.h) from gr-digital. Should I do that explicitly in 
myoot_swig.i, or is there a better way to add gr-digital to include dirs in 
swig/CMakeLists.txt?
_______________________________________________
Discuss-gnuradio mailing list
Discuss-gnuradio@gnu.org
https://lists.gnu.org/mailman/listinfo/discuss-gnuradio

Reply via email to