Page 1 of 1

frozen modules - execution vs. import

Posted: Sun Jan 03, 2021 8:29 pm
by oclyke
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:

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);
	}
}
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

Re: frozen modules - execution vs. import

Posted: Mon Jan 04, 2021 8:10 am
by stijn
Actual importing of a module is done using mp_builtin___import__, not mp_module_get

Re: frozen modules - execution vs. import

Posted: Tue Jan 05, 2021 3:42 am
by oclyke
aha! excellent - that was exactly what I was looking for. previously I had not considered the "builtin" functions as "okay" to call from my application. this opens up a world of possibilities. thank you stijn

Re: frozen modules - execution vs. import

Posted: Tue Jan 05, 2021 8:19 am
by stijn
You're welcome; I get where the confusion about what can be used comes from, but the idea is basically this: everything which is in .h files is part of the public API and can be used. (however dynamic native modules do not currently implement all of that)