Adds guards for missing information in call stack frames (#27217)

This commit is contained in:
Franck Nijhof 2019-10-05 11:59:34 +02:00 committed by Pascal Vizeli
parent 71a3516053
commit 2e17ad86af
5 changed files with 34 additions and 12 deletions

View File

@ -600,7 +600,8 @@ def deprecated(
if module is not None:
module_name = module.__name__
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
module_name = __name__

View File

@ -54,7 +54,15 @@ def get_deprecated(
and a warning is issued to the user.
"""
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.warning(
"'%s' is deprecated. Please rename '%s' to '%s' in your "

View File

@ -130,7 +130,15 @@ def catch_log_exception(
"""Decorate a callback to catch and log exceptions."""
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
frames = len(inspect.trace()) - 1
exc_msg = traceback.format_exc(-frames)
@ -178,9 +186,15 @@ def catch_log_coro_exception(
try:
return await target
except Exception: # pylint: disable=broad-except
module_name = inspect.getmodule( # type: ignore
inspect.trace()[1][0]
).__name__
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 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
frames = len(inspect.trace()) - 1
exc_msg = traceback.format_exc(-frames)

View File

@ -494,7 +494,10 @@ def test_deprecated_with_no_optionals(caplog, schema):
test_data = {"mars": True}
output = deprecated_schema(test_data.copy())
assert len(caplog.records) == 1
assert caplog.records[0].name == __name__
assert caplog.records[0].name in [
__name__,
"homeassistant.helpers.config_validation",
]
assert (
"The 'mars' option (with value 'True') is deprecated, "
"please remove it from your configuration"

View File

@ -72,12 +72,8 @@ async def test_async_create_catching_coro(hass, caplog):
async def job():
raise Exception("This is a bad coroutine")
pass
hass.async_create_task(logging_util.async_create_catching_coro(job()))
await hass.async_block_till_done()
assert "This is a bad coroutine" in caplog.text
assert (
"hass.async_create_task("
"logging_util.async_create_catching_coro(job()))" in caplog.text
)
assert "in test_async_create_catching_coro" in caplog.text