calling Pyrex results from C

Kyler Laird Kyler at news.Lairds.org
Wed Jan 21 10:12:06 EST 2004


The help I've gotten in this thread has enabled me to complete my
assignment...well, at least I can generate C code that spits out
the answer.

I'd like to factor out more of the Python so that my handwritten
C code can do more of the high-level operations.  I'm having some
difficulties passing data between my C and the Pyrex code.

One of the first things I thought I'd build is a helper function
to return a string representation of an object.  I thought that
this would be fairly straightforward.  Unfortunately it segfaults
when called with some Python objects.

Pyrex:
	cdef public image_file_open(char* image_filename):
		return(Image.open(image_filename))

	cdef public image_size(image_PIL):
		return(Image.size)

	cdef public char* string(x):
		s = str(x)
		return(s)

my C:
	void *im, *im_size;
        im = image_file_open(input_filename);
        im_size = image_size(im);
	printf("im=%s\n", string(im));
        printf("im_size=%s\n", string(im_size));

The first call to string() succeeds but the second one fails.  I
suspect that I've expected too much of Pyrex again.  Do I need to
allocate memory, manipulate reference counts or something like
that?

It's difficult for me to get back to thinking in terms of
passing around data in C ways.  I almost forgot what a boon
tuples have been.  (Perhaps I need to code in assembly for
awhile to get back in that frame of mind.)  Someday I might try 
to pass structures between C and Pyrex.  I'm slowly getting
comfortable with Pyrex but I can see how my code can get *much*
cleaner once I understand more of its capabilities.

Thank you.

--kyler



More information about the Python-list mailing list