[pypy-dev] pypy callback function pointer become invalid
Armin Rigo
arigo at tunes.org
Mon Aug 17 10:08:48 CEST 2015
Hi Yicong,
On 17 August 2015 at 08:40, Yicong Huang <hengha.mao at gmail.com> wrote:
> yes, we could use a structure to wrap severl python callback function
> pointers in one execution.
> However, the issue is that we might not be able to get all python functions
> that would be executed at the beginning.
The basic idea is to define whatever API you need with just one call
to pypy_execute_source_ptr(). If you want to be able to compile and
execute arbitrary Python functions, just make that feature available
in your API. This is the same idea as PyRun_SimpleString(), which is
*one* API function but lets you execute arbitrary Python code.
Example:
struct API {
long (*run_function)(char *);
};
struct API api; /* global var */
int initialize_api(void) { /* run this only once */
static char source[] =
"import sys; sys.path.insert(0, '.'); "
"import interface; interface.fill_api(c_argument)";
pypy_execute_source_ptr(source, &api);
}
/* then execute 'api.run_function(some_source_code)' any number of times */
# file "interface.py"
import cffi
ffi = cffi.FFI()
ffi.cdef('''
struct API {
long (*run_function)(char *);
};
''')
@ffi.callback("long(char *)")
def run_function(p_src):
src = ffi.string(p_src)
# here 'src' is a string, we can do whatever we want with it---like eval()
return eval(src, {})
def fill_api(ptr):
global api
api = ffi.cast("struct API*", ptr)
api.run_function = run_function
---
Armin
More information about the pypy-dev
mailing list