-
Notifications
You must be signed in to change notification settings - Fork 68
[IMP] adapt code to new simplified safe_eval
API
#265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -495,11 +495,11 @@ def preprocess(klass, expr): | |
Example: [('company_id', 'in', [*company_ids, False]) | ||
|
||
Returns a pair with the new expression and an evaluation context that should | ||
be used in `safe_eval`. | ||
be used in :func:`~odoo.upgrade.util.misc.safe_eval`. | ||
|
||
``` | ||
>>> prepared_domain, context = util.SelfPrintEvalContext.preprocess(domain) | ||
>>> safe_eval(prepared_domain, context, nocopy=True) | ||
>>> safe_eval(prepared_domain, context) | ||
``` | ||
|
||
:meta private: exclude from online docs | ||
|
@@ -533,6 +533,40 @@ def visit_UnaryOp(self, node): | |
return (ast_unparse(visited).strip(), SelfPrintEvalContext(replacer.replaces)) | ||
|
||
|
||
if version_gte("saas~18.4"): | ||
import odoo.tools.safe_eval as _safe_eval_mod | ||
|
||
def safe_eval(expr, context=None): | ||
if context is None: | ||
context = SelfPrintEvalContext() | ||
|
||
assert isinstance(expr, (str, bytes)) | ||
assert isinstance(context, SelfPrintEvalContext) | ||
|
||
c = _safe_eval_mod.test_expr(expr, _safe_eval_mod._SAFE_OPCODES, mode="eval", filename=None) | ||
context["__builtins__"] = dict(_safe_eval_mod._BUILTINS) | ||
try: | ||
return _safe_eval_mod.unsafe_eval(c, context, None) | ||
except _safe_eval_mod._BUBBLEUP_EXCEPTIONS: | ||
raise | ||
except Exception as e: | ||
raise ValueError("{!r} while evaluating\n{!r}".format(e, expr)) | ||
finally: | ||
del context["__builtins__"] | ||
else: | ||
try: | ||
from odoo.tools.safe_eval import safe_eval as _safe_eval_func | ||
except ImportError: | ||
from openerp.tools.safe_eval import safe_eval as _safe_eval_func | ||
|
||
def safe_eval(expr, context=None): | ||
if context is None: | ||
context = SelfPrintEvalContext() | ||
assert isinstance(context, SelfPrintEvalContext) | ||
|
||
return _safe_eval_func(expr, context, nocopy=True) | ||
Comment on lines
+536
to
+567
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't we assert in both cases that expr is str ot bytes? If we can assert it in both then I'd suggest we move the shared part to a toplevel if version_gte("saas~18.4"):
import odoo.tools.safe_eval as _safe_eval_mod
def _safe_eval_impl(expr, context):
c = _safe_eval_mod.test_expr(expr, _safe_eval_mod._SAFE_OPCODES, mode="eval", filename=None)
context["__builtins__"] = dict(_safe_eval_mod._BUILTINS)
try:
return _safe_eval_mod.unsafe_eval(c, context, None)
except _safe_eval_mod._BUBBLEUP_EXCEPTIONS:
raise
except Exception as e:
raise ValueError("{!r} while evaluating\n{!r}".format(e, expr))
finally:
del context["__builtins__"]
else:
try:
from odoo.tools.safe_eval import safe_eval as _safe_eval_func
except ImportError:
from openerp.tools.safe_eval import safe_eval as _safe_eval_func
def _safe_eval_impl(expr, context):
return _safe_eval_func(expr, context, nocopy=True)
def safe_eval(expr, context=None):
if context is None:
context = SelfPrintEvalContext()
assert isinstance(expr, (str, bytes))
assert isinstance(context, SelfPrintEvalContext)
return _safe_eval_impl(expr, context) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually compile already checks the type of the source here. It's can either be a string, bytesarray or AST. We already check that the source object is not a codeobject, so I think the |
||
|
||
|
||
class _Replacer(ast.NodeTransformer): | ||
"""Replace literal nodes in an AST.""" | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential change of behaviour, LGTM just flagging it it new errors start appearing after the merge.