From: Kris T. <kri...@gm...> - 2012-09-19 02:09:59
|
Oh well. You can ignore my previous email. I forgot to %include <stl.i>, which explains my failure with std::string. Now I'm left with problems with builtin, see edited question below. Apologies. > From: Kris Thielemans [mailto:kri...@gm...] > Sent: 19 September 2012 02:20 > > I'm trying to debug some stuff with __repr__ and __str__ (see previous > emails if you're interested but it's irrelevant here) so went back to basics > and made short example. Sadly, this short example works even less than my > complicated one, so I must be doing something very stupid. Maybe someone > can > help? > > My simple example is as follows: define simple class, extend it with > __repr__. Here is the code > > %module testKT > %{ > #include <string> > %} > %include <stl.i> > %inline { > class A > { > public: > int _a; > A(int a): _a(a) {} > }; > } > > %extend A { > std::string __repr__() > { return "in repr"; } > #if defined(SWIGPYTHON_BUILTIN) > %feature("python:slot", "tp_repr", functype="reprfunc") __repr__; > #endif > } > } > Now, without builtin everything is at it should. But if I use builtin, then A.__repr__ works, but repr(A) doesn't: >>> from testKT import * >>> a=A(1) >>> a.__repr__() 'in repr' >>> repr(a) "<Swig Object of type 'A *' at 0x7f12e470cab0>" (i.e. repr(a) falls back to the default swig version). Checking the wrapped code I see that my function is wrapped and is added as a method (SwigPyBuiltin__A_methods), but isn't inserted into the type definition table (SwigPyBuiltin__A_type) as the tp_repr slot. > > The weird thing is that in my complicated code (too long to include here), > the above type of lines do work, both with and without builtin (and using a > std::string return signature). > > What am I missing here? > > I'm using swig 2.0.7 and python 2.7.2. on ubuntu. > > thanks for any help > All this still applies :-; Kris |