Trouble with SWIG for packet_formatter_default child class

Hi all -

I’m working on Tom’s packet_handling branch
(GitHub - trondeau/gnuradio: GNU Radio) 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_ptrgr::myoot::packet_formatter_custom;
%pythoncode %{
packet_formatter_custom_sptr.repr = lambda self:
“<packet_formatter_custom>”
packet_formatter_custom = packet_formatter_custom .make;
%}

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

Hi all -

I’m working on Tom’s packet_handling branch
(GitHub - trondeau/gnuradio: GNU Radio) 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_ptrgr::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 “”, line 1, in
File “/home/me/target/lib/python2.7/dist-packages/myoot/init.py”,
line 29, in
from myoot_swig import *
File
“/home/me/target/lib/python2.7/dist-packages/myoot/myoot_swig.py”, line
265, in
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 “”, line 1, in
File
“/home/me/target/lib/python2.7/dist-packages/myoot/myoot_swig.py”, line
261, in
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?

Hi,

I also created a custom header that derives from
packet_formatter_default and this swig file works for me

I forgot why it did it like that, but maybe it helps.

Best,
Bastian

result = gr::myoot::packet_formatter_custom::make((std::string const 

&)*arg1,(std::string const &)*arg2,arg3);

%include “myoot_swig_doc.i”
---- gr-myoot/swig/packet_formatter_custom.i ----

from myoot_swig import *

formatter1 = myoot.packet_formatter_custom(“1”, “1”, 1)
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
[email protected]
Discuss-gnuradio Info Page


Dipl.-Inform. Bastian B.
Distributed Embedded Systems Group
University of Paderborn, Germany


From: [email protected] [email protected] on behalf of Tom
Rondeau [email protected]
Sent: Tuesday, February 24, 2015 10:32 AM
To: Bastian B.
Cc: Nowlan, Sean; [email protected]
Subject: Re: [Discuss-gnuradio] Trouble with SWIG for
packet_formatter_default child class

On Tue, Feb 24, 2015 at 3:00 AM, Bastian B.
<[email protected]mailto:[email protected]> wrote:
Hi,

I also created a custom header that derives from
packet_formatter_default and this swig file works for me

I forgot why it did it like that, but maybe it helps.

Best,
Bastian

result = gr::myoot::packet_formatter_custom::make((std::string const 

&)*arg1,(std::string const &)*arg2,arg3);

%include “myoot_swig_doc.i”
---- gr-myoot/swig/packet_formatter_custom.i ----

from myoot_swig import *

formatter1 = myoot.packet_formatter_custom(“1”, “1”, 1)
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 didnt 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?

Sean,

Did you have a look at the packet_header.i file? It’s basically what
Bastian figured out (maybe where he got it from). We have to do a bit
more swig manipulation to get the make functions working properly.

https://github.com/trondeau/gnuradio/blob/packet_handling/gr-digital/swig/packet_header.i

Tom

Bastian, Tom,

Yes, I had basically copied my template from
gr-digital/swig/packet_header.i. However, SWIG couldn’t figure out that
my subclass’ parent is gr::digital::packet_formatter_default, so I
didn’t have access to its methods or members. I’ll try again, taking a
lead from gr-ieee802-11.

Thanks,
Sean

​​


From: discuss-gnuradio-bounces+sean.nowlan=removed_email_address@domain.invalid
discuss-gnuradio-bounces+sean.nowlan=removed_email_address@domain.invalid on behalf
of Nowlan, Sean [email protected]
Sent: Tuesday, February 24, 2015 1:27 PM
To: Tom R.; Bastian B.
Cc: [email protected]
Subject: Re: [Discuss-gnuradio] Trouble with SWIG for
packet_formatter_default child class


From: [email protected] [email protected] on behalf of Tom
Rondeau [email protected]
Sent: Tuesday, February 24, 2015 10:32 AM
To: Bastian B.
Cc: Nowlan, Sean; [email protected]
Subject: Re: [Discuss-gnuradio] Trouble with SWIG for
packet_formatter_default child class

On Tue, Feb 24, 2015 at 3:00 AM, Bastian B.
<[email protected]mailto:[email protected]> wrote:
Hi,

I also created a custom header that derives from
packet_formatter_default and this swig file works for me

I forgot why it did it like that, but maybe it helps.

Best,
Bastian

result = gr::myoot::packet_formatter_custom::make((std::string const 

&)*arg1,(std::string const &)*arg2,arg3);

%include “myoot_swig_doc.i”
---- gr-myoot/swig/packet_formatter_custom.i ----

from myoot_swig import *

formatter1 = myoot.packet_formatter_custom(“1”, “1”, 1)
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?

Sean,

Did you have a look at the packet_header.i file? It’s basically what
Bastian figured out (maybe where he got it from). We have to do a bit
more swig manipulation to get the make functions working properly.

https://github.com/trondeau/gnuradio/blob/packet_handling/gr-digital/swig/packet_header.i

Tom

Bastian, Tom,

Yes, I had basically copied my template from
gr-digital/swig/packet_header.i. However, SWIG couldn’t figure out that
my subclass’ parent is gr::digital::packet_formatter_default, so I
didn’t have access to its methods or members. I’ll try again, taking a
lead from gr-ieee802-11.

Thanks,
Sean

Got it working. I may not have done #include DIGITAL_API or properly
included the header,
gr-digital/include/gnuradio/digital/packet_formatter_default.h. Thanks
for your help!

Sean

On Tue, Feb 24, 2015 at 3:00 AM, Bastian B. [email protected]
wrote:

---- first few lines of make errors ----
sptr result;

GR_SWIG_BLOCK_MAGIC2(myoot, packet_formatter_custom);
packet_formatter_custom_sptr.repr = lambda self:
typedef
from myoot_swig import *
Now if I build without the special sauce in
File “”, line 1, in
meaning it inherited the definition of the typedef from
swig/CMakeLists.txt?

Sean,

Did you have a look at the packet_header.i file? It’s basically what
Bastian figured out (maybe where he got it from). We have to do a bit
more
swig manipulation to get the make functions working properly.

https://github.com/trondeau/gnuradio/blob/packet_handling/gr-digital/swig/packet_header.i

Tom