[Pyrex] char * python type with extensions

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Jul 4 06:21:03 CEST 2005


Nathaniel Haggard wrote:

> cdef class const_data:
> 	cdef char *tc
> 	
> 	cdef void setme(self, tree_cell *t):
> 		self.tc = t

> char * tmp
> tmp = c_routine_that_returns_character_array()
> a = const_data()
> a.setme(tmp)

> But it's trying to coerce the char * to a pyhton string instead

The reason for this is that, since you haven't given
a type to 'a', Pyrex is treating it as a generic Python
object and trying to make a Python call to a Python
method called 'setme'.

You need to declare the type of 'a':

   cdef const_data a

and then Pyrex will know that you're calling the
C method 'setme' and won't do the conversion.

By the way, there's no need for the setme method
if you do this; you can just do

   a.tc = tmp

Also, you'd better be completely sure that the
memory pointed to is going to be around for at least
as long as the Python object will be.

-- 
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg.ewing at canterbury.ac.nz	   +--------------------------------------+



More information about the Pyrex mailing list