enabling or disabling micropython modules at runtime

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
ThomasK
Posts: 5
Joined: Wed Nov 22, 2017 1:56 pm

enabling or disabling micropython modules at runtime

Post by ThomasK » Tue Jan 30, 2018 10:55 am

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...

User avatar
deshipu
Posts: 1348
Joined: Thu May 28, 2015 5:54 pm

Re: enabling or disabling micropython modules at runtime

Post by deshipu » Tue Jan 30, 2018 7:17 pm

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.

ThomasK
Posts: 5
Joined: Wed Nov 22, 2017 1:56 pm

Re: enabling or disabling micropython modules at runtime

Post by ThomasK » Wed Jan 31, 2018 9:01 am

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__.

User avatar
deshipu
Posts: 1348
Joined: Thu May 28, 2015 5:54 pm

Re: enabling or disabling micropython modules at runtime

Post by deshipu » Wed Jan 31, 2018 8:45 pm

Personally I would just use different firmware for the factory functions.

Post Reply