________________________________ From: discuss-gnuradio-bounces+sean.nowlan=gtri.gatech....@gnu.org <discuss-gnuradio-bounces+sean.nowlan=gtri.gatech....@gnu.org> on behalf of Nowlan, Sean <sean.now...@gtri.gatech.edu> Sent: Tuesday, February 24, 2015 1:27 PM To: Tom Rondeau; Bastian Bloessl Cc: discuss-gnuradio@gnu.org Subject: Re: [Discuss-gnuradio] Trouble with SWIG for packet_formatter_default child class
________________________________ From: trond...@trondeau.com <trond...@trondeau.com> on behalf of Tom Rondeau <t...@trondeau.com> Sent: Tuesday, February 24, 2015 10:32 AM To: Bastian Bloessl Cc: Nowlan, Sean; discuss-gnuradio@gnu.org Subject: Re: [Discuss-gnuradio] Trouble with SWIG for packet_formatter_default child class On Tue, Feb 24, 2015 at 3:00 AM, Bastian Bloessl <bloe...@ccs-labs.org<mailto:bloe...@ccs-labs.org>> 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 I forgot why it did it like that, but maybe it helps. Best, Bastian > On 23 Feb 2015, at 22:10, Nowlan, Sean > <sean.now...@gtri.gatech.edu<mailto:sean.now...@gtri.gatech.edu>> wrote: > > From: > discuss-gnuradio-bounces+sean.nowlan=gtri.gatech....@gnu.org<mailto:gtri.gatech....@gnu.org> > > [mailto:discuss-gnuradio-bounces+sean.nowlan<mailto:discuss-gnuradio-bounces%2Bsean.nowlan>=gtri.gatech....@gnu.org<mailto:gtri.gatech....@gnu.org>] > On Behalf Of Nowlan, Sean > Sent: Monday, February 23, 2015 3:10 PM > To: discuss-gnuradio@gnu.org<mailto: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<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_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<http://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? 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
_______________________________________________ Discuss-gnuradio mailing list Discuss-gnuradio@gnu.org https://lists.gnu.org/mailman/listinfo/discuss-gnuradio