Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.2k
py/runtime.c: Add support for using __all__ in star import.#17331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Uh oh!
There was an error while loading.Please reload this page.
This file was deleted.
Uh oh!
There was an error while loading.Please reload this page.
This file was deleted.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# 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") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
__all__ = ['publicFun', 'PublicClass', 'dynamicFun'] | ||
# Definitions below should always be imported by a star import | ||
def publicFun(): | ||
return 1 | ||
class PublicClass: | ||
def __init__(self): | ||
self._val = 1 | ||
# If __all__ support is enabled, definitions below | ||
# should not be imported by a star import | ||
def unlistedFun(): | ||
return 0 | ||
class UnlistedClass: | ||
def __init__(self): | ||
self._val = 0 | ||
# Definitions below should be not be imported by a star import | ||
# (they start with an underscore, and are not listed in __all__) | ||
def _privateFun(): | ||
return -1 | ||
class _PrivateClass: | ||
def __init__(self): | ||
self._val = -1 | ||
# Test lazy loaded function, as used by extmod/asyncio: | ||
# Works with a star import only if __all__ support is enabled | ||
_attrs = { | ||
"dynamicFun": "funcs", | ||
} | ||
def __getattr__(attr): | ||
mod = _attrs.get(attr, None) | ||
if mod is None: | ||
raise AttributeError(attr) | ||
value = getattr(__import__(mod, globals(), locals(), True, 1), attr) | ||
globals()[attr] = value | ||
return value |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def dynamicFun(): | ||
return 777 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__all__ = 42 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
__all__ = ('existingFun', 'missingFun') | ||
def existingFun(): | ||
return None | ||
# missingFun is not defined, should raise an error on import |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
__all__ = ('publicFun2', 'PublicClass2') | ||
# Definitions below should always be imported by a star import | ||
def publicFun2(): | ||
return 2 | ||
class PublicClass2: | ||
def __init__(self): | ||
self._val = 2 | ||
# If __all__ support is enabled, definitions below | ||
# should not be imported by a star import | ||
def unlistedFun2(): | ||
return 0 | ||
class UnlistedClass2: | ||
def __init__(self): | ||
self._val = 0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# When __all__ is undefined, star import should only | ||
# show objects that do not start with an underscore | ||
def visibleFun(): | ||
return 42 | ||
class VisibleClass: | ||
def __init__(self): | ||
self._val = 42 | ||
def _hiddenFun(): | ||
return -1 | ||
class _HiddenClass: | ||
def __init__(self): | ||
self._val = -1 |
Uh oh!
There was an error while loading.Please reload this page.