Page 1 of 1

enabling or disabling micropython modules at runtime

Posted: Tue Jan 30, 2018 10:55 am
by ThomasK
Hi,

I was wondering if it there was a way to register new custom modules in micropython during runtime?

I've currently implemented this by registering them in the MICROPY_PORT_BUILTIN_MODULES list,
and capturing the "import" command it via mp_module_get so the module can't be found/imported unless some other criteria are met,
however this does not exclude the module from being listed in help("modules"), and just feels "wrong". :?

Is there another, better way to block access to / disable / hide a specific module at runtime?

once loaded, the module can of course remain loaded until the kernel is rebooted...

Re: enabling or disabling micropython modules at runtime

Posted: Tue Jan 30, 2018 7:17 pm
by deshipu
I can't help but wonder what is your use case for this? If you don't want a module, simply don't import it.

Re: enabling or disabling micropython modules at runtime

Posted: Wed Jan 31, 2018 9:01 am
by ThomasK
The use case is fairly simple, µPy is used to call certain factory calibration routines,
This module should only be used for configuring factory calibration settings, for example offset values of the analog I/O.
having the module unlisted, and impossible to import avoids some of the more adventurous users trying to be clever and accidentally bricking the board.

Rather than requiring a password on each function of the module, i'd rather have it so the module doesn't exist at all unless unlocked.

I've already solved this by implementing a separate module map and adding it to mp_builtin___import__.

Re: enabling or disabling micropython modules at runtime

Posted: Wed Jan 31, 2018 8:45 pm
by deshipu
Personally I would just use different firmware for the factory functions.