mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Adds guards for missing information in call stack frames (#27217)
This commit is contained in:
parent
71a3516053
commit
2e17ad86af
@ -600,7 +600,8 @@ def deprecated(
|
|||||||
if module is not None:
|
if module is not None:
|
||||||
module_name = module.__name__
|
module_name = module.__name__
|
||||||
else:
|
else:
|
||||||
# Unclear when it is None, but it happens, so let's guard.
|
# If Python is unable to access the sources files, the call stack frame
|
||||||
|
# will be missing information, so let's guard.
|
||||||
# https://github.com/home-assistant/home-assistant/issues/24982
|
# https://github.com/home-assistant/home-assistant/issues/24982
|
||||||
module_name = __name__
|
module_name = __name__
|
||||||
|
|
||||||
|
@ -54,7 +54,15 @@ def get_deprecated(
|
|||||||
and a warning is issued to the user.
|
and a warning is issued to the user.
|
||||||
"""
|
"""
|
||||||
if old_name in config:
|
if old_name in config:
|
||||||
module_name = inspect.getmodule(inspect.stack()[1][0]).__name__ # type: ignore
|
module = inspect.getmodule(inspect.stack()[1][0])
|
||||||
|
if module is not None:
|
||||||
|
module_name = module.__name__
|
||||||
|
else:
|
||||||
|
# If Python is unable to access the sources files, the call stack frame
|
||||||
|
# will be missing information, so let's guard.
|
||||||
|
# https://github.com/home-assistant/home-assistant/issues/24982
|
||||||
|
module_name = __name__
|
||||||
|
|
||||||
logger = logging.getLogger(module_name)
|
logger = logging.getLogger(module_name)
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"'%s' is deprecated. Please rename '%s' to '%s' in your "
|
"'%s' is deprecated. Please rename '%s' to '%s' in your "
|
||||||
|
@ -130,7 +130,15 @@ def catch_log_exception(
|
|||||||
"""Decorate a callback to catch and log exceptions."""
|
"""Decorate a callback to catch and log exceptions."""
|
||||||
|
|
||||||
def log_exception(*args: Any) -> None:
|
def log_exception(*args: Any) -> None:
|
||||||
module_name = inspect.getmodule(inspect.trace()[1][0]).__name__ # type: ignore
|
module = inspect.getmodule(inspect.stack()[1][0])
|
||||||
|
if module is not None:
|
||||||
|
module_name = module.__name__
|
||||||
|
else:
|
||||||
|
# If Python is unable to access the sources files, the call stack frame
|
||||||
|
# will be missing information, so let's guard.
|
||||||
|
# https://github.com/home-assistant/home-assistant/issues/24982
|
||||||
|
module_name = __name__
|
||||||
|
|
||||||
# Do not print the wrapper in the traceback
|
# Do not print the wrapper in the traceback
|
||||||
frames = len(inspect.trace()) - 1
|
frames = len(inspect.trace()) - 1
|
||||||
exc_msg = traceback.format_exc(-frames)
|
exc_msg = traceback.format_exc(-frames)
|
||||||
@ -178,9 +186,15 @@ def catch_log_coro_exception(
|
|||||||
try:
|
try:
|
||||||
return await target
|
return await target
|
||||||
except Exception: # pylint: disable=broad-except
|
except Exception: # pylint: disable=broad-except
|
||||||
module_name = inspect.getmodule( # type: ignore
|
module = inspect.getmodule(inspect.stack()[1][0])
|
||||||
inspect.trace()[1][0]
|
if module is not None:
|
||||||
).__name__
|
module_name = module.__name__
|
||||||
|
else:
|
||||||
|
# If Python is unable to access the sources files, the frame
|
||||||
|
# will be missing information, so let's guard.
|
||||||
|
# https://github.com/home-assistant/home-assistant/issues/24982
|
||||||
|
module_name = __name__
|
||||||
|
|
||||||
# Do not print the wrapper in the traceback
|
# Do not print the wrapper in the traceback
|
||||||
frames = len(inspect.trace()) - 1
|
frames = len(inspect.trace()) - 1
|
||||||
exc_msg = traceback.format_exc(-frames)
|
exc_msg = traceback.format_exc(-frames)
|
||||||
|
@ -494,7 +494,10 @@ def test_deprecated_with_no_optionals(caplog, schema):
|
|||||||
test_data = {"mars": True}
|
test_data = {"mars": True}
|
||||||
output = deprecated_schema(test_data.copy())
|
output = deprecated_schema(test_data.copy())
|
||||||
assert len(caplog.records) == 1
|
assert len(caplog.records) == 1
|
||||||
assert caplog.records[0].name == __name__
|
assert caplog.records[0].name in [
|
||||||
|
__name__,
|
||||||
|
"homeassistant.helpers.config_validation",
|
||||||
|
]
|
||||||
assert (
|
assert (
|
||||||
"The 'mars' option (with value 'True') is deprecated, "
|
"The 'mars' option (with value 'True') is deprecated, "
|
||||||
"please remove it from your configuration"
|
"please remove it from your configuration"
|
||||||
|
@ -72,12 +72,8 @@ async def test_async_create_catching_coro(hass, caplog):
|
|||||||
|
|
||||||
async def job():
|
async def job():
|
||||||
raise Exception("This is a bad coroutine")
|
raise Exception("This is a bad coroutine")
|
||||||
pass
|
|
||||||
|
|
||||||
hass.async_create_task(logging_util.async_create_catching_coro(job()))
|
hass.async_create_task(logging_util.async_create_catching_coro(job()))
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert "This is a bad coroutine" in caplog.text
|
assert "This is a bad coroutine" in caplog.text
|
||||||
assert (
|
assert "in test_async_create_catching_coro" in caplog.text
|
||||||
"hass.async_create_task("
|
|
||||||
"logging_util.async_create_catching_coro(job()))" in caplog.text
|
|
||||||
)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user