havakv / safescope Goto Github PK
View Code? Open in Web Editor NEWPrevent access to global variables in functions
License: BSD 2-Clause "Simplified" License
Prevent access to global variables in functions
License: BSD 2-Clause "Simplified" License
@assert_local_vars gives an error when run with recursion.
@assert_local_vars
def foo(x):
if x == 0:
return 0
return x + foo(x-1)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-49-a28fcc2d95da> in <module>()
----> 1 @assert_local_vars
2 def foo(x):
3 if x == 0:
4 return 0
5 return x + foo(x-1)
~/packages/safescope/safescope/safescope.py in assert_local_vars(func)
9 variables from enclosing scopes.
10 '''
---> 11 var_names = get_non_local_var_names(func)
12 if len(var_names) > 0:
13 raise ValueError('Variables: ' + '[' + ', '.join(var_names) + '] ' +
~/packages/safescope/safescope/safescope.py in get_non_local_var_names(func)
21 obj_names = []
22 obj_names += non_local_callable_class_objects(func)
---> 23 obj_names += non_local_non_callable_variables(func)
24 return obj_names
25
~/packages/safescope/safescope/safescope.py in non_local_non_callable_variables(func)
65 if len(re.findall(r'[^\.]' + name + r'[^a-zA-z0-9_]', source)) > 0]
66
---> 67 return [name for name in non_callables if not _name_is_module(name, func.__globals__)]
~/packages/safescope/safescope/safescope.py in <listcomp>(.0)
65 if len(re.findall(r'[^\.]' + name + r'[^a-zA-z0-9_]', source)) > 0]
66
---> 67 return [name for name in non_callables if not _name_is_module(name, func.__globals__)]
~/packages/safescope/safescope/safescope.py in _name_is_module(name, globals)
52 def _name_is_module(name, globals):
53 '''If name is a package in global scope.'''
---> 54 return eval(name, globals).__class__.__name__ == 'module'
55
56 def non_local_non_callable_variables(func):
<string> in <module>()
NameError: name 'foo' is not defined
Consider one object safescope
that can be used as the wrapper @safescope
, as the context manager with safescope
, and has the side_scope.add
method safescope.add
.
Should add a warning if there is an unused variable in the function (like IDS' typically do)
Should use setattr
and getattr
instead of sidescope.__dict__
commands
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.