tests/basics: Add tests for __name__ and __globals__ attrs on closures.
Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
parent
d68532558d
commit
268ec1e3eb
|
@ -19,3 +19,18 @@ try:
|
||||||
foo.__globals__ = None
|
foo.__globals__ = None
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
print("AttributeError")
|
print("AttributeError")
|
||||||
|
|
||||||
|
# test closures have the __globals__ attribute
|
||||||
|
|
||||||
|
|
||||||
|
def outer():
|
||||||
|
x = 1
|
||||||
|
|
||||||
|
def inner():
|
||||||
|
return x
|
||||||
|
|
||||||
|
return inner
|
||||||
|
|
||||||
|
|
||||||
|
print(outer.__globals__ is globals())
|
||||||
|
print(outer().__globals__ is globals())
|
||||||
|
|
|
@ -24,9 +24,11 @@ except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# name of a function that has closed over variables
|
# name of a function that has closed over variables
|
||||||
|
# and also the name of the inner closure
|
||||||
def outer():
|
def outer():
|
||||||
x = 1
|
x = 1
|
||||||
def inner():
|
def inner():
|
||||||
return x
|
return x
|
||||||
return inner
|
return inner
|
||||||
print(outer.__name__)
|
print(outer.__name__)
|
||||||
|
print(outer().__name__)
|
||||||
|
|
Loading…
Reference in New Issue