Override builtin/frozen module functions
Posted: Mon Nov 22, 2021 8:44 pm
Hi,
i can easily override print in micropython when "exec"'ing user provided code:
This happily outputs
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::
unexpectedly fails with
Is there a way to do this in micropython?
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 })
Code: Select all
>> Hallo
Code: Select all
import time
time.sleep = lambda x: None
Code: Select all
AttributeError: 'module' object has no attribute 'sleep'