[Pyrex] Pyrex questions

Tom Kobialka u3356514 at anu.edu.au
Wed Jun 2 04:01:02 CEST 2004


Hi Greg,

  Thanks for your reply.

Greg Ewing wrote:
> Tom Kobialka <u3356514 at anu.edu.au>:
>>I am encapsulating a small C program that makes use of a very specific
>>gravitational wave frame library. I want to call the routines in the
>>library from my C program. Do I have to encapsulate the library in
>>Pyrex in order to call it from my encapsulated C program?
> 
> 
> I'm a bit confused about what you're trying to do. If the
> gravitational wave library is only called through your C code, and
> never directly from Python, you should only have to wrap your C code,
> i.e.
> 
>   Python -> Pyrex wrapper for your C code -> your C code -> Library
> 

  Ok, this is a relif! This purpose of this whole exercise is to get my 
C program, which makes us of a Grav Wave library, to interface with a 
thrid party python program. I was a bit confused as to whether I would 
have to wrap the routines of the Grav Wave library in addition to my C 
program. Obviously I don't, so this is music to my ears :)

>>it doesn't seem to mention C routines and procedures such as atoi,
>>malloc, opendir (dirent.h), casting or nesting i.e monkey = (char *)
>>malloc(strlen(banana) + strlen(paddlepop->chocolate))
> 
> 
> Any C routine can be called from Pyrex if you declare it appropriately
> in a 'cdef extern from' block; see the section on "Interfacing with
> External C Code". Casting is possible, bit with a slightly different
> syntax; see "Differences between C and Pyrex expression syntax".
> 
> Your example would be translated into Pyrex as follows:
> 
>   cdef extern from "stdlib.h":
>     void *malloc(int len)
> 
>   cdef extern from "string.h":
>     int strlen(char *)
> 
>   # suitable definitions here for monkey, banana,
>   # paddleop and chocolate
> 
>   monkey = <char *> malloc(strlen(banana) +
>     strlen(paddlepop.chocolate))

  Great, so i'm on the right track :)

> 
>>Does anyone have a small sized (300 + line) C program and
>>corresponding pyrex code
> 
> 
> Have you looked at Michael's Quick Guide to Pyrex? It contains
> a couple of examples of wrapping real-world C libraries.

  Yes, since posting this message i've refined my google searches and 
have come across some great examples in  various CVS repositories.

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

Cheers

  Tom





More information about the Pyrex mailing list