frozen modules - execution vs. import
Posted: Sun Jan 03, 2021 8:29 pm
Hi everyone.
I am trying to trigger execution of certain frozen python code (module members etc) from underlying C code. Thanks to some other posts on the forum (links at bottom) I've been able to:
- execute the built-in print() function with my own arguments
- execute the built in os.listdir() module method (no errors, but did not see output printed to REPL)
so far so good for built-in modules and functions, however I have been unable to load a frozen module the same way.
generally the code looks like this:
this fails to load the module, even when attempting to import the module into global namespace by executing the frozen module with pyexec_frozen_module("test.py");.
However it *is* possible to achieve the desired results by executing another frozen module pyexec_frozen_module("import_test.py"); which simply contains import test. Inspecting the mp_module_get function reveals that the method needs to be loaded into the mp_loaded_modules_dict portion of the VM state (or be a builtin name). The only existing C API to add to the loaded modules dictionary seems to be void mp_module_register(qstr qst, mp_obj_t module) which requires a module object to append.
Is there some way for C code to get a module from frozen bytecode without using the kludgy-feeling method described above? For example something like mp_get_frozen_module().
Thanks in advance!
calling python from c
I am trying to trigger execution of certain frozen python code (module members etc) from underlying C code. Thanks to some other posts on the forum (links at bottom) I've been able to:
- execute the built-in print() function with my own arguments
- execute the built in os.listdir() module method (no errors, but did not see output printed to REPL)
so far so good for built-in modules and functions, however I have been unable to load a frozen module the same way.
generally the code looks like this:
Code: Select all
mp_obj_t test_module_obj = mp_module_get(MP_QSTR_test);
if(test_module_obj){
// mp_obj_t test_write_to_fn = mp_load_attr(test_module_obj, MP_QSTR_write_to);
// mp_obj_t test_write_to_fn = mp_import_from(test_module_obj, MP_QSTR_write_to);
mp_obj_t test_write_to_fn = NULL;
mp_load_method(test_module_obj, MP_QSTR_write_to, &test_write_to_fn);
if(test_write_to_fn){
mp_call_function_2(test_write_to_fn, file, data);
}
}
However it *is* possible to achieve the desired results by executing another frozen module pyexec_frozen_module("import_test.py"); which simply contains import test. Inspecting the mp_module_get function reveals that the method needs to be loaded into the mp_loaded_modules_dict portion of the VM state (or be a builtin name). The only existing C API to add to the loaded modules dictionary seems to be void mp_module_register(qstr qst, mp_obj_t module) which requires a module object to append.
Is there some way for C code to get a module from frozen bytecode without using the kludgy-feeling method described above? For example something like mp_get_frozen_module().
Thanks in advance!
calling python from c