Skip to content

Commit 2752d39

Browse files
committed
esp32/modules: Use "from machine import *" instead of __getattr__.
The esp32 port has the machine Counter and Encoder classes implemented in Python, requiring a `machine.py` that extends the built-in machine module. That previously used `__getattr__()` to delegate lookups to the built-in, but that means any failed lookup raises an `AttributeError` instead of an `ImportError`. This means (among other things) that certain tests like CAN and I2CTarget would fail because they couldn't skip the test correctly. This commit improves the situation by using `from machine import *` instead of `__getattr__()`, which puts all the built-in functions/classes/constants directly in the `machine.py` global namespace. That means an `ImportError` is now correctly raised for attributes that don't exist. Although this takes up a bit more RAM, it's now a lot faster to import from the machine module: what used to take around 100us to lookup a name now takes only 5us. Signed-off-by: Damien George <damien@micropython.org>
1 parent c895770 commit 2752d39

1 file changed

Lines changed: 1 addition & 6 deletions

File tree

ports/esp32/modules/machine.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
_path = sys.path
44
sys.path = ()
55
try:
6-
import machine as _machine
6+
from machine import *
77
finally:
88
sys.path = _path
99
del _path
@@ -185,8 +185,3 @@ def phases(self):
185185

186186

187187
del esp32
188-
189-
190-
# Delegate to built-in machine module.
191-
def __getattr__(attr):
192-
return getattr(_machine, attr)

0 commit comments

Comments
 (0)