[Pyrex] Function wrapping problems

Simon Frost sdfrost at ucsd.edu
Wed Oct 15 01:18:46 CEST 2003


Dear Bob,

Thanks for the tip. However, the following code chokes on a type error.

epidemic.pyx:19:21: Cannot assign type 'void (*)' to 'char (*(*))'

Although I'm terribly clumsy with pointers, this seems to be a problem with 
argv, although if so, I can't see why I'm getting a void(*) type.

Best
Simon
-------------------------------------------------
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

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)


At 05:32 PM 10/14/2003 -0400, you wrote:
>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

Simon D.W. Frost, M.A., D.Phil.
Department of Pathology
University of California, San Diego
Antiviral Research Center
150 W. Washington St., Suite 100
San Diego, CA 92103
USA

Tel: +1 619 543 8080 x275
Fax: +1 619 298 0177
Email: sdfrost at ucsd.edu

The information transmitted in this e-mail is intended only for the
person(s) or entity to which it is addressed and may contain
CONFIDENTIAL and/or privileged material. Any review, re-transmission,
dissemination or other use of or taking of any action in reliance upon
this information by persons or entities other than the intended
recipient is prohibited. If you received this e-mail in error, please
contact the sender and delete/destroy the material/information from any
computer.








More information about the Pyrex mailing list