circuitpython/tests/import/import_star.py
Yoctopuce dev 66c0148022 py/runtime: Add support for using __all__ in star import.
When the symbol `__all__` is defined in a module, `mp_import_all()` should
import all listed symbols into the global environment, rather than relying
on the underscore-is-private default.  This is the standard in CPython.

Each item is loaded in the same way as if it would be an explicit import
statement, and will invoke the module's `__getattr__` function if needed.
This provides a straightforward solution for fixing star import of modules
using a dynamic loader, such as `extmod/asyncio` (see issue #7266).

This improvement has been enabled at BASIC_FEATURES level, to avoid
impacting devices with limited ressources, for which star import is of
little use anyway.

Additionally, detailled reporting of errors during `__all__` import has
been implemented to match CPython, but this is only enabled when
ERROR_REPORTING is set to MICROPY_ERROR_REPORTING_DETAILED.

Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
2025-06-23 16:05:12 +10:00

59 lines
1.7 KiB
Python

# test `from package import *` conventions, including __all__ support
#
# This test requires MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_BASIC_FEATURES
try:
next(iter([]), 42)
except TypeError:
# 2-argument version of next() not supported
# we are probably not at MICROPY_CONFIG_ROM_LEVEL_BASIC_FEATURES
print('SKIP')
raise SystemExit
# 1. test default visibility
from pkgstar_default import *
print('visibleFun' in globals())
print('VisibleClass' in globals())
print('_hiddenFun' in globals())
print('_HiddenClass' in globals())
print(visibleFun())
# 2. test explicit visibility as defined by __all__ (as an array)
from pkgstar_all_array import *
print('publicFun' in globals())
print('PublicClass' in globals())
print('unlistedFun' in globals())
print('UnlistedClass' in globals())
print('_privateFun' in globals())
print('_PrivateClass' in globals())
print(publicFun())
# test dynamic import as used in asyncio
print('dynamicFun' in globals())
print(dynamicFun())
# 3. test explicit visibility as defined by __all__ (as an tuple)
from pkgstar_all_tuple import *
print('publicFun2' in globals())
print('PublicClass2' in globals())
print('unlistedFun2' in globals())
print('UnlistedClass2' in globals())
print(publicFun2())
# 4. test reporting of missing entries in __all__
try:
from pkgstar_all_miss import *
print("missed detection of incorrect __all__ definition")
except AttributeError as er:
print("AttributeError triggered for bad __all__ definition")
# 5. test reporting of invalid __all__ definition
try:
from pkgstar_all_inval import *
print("missed detection of incorrect __all__ definition")
except TypeError as er:
print("TypeError triggered for bad __all__ definition")