[pypy-dev] How to get the return value from pypy function for C code

Yicong Huang hengha.mao at gmail.com
Wed May 13 09:55:00 CEST 2015


Thanks for your advices!
But your code seems to only solve the problem about how to pass the
function parameter from C to Python.
In the document example:

char source[] = "from cffi import FFI\n\ffi =
FFI()\n\@ffi.callback('int(int)')\n\def func(a):\n\    print 'Got from
C %d' % a\n\    return a * 2\n\ffi.cdef('int callback(int
(*func)(int));')\n\c_func = ffi.cast('int(*)(int(*)(int))',
c_argument)\n\c_func(func)\n\print 'finished the Python part'\n\";
int callback(int (*func)(int)){
    printf("Calling to Python, result: %d\n", func(3));}

We are able to pass the function paramter '3' to python function func(a).
But how could we extract the function result '6' to C code?
In CPython, there is  'PyObject_CallObject', which returns python fucntion
result.
Does pypy have similar API?

On Wed, May 13, 2015 at 3:25 PM, Amaury Forgeot d'Arc <amauryfa at gmail.com>
wrote:

> Hi,
>
> 2015-05-13 7:39 GMT+02:00 Yicong Huang <hengha.mao at gmail.com>:
>
>> From the document " "Embedding Pypy", we got the method to call python
>> function from C code.
>> But there are no examples on how to get the function return value.
>> Does pypy have this feature?
>>
>
>
> I think you need to use the function "pypy_execute_source_ptr", and pass a
> pointer to a C variable.
> From the Python side, it's turned into an int, that you can cast back to a
> ffi pointer.
> Something like this:
>
> import cffi
> ffi = cffi.FFI()
> result_ptr = ffi.cast('double *', c_argument)
> result_ptr[0] = 42.0
>
> --
> Amaury Forgeot d'Arc
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pypy-dev/attachments/20150513/26ad849d/attachment.html>


More information about the pypy-dev mailing list