[PyKDE] convertSubClass() -- I introduced a bug :-(

Dave Cuthbert dacut at neolinear.com
Wed Apr 7 22:31:01 BST 2004


A big mea culpa here...

Phil, earlier we thought that the arguments to PyType_IsSubType() were 
switched for subclass converters; I emailed you directly, and you went 
ahead and made the change in the SIP tree (post 4.0rc3).

I think we made another mistake in understanding what SIP was doing.

In pseudocode form, the original code in rc3 looks like:

convertSubClass(obj_type, obj) is
   ## obj_type is possibly a superclass of obj -- we want to find out
   ## the real type of obj

   foreach module in loaded_modules do
     if module has subclass_converters then
       foreach converter in subclass_converters do
         ## attempt conversion
         if PyType_IsSubType(obj_type, converter.type) then
           converter.convert(obj)
           if conversion was successful then return


We thought that converter.convert() (scc->scc_converter() in the actual 
code) could only convert to converter.type 
(scc->scc_basetype->super.type) and would reject attempts to do 
otherwise.  By this reasoning, the arguments are reversed -- the check 
should be seeing if converter.type is a subtype of obj_type for a 
potential conversion.

But that's not the case!  What converter.convert() really does is 
convert obj to the _most derived type_ of converter.type -- the 
IsSubType() check is just making sure the converter is applicable.

Now, the real bug is if class A defines a subclass converter, no other 
class derived from A can also define a subclass converter (and expect it 
to be called consistently).  A's subclass converter has to be aware of 
the entire hierarchy.

Unfortunately, fixing this is very non-trivial (IMHO).  Fortunately, for 
the QCanvasItem problem we can work around it by editing the source.

Again, my apologies for my earlier misleading analysis.

Dave




More information about the PyQt mailing list