py/objfun: Support fun.__globals__ attribute.

This returns a reference to the globals dict associated with the function,
ie the global scope that the function was defined in.  This attribute is
read-only but the dict itself is modifiable, per CPython behaviour.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George
2020-09-26 01:17:11 +10:00
parent 5d68b5e22c
commit 925bd67cfb
2 changed files with 25 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
# 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")