1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Adjust importlib helper to avoid leaking memory on re-raise (#115377)

This commit is contained in:
J. Nick Koston
2024-04-10 15:19:17 -10:00
committed by GitHub
parent 6bd6adc4f5
commit f0c8c2a684
2 changed files with 32 additions and 9 deletions

View File

@@ -41,16 +41,40 @@ async def test_async_import_module_failures(hass: HomeAssistant) -> None:
with (
patch(
"homeassistant.helpers.importlib.importlib.import_module",
side_effect=ImportError,
side_effect=ValueError,
),
pytest.raises(ImportError),
pytest.raises(ValueError),
):
await importlib.async_import_module(hass, "test.module")
mock_module = MockModule()
# The failure should be not be cached
with (
patch(
"homeassistant.helpers.importlib.importlib.import_module",
return_value=mock_module,
),
):
assert await importlib.async_import_module(hass, "test.module") is mock_module
async def test_async_import_module_failure_caches_module_not_found(
hass: HomeAssistant,
) -> None:
"""Test importing a module caches ModuleNotFound."""
with (
patch(
"homeassistant.helpers.importlib.importlib.import_module",
side_effect=ModuleNotFoundError,
),
pytest.raises(ModuleNotFoundError),
):
await importlib.async_import_module(hass, "test.module")
mock_module = MockModule()
# The failure should be cached
with (
pytest.raises(ImportError),
pytest.raises(ModuleNotFoundError),
patch(
"homeassistant.helpers.importlib.importlib.import_module",
return_value=mock_module,