diff options
| author | Jim Mussared <jim.mussared@gmail.com> | 2023-05-12 17:10:07 +1000 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2023-06-01 16:21:37 +1000 |
| commit | dfa7677e2fb1ff2314be04b5ea05346ac168fe84 (patch) | |
| tree | 0cfa7945367f927ba67f49bb6e895de70a8e2063 | |
| parent | 5e0452125146f3afed89c09f5813790156d24471 (diff) | |
tests/import/builtin_ext.py: Add test for built-in module override.
This verifies the behavior:
- Exact matches of built-ins bypass filesystem.
- u-prefix modules 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>
| -rw-r--r-- | tests/import/builtin_ext.py | 36 | ||||
| -rw-r--r-- | tests/import/builtin_ext.py.exp | 10 | ||||
| -rw-r--r-- | tests/import/ext/micropython.py | 2 | ||||
| -rw-r--r-- | tests/import/ext/os.py | 5 | ||||
| -rw-r--r-- | tests/import/ext/sys.py | 2 | ||||
| -rw-r--r-- | tests/import/ext/time.py | 14 | ||||
| -rw-r--r-- | tests/import/ext/usys.py | 3 |
7 files changed, 72 insertions, 0 deletions
diff --git a/tests/import/builtin_ext.py b/tests/import/builtin_ext.py new file mode 100644 index 000000000..dd8f95cbc --- /dev/null +++ b/tests/import/builtin_ext.py @@ -0,0 +1,36 @@ +import sys + +print(sys, hasattr(sys, "__file__")) + +sys.path.clear() +sys.path.append("ext") + +# All three should only get builtins, despite sys.py, usys.py, and +# micropython.py being in the path. +import micropython + +print(micropython, hasattr(micropython, "__file__")) +import sys + +print(sys, hasattr(sys, "__file__")) +import usys + +print(usys, hasattr(usys, "__file__")) + +# This should get os.py, which uses uos to get the builtin. +import os + +print(os, hasattr(os, "__file__"), os.sep, os.extra) + +# This should get time.py, which uses empty sys.path to get the builtin. +import time + +print(time, hasattr(time, "__file__"), time.sleep, time.extra) + +# These should get the builtins. +import uos + +print(uos, hasattr(uos, "__file__"), hasattr(uos, "extra")) +import utime + +print(utime, hasattr(utime, "__file__"), hasattr(utime, "extra")) diff --git a/tests/import/builtin_ext.py.exp b/tests/import/builtin_ext.py.exp new file mode 100644 index 000000000..08496e14b --- /dev/null +++ b/tests/import/builtin_ext.py.exp @@ -0,0 +1,10 @@ +<module 'sys'> False +<module 'micropython'> False +<module 'sys'> False +<module 'sys'> False +os from filesystem +<module 'os' from 'ext/os.py'> True / 1 +time from filesystem +<module 'time' from 'ext/time.py'> True <function> 1 +<module 'uos'> False False +<module 'utime'> False False diff --git a/tests/import/ext/micropython.py b/tests/import/ext/micropython.py new file mode 100644 index 000000000..88c8b770e --- /dev/null +++ b/tests/import/ext/micropython.py @@ -0,0 +1,2 @@ +# micropython is always builtin and cannot be overriden by the filesystem. +print("ERROR: micropython from filesystem") diff --git a/tests/import/ext/os.py b/tests/import/ext/os.py new file mode 100644 index 000000000..e1448805d --- /dev/null +++ b/tests/import/ext/os.py @@ -0,0 +1,5 @@ +print("os from filesystem") + +from uos import * + +extra = 1 diff --git a/tests/import/ext/sys.py b/tests/import/ext/sys.py new file mode 100644 index 000000000..71ee633e1 --- /dev/null +++ b/tests/import/ext/sys.py @@ -0,0 +1,2 @@ +# sys is always builtin and cannot be overriden by the filesystem. +print("ERROR: sys from filesystem") diff --git a/tests/import/ext/time.py b/tests/import/ext/time.py new file mode 100644 index 000000000..6f2d4a42c --- /dev/null +++ b/tests/import/ext/time.py @@ -0,0 +1,14 @@ +print("time from filesystem") + +# Tests the CPython-compatible / non-u-prefix way of forcing a builtin +# import. +import sys + +_path = sys.path[:] +sys.path.clear() +from time import * + +sys.path.extend(_path) +del _path + +extra = 1 diff --git a/tests/import/ext/usys.py b/tests/import/ext/usys.py new file mode 100644 index 000000000..d7629a449 --- /dev/null +++ b/tests/import/ext/usys.py @@ -0,0 +1,3 @@ +# usys (and any u-prefix) is always builtin and cannot be overriden by the +# filesystem. +print("ERROR: usys from filesystem") |
