diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index 2d1bb89d23a..4d5df8785e2 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -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__ diff --git a/homeassistant/helpers/deprecation.py b/homeassistant/helpers/deprecation.py index 881534b5bed..2a4fafde75b 100644 --- a/homeassistant/helpers/deprecation.py +++ b/homeassistant/helpers/deprecation.py @@ -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 " diff --git a/homeassistant/util/logging.py b/homeassistant/util/logging.py index 99e606d2866..de04f23d9dd 100644 --- a/homeassistant/util/logging.py +++ b/homeassistant/util/logging.py @@ -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) diff --git a/tests/helpers/test_config_validation.py b/tests/helpers/test_config_validation.py index e09f8cf57aa..1f5d6ddfc40 100644 --- a/tests/helpers/test_config_validation.py +++ b/tests/helpers/test_config_validation.py @@ -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" diff --git a/tests/util/test_logging.py b/tests/util/test_logging.py index 414b246466c..d5f8eb4a2c7 100644 --- a/tests/util/test_logging.py +++ b/tests/util/test_logging.py @@ -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