[Pyrex] numeric C pointer argument and python integer argument

Lenard Lindstrom len-l at telus.net
Thu Apr 10 21:51:54 CEST 2008


Are you writing a script to parse a C header and generate a Python 
wrapper in Pyrex? This will grow quickly into a large project unless the 
C headers are relatively simple.

There is no way to automatically determine if an (int *) argument points 
to a single integer or a vector. The C language makes no distinction. A 
pointer to a single integer is equivalent to an integer vector of length 
1. So the simplest solution is to assume it is a vector in every case. 
Of course C does not track array lengths. So neither will the Python 
wrapper. Call a method with an array of the wrong length and the program 
may crash.

How to represent an integer vector in Python depends in part on how it 
is to be allocated. Do the wrapped functions simply accept vectors that 
are allocated by the user?

int a, v[10], *p;  /* Automatic variables */
int *p = malloc(sizeof(int) * 30);  /* The heap */

If so then existing Python types can be used. Both array module and 
ctypes package arrays support the buffer protocol (section "6.6 Buffer 
Protocol" of the "Python/C API Reference Manual") which allows direct 
access of their contents from C. These arrays are mutable, so can be 
passed by reference just like in C.


Lenard


Daniele Pianu wrote:
> Mmmm, ok...but your solution is not praticable in the script that I'm 
> writing. I'm writing a script that generate python classes from C 
> structures(and relative methods that use those structures), so, when I 
> parse a method in a C header, I need to know if a pointer argument 
> points to a single integer or to a vector to wrap the method. I think 
> it's impossible to do this in the script. Am I wrong?
>
> 2008/4/10, Lenard Lindstrom <len-l at telus.net <mailto:len-l at telus.net>>:
>
>     One way is to create a new Python container type that wraps the C int
>     array. The other is to copy values between the int array and some
>     existing Python container type, such as a list.
>
>     Lenard
>
>
>
>     Daniele Pianu wrote:
>     > And if the c pointer points to a vector of numeric values and not a
>     > single int? Is there any way to manage that in pyrex?
>     >
>
>     > 2008/4/9, Lenard Lindstrom <len-l at telus.net
>     <mailto:len-l at telus.net> <mailto:len-l at telus.net
>     <mailto:len-l at telus.net>>>:
>
>     >
>     >     Daniele Pianu wrote:
>     >     > I've a C function like this
>     >     >
>     >     > void Socket_getRemoteAddr(Socket *self, char *serverHostAddr,
>     >     int bufferSize, int *serverPortNo)
>     >     >
>     >     > and I want to wrap it on this python method
>     >     >
>     >     > def setRemoteAddr( self, serverHostAddr, serverPortNo )
>     >     >
>     >     > serverPortNo is a python integer, and it must reflect
>     modifications
>     >     > done on the relative C argument (serverPortNo). How can I
>     do it
>     >     in pyrex?
>     >     >
>     >
>     >     The usual way is to return the changed value as a value of the
>     >     function:
>     >
>     >     def setRemoteAddr(...., int serverPortNo):
>     >         ....
>     >         Socket_getRemoteAddr(...., &serverPortNo)
>     >         ....
>     >         return serverPortNo
>     >
>




More information about the Pyrex mailing list