Skip to content

Instantly share code, notes, and snippets.



Created Jul 26, 2012
What would you like to do?
Limited evaluator for Python expressions
vinay@eta-jaunty:~/projects/scratch$ python
Enter source to evaluate:handlers.WatchedFileHandler
Enter source to evaluate:FileHandler
Enter source to evaluate:DEBUG
Enter source to evaluate:INFO
Enter source to evaluate:__all__[:3]
['BASIC_FORMAT', 'BufferingFormatter', 'CRITICAL']
Enter source to evaluate:DEBUG < INFO < WARNING
# Copyright 2012 by Vinay Sajip. All Rights Reserved.
import ast
import logging
import logging.handlers
import sys
class EvaluationError(ValueError):
class Evaluator(object):
builtin_names = {
'None': None,
'False': False,
'True': True,
operators = {
'add': lambda x, y: x + y,
'bitand': lambda x, y: x & y,
'bitor': lambda x, y: x | y,
'bitxor': lambda x, y: x ^ y,
'div': lambda x, y: x / y,
'eq': lambda x, y: x == y,
'floordiv': lambda x, y: x // y,
'gt': lambda x, y: x > y,
'gte': lambda x, y: x >= y,
'in': lambda x, y: x in y,
'invert': lambda x: ~x,
'lshift': lambda x, y: x << y,
'lt': lambda x, y: x < y,
'lte': lambda x, y: x <= y,
'mod': lambda x, y: x % y,
'mult': lambda x, y: x * y,
'not': lambda x: not x,
'noteq': lambda x, y: x != y,
'notin': lambda x, y: x not in y,
'pow': lambda x, y: x ** y,
'rshift': lambda x, y: x >> y,
'sub': lambda x, y: x - y,
'uadd': lambda x: +x,
'usub': lambda x: -x,
handlers = {}
def __init__(self, context=None, allow_imports=False):
self.context = context or {}
self.source = None
self.allow_imports = allow_imports
def get_fragment(self, offset):
fragment_len = 10
s = 'at position %d: %r' % (offset, self.source[offset:offset + fragment_len])
if offset + fragment_len < len(self.source):
s += '...'
return s
def evaluate(self, node, filename=None):
if isinstance(node, str):
self.source = node
kwargs = dict(mode='eval')
if filename:
kwargs['filename'] = filename
node = ast.parse(node, **kwargs)
except SyntaxError as e:
s = self.get_fragment(e.offset)
raise EvaluationError('syntax error %s' % s)
node_type = node.__class__.__name__.lower()
if node_type in self.handlers:
handler = self.handlers[node_type]
handler = getattr(self, 'do_%s' % node_type, None)
if handler is None:
if self.source is None:
s = '(source not available)'
s = self.get_fragment(node.col_offset)
raise EvaluationError("don't know how to evaluate %r %s" % (
node_type, s))
return handler(node)
def do_attribute(self, node):
container = self.evaluate(node.value)
return getattr(container, node.attr)
def do_binop(self, node):
op = node.op.__class__.__name__.lower()
if op not in self.operators:
raise EvaluationError('unsupported operation: %r' % op)
lhs = self.evaluate(node.left)
rhs = self.evaluate(node.right)
return self.operators[op](lhs, rhs)
def do_boolop(self, node):
result = self.evaluate(node.values[0])
is_or = node.op.__class__ is ast.Or
is_and = node.op.__class__ is ast.And
assert is_or or is_and
if (is_and and result) or (is_or and not result):
for n in node.values[1:]:
result = self.evaluate(n)
if (is_or and result) or (is_and and not result):
return result
def do_compare(self, node):
lhs = self.evaluate(node.left)
result = True
for op, right in zip(node.ops, node.comparators):
op = op.__class__.__name__.lower()
if op not in self.operators:
raise EvaluationError('unsupported operation: %r' % op)
rhs = self.evaluate(right)
result = self.operators[op](lhs, rhs)
if not result:
lhs = rhs
return result
def do_dict(self, node):
e = self.evaluate
return dict((e(k), e(v)) for k, v in zip(node.keys, node.values))
def do_ellipsis(self, node):
return Ellipsis
def do_expr(self, node):
return self.evaluate(node.value)
do_index = do_expr
def do_expression(self, node):
return self.evaluate(node.body)
def do_extslice(self, node):
e = self.evaluate
return tuple((e(n) for n in node.dims))
def do_list(self, node):
return list([self.evaluate(n) for n in node.elts])
def do_name(self, node):
if in self.builtin_names:
result = self.builtin_names[]
elif in self.context:
result = self.context[]
if not self.allow_imports:
raise EvaluationError('unknown name: %r' %
result = __import__(
except ImportError:
raise EvaluationError('unknown name: %r' %
return result
def do_num(self, node):
return node.n
def do_slice(self, node):
if node.lower is None:
lower = None
lower = self.evaluate(node.lower)
if node.upper is None:
upper = None
upper = self.evaluate(node.upper)
if node.step is None:
step = None
step = self.evaluate(node.step)
return slice(lower, upper, step)
def do_str(self, node):
return node.s
def do_subscript(self, node):
assert node.ctx.__class__ is ast.Load
val = self.evaluate(node.value)
if not isinstance(node.slice, (ast.Index, ast.Slice, ast.Ellipsis,
raise EvaluationError('Unable to get subscript: %r',
indices = self.evaluate(node.slice)
if isinstance(node.slice, ast.ExtSlice):
result = val[(indices)]
result = val.__getitem__(indices)
return result
def do_tuple(self, node):
return tuple([self.evaluate(n) for n in node.elts])
def do_unaryop(self, node):
op = node.op.__class__.__name__.lower()
operand = self.evaluate(node.operand)
if op not in self.operators:
raise EvaluationError('unsupported operation: %r' % op)
return self.operators[op](operand)
if __name__ == '__main__':
except NameError:
raw_input = input
context = dict(vars(logging))
context['handlers'] = logging.handlers
evaluator = Evaluator(context, True)
while True:
line = raw_input('Enter source to evaluate:').strip()
if not line:
result = evaluator.evaluate(line.strip(), '<interactive>')
except EvaluationError as e:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.