tests: Add tests for overriding builtins.__import__.

This commit is contained in:
Damien George 2019-07-31 22:34:43 +10:00
parent f60229e261
commit 48f43b77aa
2 changed files with 35 additions and 0 deletions

View File

@ -12,7 +12,25 @@ except AttributeError:
print(abs(1))
# __build_class__ is handled in a special way
orig_build_class = __build_class__
builtins.__build_class__ = lambda x, y: ('class', y)
class A:
pass
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

View 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')