calling Pyrex results from C

Kyler Laird Kyler at
Wed Jan 21 18:16:54 CET 2004

On Wed, Jan 21, 2004 at 08:23:53AM -0800, Paul Prescod wrote:

> First, I'd suggest that "integer" is a perfectly good type in both C and 
> Pyrex so you shouldn't pass around Python objects representing integers.

That would be my inclination too if I just had an integer to pass.
Are you suggesting that I should pass tuples of integers as pointers
to (an array of) integers?

> Second, you aren't checking the return codes of your functions and C has 
> no exception handling, tracebacks, etc. image_size is probably returning 
> 0 because it is probably throwing an exception because you are asking 
> for the size attribute of the Image class rather than the image_PIL object.

I'm trying to keep my examples simple so that it's easy to pick out the

> Once I fix the Image/Image_PIL error your code runs okay on my computer. 

Grrr...yup, that does it.  Dumb mistake - the result of pulling code
out of various places and gluing it together without checks.

>  But you are walking on thin ice and may just be lucky. It is simply 
> not possible to work with strings generated at runtime in C without 
> worrying about memory allocation sometime. In this case I have the 
> strong suspicion that the string() function is either creating and 
> destroying a string object and then returning you a pointer to the dead 
> object's internal memory buffer (bad news!) or simply losing a reference 
> to the (still living) string object: still not a good thing. Eyeballing 
> the code I believe the former is the issue. You could check for sure by 
> adding some printf's to the generated code to look at __pyx_v_s->ob_refcnt.

I feared this was a problem.  I'll play with it.

> Why not let Python do the "print" rather than using "printf".

Printing is not always my final goal.

Thank you.


More information about the Python-list mailing list