From: William S F. <ws...@fu...> - 2008-07-07 20:45:56
|
Nitro wrote: > Am 05.07.2008, 22:36 Uhr, schrieb William S Fulton > <ws...@fu...>: > > > I found a problem with the Python typemaps when bool is overloaded with > > some other numeric types. It was impossible to call the bool overload. I > > have an example class below and the fix supplying a typecheck typemap. > > The original typecheck typemap relies on SWIG_AsVal_bool to indicate > > whether the typemap can be converted to bool... > > I have seen this in my wrappers before, but never bothered to > investigate the problem more deeply. Does this happen with and without > the -castmode option (the casting rank mechanism) or just with one of > them? > I don't really know this option to be honest. It looks like it uses implicit conversions, so am not sure how it differs from %implicitconv. Anyway, I think it is doing the right thing. Here is a test case for the example I showed earlier... from example import * class Ai(): def __init__(self,x): self.x = x def __int__(self): return self.x x = XMLFile("hi", False) x = XMLFile("hi", True) x = XMLFile("hi", 0) x = XMLFile("hi", 1) x = XMLFile("hi", 2) x = XMLFile("hi", 0.0) x = XMLFile("hi", 1.0) x = XMLFile("hi", 2.0) x = XMLFile("hi", 2.2) x = XMLFile("hi", Ai(4)) With the new typecheck typemap and -castmode, the output is: XmlFile(string=hi, bool=0) XmlFile(string=hi, bool=1) XmlFile(string=hi, int=0) XmlFile(string=hi, int=1) XmlFile(string=hi, int=2) XmlFile(string=hi, double=0) XmlFile(string=hi, double=1) XmlFile(string=hi, double=2) XmlFile(string=hi, double=2.2) XmlFile(string=hi, int=4) Which seems sensible to me. There is a change in behaviour to previously though without -castmode. When calling just: x = XMLFile("hi", Ai(4)) we got this output: XmlFile(string=hi, bool=1) which seems plain wrong to me. Now we get: NotImplementedError: Wrong number of arguments for overloaded function 'new_XMLFile'. Possible C/C++ prototypes are: XMLFile(std::string const &,bool) XMLFile(std::string const &) XMLFile(std::string const &,double) XMLFile(std::string const &,int) Which seems better like the right thing to me. Is there an equivalent to 'def __int__(self)' for converting to booleans? That might be worth testing. William |