Trees | Indices | Help |
---|
|
'Safe' python code evaluation
Based on the public domain code of Babar K. Zafar http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496746 (version 0.1 or 1.2 May 27 2006)
The idea is to examine the compiled ast tree and chack for invalid entries
I have removed the timeout checking as this probably isn't a serious problem for veusz documents
|
|||
SafeEvalError Base class for all which occur while walking the AST. |
|||
SafeEvalASTNodeError Expression/statement in AST evaluates to a restricted AST node type. |
|||
SafeEvalBuiltinError Expression/statement in tried to access a restricted builtin. |
|||
SafeEvalAttrError Expression/statement in tried to access a restricted attribute. |
|||
SafeEvalVisitor Data-driven visitor which walks the AST for some code and makes sure it doesn't contain any expression/statements which are declared as restricted in 'unallowed_ast_nodes'. |
|||
SafeEvalException Base class for all safe-eval related errors. |
|||
SafeEvalCodeException Exception class for reporting all errors which occured while validating AST for source code in safe_eval(). |
|||
SafeEvalContextException Exception class for reporting unallowed objects found in the dict intended to be used as the local enviroment in safe_eval(). |
|||
SafeEvalTimeoutException Exception class for reporting that code evaluation execeeded the given timelimit. |
|||
TestSafeEval |
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|
|||
DEBUG = False
|
|||
all_ast_nodes =
|
|||
all_builtins =
|
|||
unallowed_ast_nodes =
|
|||
unallowed_builtins =
|
|||
unallowed_attr =
|
|||
ast_name =
|
|||
name =
|
|
Check the context statements will be executed in. Returns True if context is okay |
Dynamically execute 'code' using 'context' as the global enviroment. SafeEvalTimeoutException is raised if execution does not finish within the given timelimit. |
Validate source code and make sure it contains no unauthorized expression/statements as configured via 'unallowed_ast_nodes' and 'unallowed_builtins'. By default this means that code is not allowed import modules or access dangerous builtins like 'open' or 'eval'. If code is considered 'safe' it will be executed via 'exec' using 'context' as the global environment. More details on how code is executed can be found in the Python Reference Manual section 6.14 (ignore the remark on '__builtins__'). The 'context' enviroment is also validated and is not allowed to contain modules or builtins. The following exception will be raised on errors: if 'context' contains unallowed objects = SafeEvalContextException if code is didn't validate and is considered 'unsafe' = SafeEvalCodeException if code did not execute within the given timelimit = SafeEvalTimeoutException |
|
all_ast_nodes
|
all_builtins
|
unallowed_ast_nodes
|
unallowed_builtins
|
unallowed_attr
|
Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Sun Aug 17 06:44:38 2008 | http://epydoc.sourceforge.net |