# Trouble with SWIG for packet\_formatter\_default child class

**URL:** <https://www.ruby-forum.com/t/trouble-with-swig-for-packet-formatter-default-child-class/234316>\
**Category:** GNU Radio\
**Created:** [February 23, 2015, 9:11pm UTC](https://www.ruby-forum.com/t/trouble-with-swig-for-packet-formatter-default-child-class/234316 "2015-02-23T21:11:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [February 23, 2015, 9:11pm UTC](https://www.ruby-forum.com/t/trouble-with-swig-for-packet-formatter-default-child-class/234316/1 "2015-02-23T21:11:48Z")

</div>

Hi all -

I’m working on Tom’s packet\_handling branch  
([GitHub - trondeau/gnuradio: GNU Radio](http://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\_ptrgr::myoot::packet\_formatter\_custom;  
%pythoncode %{  
packet\_formatter\_custom\_sptr. **repr** = lambda self:  
“\<packet\_formatter\_custom\>”  
packet\_formatter\_custom = packet\_formatter\_custom .make;  
%}

---

<div class="post-metadata">

**Author:** ![NowlanS\_Sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f05b48/32.png) [@NowlanS\_Sean](https://www.ruby-forum.com/u/NowlanS_Sean)\
**Post date:** [February 23, 2015, 10:12pm UTC](https://www.ruby-forum.com/t/trouble-with-swig-for-packet-formatter-default-child-class/234316/2 "2015-02-23T22:12:24Z")

</div>

From: [discuss-gnuradio-bounces+sean.nowlan=removed\_email\_address@domain.invalid](mailto:discuss-gnuradio-bounces+sean.nowlan=removed_email_address@domain.invalid)  
[[mailto: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: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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](http://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\_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?

---

<div class="post-metadata">

**Author:** ![NowlanS\_Sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f05b48/32.png) [@NowlanS\_Sean](https://www.ruby-forum.com/u/NowlanS_Sean)\
**Post date:** [February 24, 2015, 9:06am UTC](https://www.ruby-forum.com/t/trouble-with-swig-for-packet-formatter-default-child-class/234316/3 "2015-02-24T09:06:18Z")

</div>

Hi,

I also created a custom header that derives from  
packet\_formatter\_default and this swig file works for me  
[https://github.com/bastibl/gr-ieee802-11/blob/master/swig/ieee802\_11\_swig.i](https://github.com/bastibl/gr-ieee802-11/blob/master/swig/ieee802_11_swig.i)

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  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [Discuss-gnuradio Info Page](https://lists.gnu.org/mailman/listinfo/discuss-gnuradio)

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

> **[Home](https://www.bastibl.net/)**
>
> I'm Bastian Bloessl, a Research Group Leader at the Secure Mobile Networking Lab, TU Darmstadt. Checkout my blog, projects, publications, and resume; or connect on Github, Twitter, YouTube, or Facebook.

---

<div class="post-metadata">

**Author:** ![NowlanS\_Sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f05b48/32.png) [@NowlanS\_Sean](https://www.ruby-forum.com/u/NowlanS_Sean)\
**Post date:** [February 24, 2015, 7:29pm UTC](https://www.ruby-forum.com/t/trouble-with-swig-for-packet-formatter-default-child-class/234316/4 "2015-02-24T19:29:52Z")

</div>

* * *

From: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) on behalf of Tom  
Rondeau [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Tuesday, February 24, 2015 10:32 AM  
To: Bastian B.  
Cc: Nowlan, Sean; [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: [Discuss-gnuradio] Trouble with SWIG for  
packet\_formatter\_default child class

On Tue, Feb 24, 2015 at 3:00 AM, Bastian B.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
Hi,

I also created a custom header that derives from  
packet\_formatter\_default and this swig file works for me  
[https://github.com/bastibl/gr-ieee802-11/blob/master/swig/ieee802\_11\_swig.i](https://github.com/bastibl/gr-ieee802-11/blob/master/swig/ieee802_11_swig.i)

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](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

---

<div class="post-metadata">

**Author:** ![NowlanS\_Sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f05b48/32.png) [@NowlanS\_Sean](https://www.ruby-forum.com/u/NowlanS_Sean)\
**Post date:** [February 24, 2015, 10:11pm UTC](https://www.ruby-forum.com/t/trouble-with-swig-for-packet-formatter-default-child-class/234316/5 "2015-02-24T22:11:27Z")

</div>

​​

* * *

From: [discuss-gnuradio-bounces+sean.nowlan=removed\_email\_address@domain.invalid](mailto:discuss-gnuradio-bounces+sean.nowlan=removed_email_address@domain.invalid)  
[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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Tuesday, February 24, 2015 1:27 PM  
To: Tom R.; Bastian B.  
Cc: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: [Discuss-gnuradio] Trouble with SWIG for  
packet\_formatter\_default child class

* * *

From: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) on behalf of Tom  
Rondeau [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Tuesday, February 24, 2015 10:32 AM  
To: Bastian B.  
Cc: Nowlan, Sean; [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: [Discuss-gnuradio] Trouble with SWIG for  
packet\_formatter\_default child class

On Tue, Feb 24, 2015 at 3:00 AM, Bastian B.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:  
Hi,

I also created a custom header that derives from  
packet\_formatter\_default and this swig file works for me  
[https://github.com/bastibl/gr-ieee802-11/blob/master/swig/ieee802\_11\_swig.i](https://github.com/bastibl/gr-ieee802-11/blob/master/swig/ieee802_11_swig.i)

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](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

---

<div class="post-metadata">

**Author:** ![NowlanS\_Sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f05b48/32.png) [@NowlanS\_Sean](https://www.ruby-forum.com/u/NowlanS_Sean)\
**Post date:** [February 24, 2015, 4:35pm UTC](https://www.ruby-forum.com/t/trouble-with-swig-for-packet-formatter-default-child-class/234316/6 "2015-02-24T16:35:04Z")

</div>

On Tue, Feb 24, 2015 at 3:00 AM, Bastian B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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](https://github.com/trondeau/gnuradio/blob/packet_handling/gr-digital/swig/packet_header.i)

Tom
