I have a test.py file:
Code: Select all
def test(arg1,arg2):
a = int(arg1)
b = int(arg2)
c= a+b
print("a+b=",c)
test(5,6)
Code: Select all
STATIC int do_file(const char *file) {
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
// create lexer based on source kind
mp_lexer_t *lex;
lex = mp_lexer_new_from_file((const char*)file);
qstr source_name = lex->source_name;
mp_parse_tree_t parse_tree = mp_parse(lex, MP_PARSE_FILE_INPUT);
mp_obj_t module_fun = mp_compile(&parse_tree, source_name, false);
mp_obj_t ret = mp_call_function_0(module_fun);
nlr_pop();
return 0;
} else {
// uncaught exception
printf("uncaught exception\n");
return (mp_obj_t)nlr.ret_val;
}
}
Now I want to call python file with input parameters and get the result return, I have tried mp_call_function_2(module_fun,mp_obj_new_int(arg1),mp_obj_new_int(arg2)) but it go uncaught exception.
And I don't know how to let C program recognize the function defined in the python file, is there any example?