Don't use the locals parameter on exec. (#29979)
Using the locals parameter makes it so that the code of a Python script runs as if it were in the body of a ``class``. One effect of this is that functions defined as part of a script cannot call one another directly. Fixes: #24704, #13653
This commit is contained in:
parent
b058742404
commit
33cbb398ad
2 changed files with 28 additions and 3 deletions
|
@ -175,6 +175,7 @@ def execute(hass, filename, source, data=None):
|
|||
builtins["sorted"] = sorted
|
||||
builtins["time"] = TimeWrapper()
|
||||
builtins["dt_util"] = dt_util
|
||||
logger = logging.getLogger(f"{__name__}.{filename}")
|
||||
restricted_globals = {
|
||||
"__builtins__": builtins,
|
||||
"_print_": StubPrinter,
|
||||
|
@ -184,14 +185,15 @@ def execute(hass, filename, source, data=None):
|
|||
"_getitem_": default_guarded_getitem,
|
||||
"_iter_unpack_sequence_": guarded_iter_unpack_sequence,
|
||||
"_unpack_sequence_": guarded_unpack_sequence,
|
||||
"hass": hass,
|
||||
"data": data or {},
|
||||
"logger": logger,
|
||||
}
|
||||
logger = logging.getLogger(f"{__name__}.{filename}")
|
||||
local = {"hass": hass, "data": data or {}, "logger": logger}
|
||||
|
||||
try:
|
||||
_LOGGER.info("Executing %s: %s", filename, data)
|
||||
# pylint: disable=exec-used
|
||||
exec(compiled.code, restricted_globals, local)
|
||||
exec(compiled.code, restricted_globals)
|
||||
except ScriptError as err:
|
||||
logger.error("Error executing script: %s", err)
|
||||
except Exception as err: # pylint: disable=broad-except
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue