[Pyrex] Passing c pointers to a class method

Phillip J. Eby pje at telecommunity.com
Tue May 4 21:08:20 CEST 2004


At 02:54 PM 5/4/04 -0400, John (J5) Palmieri wrote:
>On Tue, 2004-05-04 at 14:43, Phillip J. Eby wrote:
> > At 02:38 PM 5/4/04 -0400, John (J5) Palmieri wrote:
> > >Hello all,
> > >
> > >How come something like this works:
> > >
> > >cdef test (void *ptr):
> > >    pass
> > >
> > >test (somepointer)
> > >
> > >Yet this spits out "Cannot convert 'void (*)' to Python object":
> > >
> > >cdef class testclass:
> > >     cdef test (self, void *ptr):
> > >         pass
> > >
> > >mytest=testclass()
> > >mytest.test(somepointer)
> >
> >
> > 'test' isn't a classmethod, that's why.
> >
> > Is there even such a thing as a cdef classmethod?
>
>Ok, so is there a way to pass a C pointer into a method?

Oops.  I misread your question as having to do with classmethods, not 
instance methods.  The correct answer is:

cdef class testclass:
      cdef test (self, void *ptr):
          pass

cdef testclass mytest
mytest = testclass()
mytest.test(NULL)

The actual problem with your code was that without 'cdef testclass mytest', 
Pyrex has no way to know that 'mytest' will always hold an instance of 
'testclass', and so it cannot invoke the C method.

Note, by the way, that this will only work from Pyrex code, not Python code.





More information about the Pyrex mailing list