mirror of
https://github.com/Faraphel/Atlas-Install.git
synced 2025-07-02 02:38:30 +02:00
safe_eval now show the template in the error traceback
This commit is contained in:
parent
8e801329e0
commit
622f43c66e
3 changed files with 21 additions and 6 deletions
|
@ -1,2 +1,15 @@
|
|||
from source.safe_eval.safe_eval import safe_eval
|
||||
from source.safe_eval.multiple_safe_eval import multiple_safe_eval
|
||||
from typing import Callable
|
||||
from source.translation import translate as _
|
||||
|
||||
|
||||
class BetterSafeEvalError(Exception):
|
||||
def __init__(self, template: str):
|
||||
super().__init__(_("SAFE_EVAL_ERROR", " (", "TEMPLATE_USED", " : ", repr(template), ")"))
|
||||
|
||||
|
||||
def better_safe_eval_error(func: Callable, template: str):
|
||||
def wrapped(*args, **kwargs):
|
||||
try: return func(*args, **kwargs)
|
||||
except Exception as exc: raise BetterSafeEvalError(template) from exc
|
||||
|
||||
return wrapped
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from typing import TYPE_CHECKING, Iterable, Callable
|
||||
|
||||
from source.safe_eval import safe_eval
|
||||
from source.safe_eval.safe_eval import safe_eval
|
||||
from source.safe_eval import better_safe_eval_error
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from source import TemplateMultipleSafeEval, TemplateSafeEval, Env
|
||||
|
@ -40,9 +41,9 @@ def multiple_safe_eval(template: "TemplateMultipleSafeEval", env: "Env" = None,
|
|||
))
|
||||
template = template[token_end:]
|
||||
|
||||
return lambda *args, **kwargs: "".join([
|
||||
return better_safe_eval_error(lambda *args, **kwargs: "".join([
|
||||
str(part(*args, **kwargs)) if callable(part) else part
|
||||
for part in lambda_templates
|
||||
])
|
||||
]), template=template)
|
||||
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ import ast
|
|||
import copy
|
||||
from typing import TYPE_CHECKING, Iterable, Callable
|
||||
|
||||
from source.safe_eval import better_safe_eval_error
|
||||
from source.safe_eval.macros import replace_macro
|
||||
from source.safe_eval.safe_function import get_all_safe_functions
|
||||
from source.translation import translate as _
|
||||
|
@ -126,7 +127,7 @@ def safe_eval(template: "TemplateSafeEval", env: "Env" = None, macros: dict[str,
|
|||
# return the evaluated formula
|
||||
lambda_template = eval(compile(expression, "<safe_eval>", "eval"), globals_, locals_)
|
||||
self.safe_eval_cache[template_key] = lambda_template # cache the callable for potential latter call
|
||||
return lambda_template
|
||||
return better_safe_eval_error(lambda_template, template=template)
|
||||
|
||||
|
||||
# TODO: disable some method and function call. for example, mod_config.path.unlink() is dangerous !
|
||||
|
|
Loading…
Reference in a new issue