2020-09-25 11:17:11 -04:00
|
|
|
# test the __globals__ attribute of a function
|
|
|
|
|
|
|
|
|
|
|
|
def foo():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
if not hasattr(foo, "__globals__"):
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
|
|
|
print(type(foo.__globals__))
|
|
|
|
print(foo.__globals__ is globals())
|
|
|
|
|
|
|
|
foo.__globals__["bar"] = 123
|
|
|
|
print(bar)
|
|
|
|
|
|
|
|
try:
|
|
|
|
foo.__globals__ = None
|
|
|
|
except AttributeError:
|
|
|
|
print("AttributeError")
|
2022-06-24 09:34:15 -04:00
|
|
|
|
|
|
|
# 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())
|