Closed delgadom closed 4 years ago
Note that this does not catch undeclared references, so @block_globals would not raise an exception in this case:
In [2]: @block_globals
...: def myfunc(num):
...: return number + 5
...:
In [3]: number = 5
In [4]: myfunc(1)
10
note that this makes use of the global number
, which snuck into the function as an undeclared reference until after myfunc's definition.
Whoa. Was trying to debug some weird behavior and found out that inspect.getclosurevars
does not handle attributes correctly:
In [1]: import inspect
In [2]: def get_persons_name(person):
...: return person.name
...:
In [3]: inspect.getclosurevars(get_persons_name)
Out[3]: ClosureVars(nonlocals={}, globals={}, builtins={}, unbound={'name'})
In [4]: age = 4
...:
...: def get_persons_age(person):
...: return person.age
...:
In [5]: inspect.getclosurevars(get_persons_age)
Out[5]: ClosureVars(nonlocals={}, globals={'age': 4}, builtins={}, unbound=set())
This is a deal breaker for this function, which should prevent unbound variables and should not prevent attributes!
May have to use dis.get_instructions
to differentiate globals from locals. Might get gnarly.
The above decorator has been updated to fix this behavior
Imagine there's no globals No longer hard to dooooooo Every object is local Whitelisting's up to youuuuuuuu