Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_functions_are_disallowed_in_expressions(self):
DISALLOWED = [type, isinstance, eval, getattr, setattr, help, repr, compile, open]
if simpleeval.PYTHON3:
exec('DISALLOWED.append(exec)') # exec is not a function in Python2...
for f in simpleeval.DISALLOW_FUNCTIONS:
assert f in DISALLOWED
DF = simpleeval.DEFAULT_FUNCTIONS.copy()
for x in DISALLOWED:
simpleeval.DEFAULT_FUNCTIONS = DF.copy()
with self.assertRaises(FeatureNotAvailable):
s = SimpleEval()
s.functions['foo'] = x
s.eval('foo(42)')
simpleeval.DEFAULT_FUNCTIONS = DF.copy()
print()
def double(x):
return x * 2
my_functions = {
"d": double,
"double": double
}
print(simple_eval("d(100) + double(1)", functions=my_functions)) # 202
import simpleeval
my_functions = simpleeval.DEFAULT_FUNCTIONS.copy()
my_functions.update(
square=lambda x: x * x,
double=lambda x: x * 2,
)
print(simple_eval('square(randint(100))', functions=my_functions)) # 1024
def set_parent(self, parent):
self._effect.set_parent(parent._effect)
class AliasException(AvraeException):
pass
def raise_alias_exception(reason):
raise AliasException(reason)
DEFAULT_OPERATORS = simpleeval.DEFAULT_OPERATORS.copy()
DEFAULT_OPERATORS.pop(ast.Pow)
DEFAULT_FUNCTIONS = simpleeval.DEFAULT_FUNCTIONS.copy()
DEFAULT_FUNCTIONS.update({'floor': floor, 'ceil': ceil, 'round': round, 'len': len, 'max': max, 'min': min,
'range': safe_range, 'sqrt': sqrt, 'sum': sum, 'any': any, 'all': all,
'roll': simple_roll, 'vroll': verbose_roll, 'load_json': load_json, 'dump_json': dump_json,
'time': time.time, 'err': raise_alias_exception})
if __name__ == '__main__':
evaluator = ScriptingEvaluator()
while True:
try:
evaluator.eval(input("Evaluate: ").strip())
except Exception as e:
print(e)
continue
print(evaluator.names)