tests: Add tests for overriding builtins.__import__.
This commit is contained in:
parent
f60229e261
commit
48f43b77aa
@ -12,7 +12,25 @@ except AttributeError:
|
|||||||
print(abs(1))
|
print(abs(1))
|
||||||
|
|
||||||
# __build_class__ is handled in a special way
|
# __build_class__ is handled in a special way
|
||||||
|
orig_build_class = __build_class__
|
||||||
builtins.__build_class__ = lambda x, y: ('class', y)
|
builtins.__build_class__ = lambda x, y: ('class', y)
|
||||||
class A:
|
class A:
|
||||||
pass
|
pass
|
||||||
print(A)
|
print(A)
|
||||||
|
builtins.__build_class__ = orig_build_class
|
||||||
|
|
||||||
|
# __import__ is handled in a special way
|
||||||
|
def custom_import(name, globals, locals, fromlist, level):
|
||||||
|
print('import', name, fromlist, level)
|
||||||
|
class M:
|
||||||
|
a = 1
|
||||||
|
b = 2
|
||||||
|
return M
|
||||||
|
builtins.__import__ = custom_import
|
||||||
|
__import__('A', None, None, None, 0)
|
||||||
|
import a
|
||||||
|
import a.b
|
||||||
|
from a import a
|
||||||
|
from a.b import a, b
|
||||||
|
from .a import a
|
||||||
|
from ..a import a, b
|
||||||
|
17
tests/import/import_override.py
Normal file
17
tests/import/import_override.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# test overriding __import__ combined with importing from the filesystem
|
||||||
|
|
||||||
|
def custom_import(name, globals, locals, fromlist, level):
|
||||||
|
print('import', name, fromlist, level)
|
||||||
|
class M:
|
||||||
|
var = 456
|
||||||
|
return M
|
||||||
|
|
||||||
|
orig_import = __import__
|
||||||
|
try:
|
||||||
|
__import__("builtins").__import__ = custom_import
|
||||||
|
except AttributeError:
|
||||||
|
print("SKIP")
|
||||||
|
raise SystemExit
|
||||||
|
|
||||||
|
# import1a will be done via normal import which will import1b via our custom import
|
||||||
|
orig_import('import1a')
|
Loading…
Reference in New Issue
Block a user