From 925bd67cfb1607264fae79a4fdf5e79ab1ae46aa Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 26 Sep 2020 01:17:11 +1000 Subject: 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 --- tests/basics/fun_globals.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/basics/fun_globals.py (limited to 'tests') diff --git a/tests/basics/fun_globals.py b/tests/basics/fun_globals.py new file mode 100644 index 000000000..3f32e8bdb --- /dev/null +++ b/tests/basics/fun_globals.py @@ -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") -- cgit v1.2.3