[Pyrex] How to access C variable of other class?

Matthias Baas baas at ira.uka.de
Wed Oct 6 08:55:28 CEST 2004


Gustavo Barbieri wrote:
> cdef class Video:
>    def clone( self, src ):
>       visual_video_clone( self.video, src.video )
> 
> but I get the following error:
>   "Cannot convert Python object to 'VisVideo (*)'"

The src argument of the clone() method is an arbitrary Python object, so 
if you call src.video the result is also an arbitrary Python object 
(this is just an ordinary attribute lookup). And visual_video_clone() 
expects a VisVideo pointer and not a pointer to a Python object.

Well, *you* know that you're only passing Video objects but how could 
*Pyrex* know in this case? Actually, the fix is easy, just "declare" the 
src parameter as "Video" like this:

     def clone( self, Video src ):
        visual_video_clone( self.video, src.video )

or even better:

     def clone( self, Video src not None):
        visual_video_clone( self.video, src.video )

Now Pyrex checks the argument and will generate an exception if the 
wrong type is passed (in the first case, passing None will be allowed 
and you would have to check that yourself). And now Pyrex knows that src 
is actually a Video object and will access the cdef video attribute.

- Matthias -




More information about the Pyrex mailing list