[Pyrex] cdef methods still unclear

Phil Frost indigo at bitglue.com
Tue Mar 7 17:43:49 CET 2006


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 ?
> 
> 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.

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


I haven't tried to compile this so I can't say it works, but I hope it
will at least get you going in the right direction.

Also, are you are aware python already has methods to convert between
character encodings? See the 'str.decode', 'str.encode', and
'unicode.encode'.



More information about the Pyrex mailing list