[Pyrex] Function wrapping problems
Bob Ippolito
bob at redivi.com
Tue Oct 14 23:32:23 CEST 2003
On Tuesday, Oct 14, 2003, at 17:19 America/New_York, Simon Frost wrote:
> I'm trying to wrap some C code (emitted from Eiffel using the
> SmartEiffel compiler, not that it matters) using Pyrex, so I can call
> the C function from Python. The C code that calls the function
> directly is as follows:
>
> #include <stdio.h>
> #include "eiffel.h"
>
> int main(int argc, char* argv[ ])
> {
> initialize_eiffel_runtime(argc,argv);
> run_epidemic(eiffel_root_object);
> return 0;
> }
>
> Here are the declarations that I (think I) need for Pyrex:
>
> cdef extern from "epidemic.h":
> void initialize_eiffel_runtime(int argc,char* argv[])
>
> cdef extern from "eiffel.h":
> void run_epidemic(void* C)
> void* eiffel_root_object
>
> This question probably reflects my lack of knowledge (of both C and
> Pyrex), but how can I reproduce my C main function within a Pyrex
> function?
I believe that you want something like this (in addition to what you
already have):
cdef extern from "Python.h":
char *PyString_FromString(object s) except NULL
void* PyMem_Realloc(void *p, int n)
void* PyMem_Malloc(int n)
void PyMem_Free(void *p)
def main(*args):
cdef char **argv
cdef int argc
cdef int i
argc = len(args)
argv = PyMem_Malloc(4 * argc)
if argv == NULL:
raise MemoryError, "Out of memory?"
try:
i = 0
for arg in args:
argv[i] = PyString_FromString(arg)
i = i + 1
initialize_eiffel_runtime(argc, argv)
run_epidemic(eiffel_root_object)
finally:
PyMem_Free(argv)
From python, you would do:
import yourmodule
yourmodule.main('first argument', 'second argument')
-bob
More information about the Pyrex
mailing list