[FIX] safe_eval: support for allowing auto-loading of python submodules, such as _strptime
bzr revid: odo@openerp.com-20101216183839-7mor9xfzbor2s9r7
This commit is contained in:
@@ -39,6 +39,8 @@ import os
|
||||
|
||||
__all__ = ['test_expr', 'literal_eval', 'safe_eval', 'const_eval', 'ext_eval' ]
|
||||
|
||||
_ALLOWED_MODULES = ['_strptime']
|
||||
|
||||
_CONST_OPCODES = set(opmap[x] for x in [
|
||||
'POP_TOP', 'ROT_TWO', 'ROT_THREE', 'ROT_FOUR', 'DUP_TOP','POP_BLOCK','SETUP_LOOP',
|
||||
'BUILD_LIST', 'BUILD_MAP', 'BUILD_TUPLE',
|
||||
@@ -207,7 +209,10 @@ except ImportError:
|
||||
node_or_string = node_or_string.body
|
||||
return _convert(node_or_string)
|
||||
|
||||
|
||||
def _import(name, globals={}, locals={}, fromlist=[], level=-1):
|
||||
if name in _ALLOWED_MODULES:
|
||||
return __import__(name, globals, locals, level)
|
||||
raise ImportError(name)
|
||||
|
||||
def safe_eval(expr, globals_dict=None, locals_dict=None, mode="eval", nocopy=False):
|
||||
"""safe_eval(expression[, globals[, locals[, mode[, nocopy]]]]) -> result
|
||||
@@ -253,6 +258,7 @@ def safe_eval(expr, globals_dict=None, locals_dict=None, mode="eval", nocopy=Fal
|
||||
|
||||
globals_dict.update(
|
||||
__builtins__ = {
|
||||
'__import__': _import,
|
||||
'True': True,
|
||||
'False': False,
|
||||
'None': None,
|
||||
|
||||
Reference in New Issue
Block a user