From: Soeren S. <swi...@nn...> - 2008-04-16 09:12:43
|
On Wed, 2008-04-16 at 10:40 +0200, Amaury Forgeot d'Arc wrote: > Hello, > > Soeren Sonnenburg wrote: > > Dear all, > > > > whenever more than a single C++ constructor is defined, typemaps will > > not be applied for the constructor. > > > > I've finally managed to isolate the problem (minimal example attached). > > > > For example lets extend the class python example to have > > > > Square(double w) : width(w) { }; > > > > Square(double d[3]) { > > x=d[0]; > > y=d[1]; > > width=d[2]; > > } > > > > void set_defaults(double d[3]) { > > x=d[0]; > > y=d[1]; > > width=d[2]; > > } > > > > and add a typemap > > > > %typemap(in) (double vals[3]) (double loc[3]) > > { > > //do sth. > > } > > > > %apply (double vals[3]) {double d[3]} > > > > > > Now this will work: > > > > s=example.Square(10) > > s.set_defaults([1,2,3]) > > print " Square = (%f, %f) area=%f" % (s.x,s.y,s.area()) > > > > But not > > > > s=example.Square([1,2,3]) > > print " Square = (%f, %f) area=%f" % (s.x,s.y,s.area()) > > > > > > A fix is to > > > > a) remove Square(double w); > > b) use %ignore Square(double w); in the .i file > > > > So it seems the typemaps are applied before the constructor wrappers are > > unrolled. Note that this happens regardless of the interface (happens in > > python/octave/R...) > > > > A toy example to trigger this is attached, just untar in the > > Examples/python directory (it will create a typemap directory), then > > cd typemap > > make > > python runme.py > > > > Any ideas on how to fix this ? > > Soeren > > From the Swig doc:: > If you define new "in" typemaps *and* your program uses overloaded methods, > you should also define a collection of "typecheck" typemaps > > I tried this, and your test case seems to work:: > > %typemap(typecheck, precedence=SWIG_TYPECHECK_DOUBLE_ARRAY) (double vals[3]) > { > $1 = PySequence_Check($input) && (PySequence_Size($input) == 3); > } Yes indeed it works :-)) Looks like I have completely missed the typecheck section http://www.swig.org/Doc1.3/Typemaps.html#Typemaps_overloading - but it is great news - you made my day! > Note that in your sample, the content of the typemap is not even used, > since the check for "double" has a higher priority as > SWIG_TYPECHECK_DOUBLE_ARRAY. So I don't know if it is even correct ;-) Looking at the table in the above link with the precedence list (*), as I understand it, first POINTER types will be checked then at some point DOUBLE and much later DOUBLE_ARRAY, but still as the typechekc for double/int must fail, it falls through to DOUBLE_ARRAY which succeeds (and works - I added some test printf's!). Thanks so much! Soeren. (*) seems like this is upward compatible as I am not aware of any INT128 type... |