Calling a python function from C
Re: Calling a python function from C
lists in python can be created from any iterable, and list is an iterable itself. In other words: use the constructor functionality to copy a list just like you would in Python, i.e. use mp_obj_new_list or whatever it's called. Likely that is exactly what list_copy does as well, didn't check.
Re: Calling a python function from C
I'd have to dostijn wrote: ↑Sun Mar 04, 2018 9:29 amlists in python can be created from any iterable, and list is an iterable itself. In other words: use the constructor functionality to copy a list just like you would in Python, i.e. use mp_obj_new_list or whatever it's called. Likely that is exactly what list_copy does as well, didn't check.
Code: Select all
mp_type_list.make_new(NULL, 1, 0, &my_list);
Re: Calling a python function from C
You sure? I'm 99% sure you could do something like my_list_copy = mp_obj_new_list(my_list->len, my_list->items);
Last edited by stijn on Sun Mar 04, 2018 8:21 pm, edited 1 time in total.
Re: Calling a python function from C
It doesn't make a copy of the items. So if you use this function, the "items" will be shared between the two lists.
Code: Select all
mp_obj_t mp_obj_new_list(size_t n, mp_obj_t *items) {
mp_obj_list_t *o = list_new(n);
if (items != NULL) {
for (size_t i = 0; i < n; i++) {
o->items[i] = items[i];
}
}
return MP_OBJ_FROM_PTR(o);
}
Re: Calling a python function from C
What you want is called 'deepcopy'. Which is also exactly what the functionality is called in CPython. In general I'd say it's better to avoid that all together though, if you can. Maybe you should rethink your design. Having an attr function which makes deepcopies doesn't follow the 'principle of least surprise' at all.