[FIX] tools.safe_eval: we want to see the expr which fail to eval(), so report it in the exception.
bzr revid: vmt@openerp.com-20130225163525-3g9qw1g3hkyyxf1j
This commit is contained in:
parent
0de24bf983
commit
c62780673c
|
@ -35,6 +35,8 @@ from opcode import HAVE_ARGUMENT, opmap, opname
|
||||||
from types import CodeType
|
from types import CodeType
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from .misc import ustr
|
||||||
|
|
||||||
__all__ = ['test_expr', 'safe_eval', 'const_eval']
|
__all__ = ['test_expr', 'safe_eval', 'const_eval']
|
||||||
|
|
||||||
# The time module is usually already provided in the safe_eval environment
|
# The time module is usually already provided in the safe_eval environment
|
||||||
|
@ -110,7 +112,7 @@ def test_expr(expr, allowed_codes, mode="eval"):
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
import sys
|
import sys
|
||||||
exc_info = sys.exc_info()
|
exc_info = sys.exc_info()
|
||||||
raise ValueError, '"%s" while compiling\n%s' % (str(e), expr), exc_info[2]
|
raise ValueError, '"%s" while compiling\n%r' % (ustr(e), expr), exc_info[2]
|
||||||
for code in _get_opcodes(code_obj):
|
for code in _get_opcodes(code_obj):
|
||||||
if code not in allowed_codes:
|
if code not in allowed_codes:
|
||||||
raise ValueError("opcode %s not allowed (%r)" % (opname[code], expr))
|
raise ValueError("opcode %s not allowed (%r)" % (opname[code], expr))
|
||||||
|
@ -238,6 +240,11 @@ def safe_eval(expr, globals_dict=None, locals_dict=None, mode="eval", nocopy=Fal
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
c = test_expr(expr, _SAFE_OPCODES, mode=mode)
|
c = test_expr(expr, _SAFE_OPCODES, mode=mode)
|
||||||
return eval(c, globals_dict, locals_dict)
|
try:
|
||||||
|
return eval(c, globals_dict, locals_dict)
|
||||||
|
except Exception, e:
|
||||||
|
import sys
|
||||||
|
exc_info = sys.exc_info()
|
||||||
|
raise ValueError, '"%s" while evaluating\n%r' % (ustr(e), expr), exc_info[2]
|
||||||
|
|
||||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||||
|
|
Loading…
Reference in New Issue