[Pyrex] How to specify __contains__ method?

Andreas Kostyrka andreas at mtg.co.at
Sun Apr 11 21:40:01 CEST 2004


Am So, den 11.04.2004 schrieb Gary Bishop um 20:43:
Known bug ;)
[I'm just not sure if I mailed it away or if I forgot to mail the bugfix
in ;) ]

Ok, in Pyrex/Compiler/TypeSlots.py look for a line like that:
        MethodSlot(objargproc, "sq_contains", "__contains__"),
change it then to:
        MethodSlot(cmpfunc, "sq_contains", "__contains__"),

Sorry, for this "manual, from memory" patch, but I've got the fixed
stuff in the office, so I cannot check before Tuesday.

Andreas

> When I compile the following simple example:
> 
> cdef class foo:
>   def __contains__(self, key):
>     return 1
> 
> I get the error message:
> 
> d:\msl\gb\python\my\Matrix\bug2.pyx:3:11: Return with value in void function
> 
> If I change "def" to "cdef int", it gets past Pyrex but the C compiler 
> complains:
> 
> bug2.c(147) : warning C4028: formal parameter 1 different from declaration
> 
> But it appears to work.
> 
> Now in a more complicated case, I can't get it to work properly.
> 
> cdef class foo:
>   cdef int val
>   def __init__(self):
>     val = 0
> 
>   cdef int __contains__(self, key):
>     print 'foo contains', self.val
>     return 1
> 
> cdef class fee(foo):
>   cdef int __contains__(self, key):
>     print 'fee contains', self.val
>     return 0
> 
> cdef class faa(fee):
>   def bof(self):
>     print 'bof', self.val
>   cdef int __contains__(self, key):
>     print 'faa contains', self.val
>     return 0
> 
> If I leave off that last cdef int __contains__, I get C compiler errors like:
> 
> bug2.c(275) : warning C4028: formal parameter 1 different from declaration
> bug2.c(421) : warning C4028: formal parameter 1 different from declaration
> bug2.c(568) : error C2065: 'None' : undeclared identifier
> bug2.c(568) : error C2099: initializer is not a constant
> 
> With it included, it will compile but when I try to do
> import bug2
> faa = bug2.faa()
> 1 in faa
> 
> I get:
> faa contains
> Exception exceptions.AttributeError: "'bug2.faa' object has no 
> attribute 'val'" in 'bug2.faa.__contains__' ignored
> 
> I seem to have lost my connection (via inheritance) to the original foo class.
> 
> I poked around in the Pyrex0.9 source a little but couldn't find my way 
> to where this might happen.
> 
> Any ideas for a work around?
> 
> Thanks for Pyrex, it is GREAT STUFF.
> gb
> 
> _______________________________________________
> Pyrex mailing list
> Pyrex at lists.copyleft.no
> http://lists.copyleft.no/mailman/listinfo/pyrex
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://lists.copyleft.no/pipermail/pyrex/attachments/20040411/83e028e0/attachment.bin


More information about the Pyrex mailing list