Re: Expose the c function to the Python environment
Lisandro Dalcin <dalcinl <at> gmail.com>
2010-07-02 13:57:20 GMT
On 2 July 2010 10:14, Rafael Cardoso Dias Costa <rfcard <at> gmail.com> wrote:
> Thank you, Lisandro!
>
> I'm testing the code that you wrote.
> I do not understand why Cython not doing the automatic conversion of types.
> He gives the following error message:
>
Try to add explicit casts to <char*>, like this out_callable(<char*>s)
>
>
> Error converting Pyrex file to C:
> ------------------------------------------------------------
> ...
> cdef object out_callable = None
> cdef object err_callable = None
>
> cdef int_t out_callback(const_char *s, int_t n):
> if out_callable is None: return 0
> ret = out_callable(s)
> ^
> ------------------------------------------------------------
>
> /home/rfcard/emso-python/CythonEMSO/emso.pyx:14:23: Cannot convert
> 'emso.const_char *' to Python object
>
>
>
>
> I changed "def" with "cdef" in indicated lines.
>
>
> cdef extern from "emso_dll.h":
> ctypedef long int_t "int_t"
> ctypedef char const_char "const_char"
> ctypedef int_t EMSO_REPORT(const_char *str, int_t len)
> void emso_init(EMSO_REPORT *out, EMSO_REPORT *err, const_char *path, \
> const_char *path_dll, int_t *ret_val)
>
>
> cdef object out_callable = None <==========================
> cdef object err_callable = None <==========================
>
> cdef int_t out_callback(const_char *s, int_t n):
> if out_callable is None: return 0
> ret = out_callable(s)
> if ret is None: return 0
> return ret
>
> cdef int_t err_callback(const_char *s, int_t n):
> if err_callable is None: return 0
> ret = err_callable(s)
> if ret is None: return 0
> return ret
>
> def init(path, path_dll, out=None, err=None):
> global out_callable, err_callable
> out_callable = out
> err_callable = err
> cdef int_t ret_val = 0
> emso_init(out_callback, err_callback, path, path_dll, &ret_val)
> return ret_val
>
>
>
> I await answers.
> Thank you!
>
>
> --
> Rafael Cardoso Dias Costa
> Control and Automation Engineer - UFMG
> Chemical Engineering Master Student - Process Control - UFRGS
>
>
>
>
>
>
>
> On Thu, Jul 1, 2010 at 4:36 PM, Lisandro Dalcin <dalcinl <at> gmail.com> wrote:
>>
>> On 1 July 2010 16:23, Rafael Cardoso Dias Costa <rfcard <at> gmail.com> wrote:
>> > Guys,
>> >
>> > I wish to expose the c function to the Python environment. How do I?
>> > I'm trying as follow, but is giving error.
>> >
>> > cdef extern from "emso_dll.h":
>> > ctypedef long int_t "int_t"
>> > ctypedef char const_char "const_char"
>> > ctypedef int_t EMSO_REPORT(const_char *str, int_t len)
>> > void emso_init(EMSO_REPORT *out, EMSO_REPORT *err, const_char *path,
>> > \
>> > const_char *path_dll, int_t *ret_val)
>> >
>> > cpdef py_emso_init(EMSO_REPORT *out, EMSO_REPORT *err, const_char *path,
>> > \
>> > const_char *path_dll, int_t *ret_val):
>> > emso_init(out, err, path, path_dll, ret_val)
>> >
>>
>> This is a pretty standard C-wrapping task
>>
>> You cannot pass pointers from Python code, then you have to accept
>> Python callables, use a couple of global vars, and a wrapper C
>> function: more or less like below.
>>
>> def object out_callable = None
>> def object err_callable = None
>>
>> cdef int_t out_callback(const_char *s, int_t n):
>> if out_callable is None: return 0
>> ret = out_callable(s)
>> if ret is None: return 0
>> return ret
>>
>> cdef int_t err_callback(const_char *s, int_t n):
>> if err_callable is None: return 0
>> ret = err_callable(s)
>> if ret is None: return 0
>> return ret
>>
>> def init(path, path_dll, out=None, err=None):
>> global out_callable, err_callable
>> out_callable = out
>> err_callable = err
>> cdef int_t ret_val = 0
>> emso_init(out_callback, err_callback, path, path_dll, &ret_val)
>> return ret_val
>>
>> Disclaimer: This was written in the browser, so is untested, I leave
>> the details to you.
>>
>> --
>> Lisandro Dalcin
>> ---------------
>> CIMEC (INTEC/CONICET-UNL)
>> Predio CONICET-Santa Fe
>> Colectora RN 168 Km 472, Paraje El Pozo
>> Tel: +54-342-4511594 (ext 1011)
>> Tel/Fax: +54-342-4511169
>
>
>
--
--
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169