Page 1 of 1

Override builtin/frozen module functions

Posted: Mon Nov 22, 2021 8:44 pm
by MasterOfGizmo
Hi,

i can easily override print in micropython when "exec"'ing user provided code:

Code: Select all

def x_print(msg):
    print(">>", msg)

exec('print("Hallo")', { "print": x_print })
This happily outputs

Code: Select all

>> Hallo
Now i'd like to do the same to time.sleep. So my plan was to import time, override sleep and then have the exec'd code use that. But I cannot even override the sleep function::

Code: Select all

import time
time.sleep = lambda x: None
unexpectedly fails with

Code: Select all

AttributeError: 'module' object has no attribute 'sleep'
Is there a way to do this in micropython?