8000 tests/import/builtin_ext.py: Add test for overriding builtins. · micropython/micropython@8c95285 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8c95285

Browse files
committed
tests/import/builtin_ext.py: Add test for overriding builtins.
This verifies the behavior: - u-prefix, sys, micropython are always builtin. - Other builtins can be overridden from the filesystem. - Builtin import can be forced using either u-prefix or sys.path=[]. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
1 parent 6918fd7 commit 8c95285

File tree

7 files changed

+72
-0
lines changed

7 files changed

+72
-0
lines changed

tests/import/builtin_ext.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import sys
2+
3+
print(sys, hasattr(sys, "__file__"))
4+
5+
sys.path.clear()
6+
sys.path.append("import/ext") # TODO: Update after #11468 to "ext"
7+
8+
# All three should only get builtins, despite sys.py, usys.py, and
9+
# micropython.py being in the path.
10+
import micropython
11+
12+
print(micropython, hasattr(micropython, "__file__"))
13+
import sys
14+
15+
print(sys, hasattr(sys, "__file__"))
16+
import usys
17+
18+
print(usys, hasattr(usys, "__file__"))
19+
20+
# This should get os.py, which uses uos to get the builtin.
21+
import os
22+
23+
print(os, hasattr(os, "__file__"), os.sep, os.extra)
24+
25+
# This should get time.py, which uses empty sys.path to get the builtin.
26+
import time
27+
28+
print(time, hasattr(time, "__file__"), time.sleep, time.extra)
29+
30+
# These should get the builtins.
31+
import uos
32+
33+
print(uos, hasattr(uos, "__file__"), hasattr(uos, "extra"))
34+
import utime
35+
36+
print(utime, hasattr(utime, "__file__"), hasattr(utime, "extra"))

tests/import/builtin_ext.py.exp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<module 'sys'> False
2+
<module 'micropython'> False
3+
<module 'sys'> False
4+
<module 'sys'> False
5+
os from filesystem
6+
<module 'os' from 'import/ext/os.py'> True / 1
7+
time from filesystem
8+
<module 'time' from 'import/ext/time.py'> True <function> 1
9+
<module 'uos'> False False
10+
<module 'utime'> False False

tests/import/ext/micropython.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# micropython is always builtin and cannot be overriden by the filesystem.
2+
print("ERROR: micropython from filesystem")

tests/import/ext/os.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
print("os from filesystem")
2+
3+
from uos import *
4+
5+
extra = 1

tests/import/ext/sys.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# sys is always builtin and cannot be overriden by the filesystem.
2+
print("ERROR: sys from filesystem")

tests/import/ext/time.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
print("time from filesystem")
2+
3+
# Tests the CPython-compatible / non-u-prefix way of forcing a builtin
4+
# import.
5+
import sys
6+
7+
_path = sys.path[:]
8+
sys.path.clear()
9+
from time import *
10+
11+
sys.path.extend(_path)
12+
del _path
13+
14+
extra = 1

tests/import/ext/usys.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# usys (and any u-prefix) is always builtin and cannot be overriden by the
2+
# filesystem.
3+
print("ERROR: usys from filesystem")

0 commit comments

Comments
 (0)
0