Module safe_eval :: Class SafeEvalVisitor
[hide private]
[frames] | no frames]

Class SafeEvalVisitor

source code

object --+
         |
        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'. We'll also make
sure that there aren't any attempts to access/lookup restricted
builtin declared in 'unallowed_builtins'. By default we also won't
allow access to lowlevel stuff which can be used to dynamically
access non-local envrioments.

Interface:
  walk(ast) = validate AST and return True if AST is 'safe'

Attributes:
  errors = list of SafeEvalError if walk() returned False

Implementation:

The visitor will automatically generate methods for all of the
available AST node types and redirect them to self.ok or self.fail
reflecting the configuration in 'unallowed_ast_nodes'. While
walking the AST we simply forward the validating step to each of
node callbacks which take care of reporting errors.

Instance Methods [hide private]
 
__init__(self)
Initialize visitor by generating callbacks for all AST node types.
source code
 
walk(self, ast)
Validate each node in AST and return True if AST is 'safe'.
source code
 
visit(self, node, *args)
Recursively validate node and all of its children.
source code
 
visitName(self, node, *args)
Disallow any attempts to access a restricted builtin/attr.
source code
 
visitGetattr(self, node, *args)
Disallow any attempts to access a restricted attribute.
source code
 
ok(self, node, *args)
Default callback for 'harmless' AST nodes.
source code
 
fail(self, node, *args)
Default callback for unallowed AST nodes.
source code
 
trace(self, node)
Debugging utility for tracing the validation of AST nodes.
source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self)
(Constructor)

source code 

Initialize visitor by generating callbacks for all AST node types.

Overrides: object.__init__