def test_expr(expr, allowed_codes):
 import dis
 allowed_codes = [dis.opmap[c] for c in allowed_codes]
 try:
  c = compile(expr, '', 'eval')
 except SyntaxError:
  raise ValueError(('%s is not a valid expression' % expr))
 codes = _get_opcodes(c)
 for code in codes:
  if (code not in allowed_codes):
   raise ValueError(('opcode %s not allowed' % dis.opname[code]))
 return c

def test_expr ( expr , allowed_codes ) : import dis allowed_codes = [ dis . opmap [ c ] for c in allowed_codes ] try : c = compile ( expr , '' , 'eval' ) except SyntaxError : raise ValueError ( ( '%s is not a valid expression' % expr ) ) codes = _get_opcodes ( c ) for code in codes : if ( code not in allowed_codes ) : raise ValueError ( ( 'opcode %s not allowed' % dis . opname [ code ] ) ) return c
test_expr -> code_object test that the expression contains only the allowed opcodes .