2015-05-13 9:55 GMT+02:00 Yicong Huang <hengha....@gmail.com>: > 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? >
But don't you have it already? the func(3) above should return the integer 6! > > On Wed, May 13, 2015 at 3:25 PM, Amaury Forgeot d'Arc <amaur...@gmail.com> > wrote: > >> Hi, >> >> 2015-05-13 7:39 GMT+02:00 Yicong Huang <hengha....@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 >> > > -- Amaury Forgeot d'Arc
_______________________________________________ pypy-dev mailing list pypy-dev@python.org https://mail.python.org/mailman/listinfo/pypy-dev