[Pyrex] cdef methods still unclear

Lenard Lindstrom len-l at telus.net
Tue Mar 7 21:35:10 CET 2006


On 7 Mar 2006 at 11:43, Phil Frost wrote:

> On Tue, Mar 07, 2006 at 05:00:02PM +0100, Helmut Jarausch wrote:
> > Sorry, bothering you again,
> > but the documentation is short!
> > 
> > I'd like to have a cdef function within
> > an extension type which is only called
> > by other Pyrex functions.
> > But I can't even define it.
> > 
> > What's wrong with
> > 
> > cdef class Document:
> >   cdef char* buffer
> >   cdef int   buffer_len
> > 
> >   cdef char* iso1_to_utf(char* in):
> > # ------------------ Expected ')'  <<<<< ERROR
> >     return in
> > 
> > 
> > Furthermore, would it be possible for this
> > iso1_to_utf to access self.buffer ?
> > 
Any method of an extension type can assess its C fields through the 
'self' argument, just as if they were Python attributes.

> > Many thanks for a hint,
> > 
> > Helmut Jarausch
> 
> 
> Try something like this:
> 
> 
> cdef class Document:
>   cdef char* buffer
>   cdef int   buffer_len
> 
>   # don't use cdef to define methods within extension types. Also, don't
>   # forget "self". You might also have to remove the "char *" return
>   # type; I don't know if pyrex will convert a char * to a python string
>   # automatically.
> 
A C method  is exactly what is needed if it is to remain private to 
the Pyrex module. And the Pyrex compiler is still under development 
so error messages are not always appropriate. The missing 'self' is 
likely the problem in this case.

>   def char* iso1_to_utf(self, char* in):
>     # you can access self.buffer here if you like
>     return in
    cdef char* iso1_to_utf(self, char* in):

Lenard Lindstrom
<len-l at telus.net>




More information about the Pyrex mailing list