Replace eval functions with helpers to catch exceptions

This commit is contained in:
JonnyWong16 2021-05-25 11:24:11 -07:00
parent 3510224cde
commit a2b2d1d227
2 changed files with 18 additions and 4 deletions

View file

@ -530,6 +530,20 @@ def cast_to_float(s):
return 0 return 0
def helper_divmod(a, b):
try:
return divmod(a, b)
except (ValueError, TypeError):
return 0
def helper_round(n, ndigits):
try:
return round(n, ndigits)
except (ValueError, TypeError):
return 0
def convert_xml_to_json(xml): def convert_xml_to_json(xml):
o = xmltodict.parse(xml) o = xmltodict.parse(xml)
return json.dumps(o) return json.dumps(o)

View file

@ -1818,10 +1818,10 @@ def str_eval(field_name, kwargs):
field_name = field_name.strip('`') field_name = field_name.strip('`')
allowed_names = { allowed_names = {
'bool': bool, 'bool': bool,
'divmod': divmod, 'divmod': helpers.helper_divmod,
'float': float, 'float': helpers.cast_to_float,
'int': int, 'int': helpers.cast_to_int,
'round': round, 'round': helpers.helper_round,
'str': str 'str': str
} }
allowed_names.update(kwargs) allowed_names.update(kwargs)