From 5df84775365bdcc671fb510dba7875dc6b611ccf Mon Sep 17 00:00:00 2001 From: Johann Kellerman Date: Mon, 24 Oct 2016 03:55:06 +0200 Subject: [PATCH] Catch UnicodeDecodeError Error (#4007) * Catch UnicodeDecodeError Error Error for #3933 * Forgot (exc) * catch... * Tests by @lwis * Docstring * Create open --- homeassistant/util/yaml.py | 3 +++ tests/util/test_yaml.py | 11 +++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/homeassistant/util/yaml.py b/homeassistant/util/yaml.py index c831a9e9784..3ee47e76cf2 100644 --- a/homeassistant/util/yaml.py +++ b/homeassistant/util/yaml.py @@ -43,6 +43,9 @@ def load_yaml(fname: str) -> Union[List, Dict]: except yaml.YAMLError as exc: _LOGGER.error(exc) raise HomeAssistantError(exc) + except UnicodeDecodeError as exc: + _LOGGER.error('Unable to read file %s: %s', fname, exc) + raise HomeAssistantError(exc) def clear_secret_cache() -> None: diff --git a/tests/util/test_yaml.py b/tests/util/test_yaml.py index 5b68042a1ff..9ead3c858a5 100644 --- a/tests/util/test_yaml.py +++ b/tests/util/test_yaml.py @@ -95,7 +95,6 @@ class TestYaml(unittest.TestCase): mock_walk.return_value = [ ['/tmp', ['tmp2', '.ignore', 'ignore'], ['zero.yaml']], ['/tmp/tmp2', [], ['one.yaml', 'two.yaml']], - ['/tmp/.ignore', [], []], ['/tmp/ignore', [], ['.ignore.yaml']] ] @@ -136,7 +135,6 @@ class TestYaml(unittest.TestCase): mock_walk.return_value = [ ['/tmp', ['tmp2', '.ignore', 'ignore'], ['first.yaml']], ['/tmp/tmp2', [], ['second.yaml', 'third.yaml']], - ['/tmp/.ignore', [], []], ['/tmp/ignore', [], ['.ignore.yaml']] ] @@ -175,7 +173,6 @@ class TestYaml(unittest.TestCase): mock_walk.return_value = [ ['/tmp', ['tmp2', '.ignore', 'ignore'], ['first.yaml']], ['/tmp/tmp2', [], ['second.yaml', 'third.yaml']], - ['/tmp/.ignore', [], []], ['/tmp/ignore', [], ['.ignore.yaml']] ] @@ -218,7 +215,6 @@ class TestYaml(unittest.TestCase): mock_walk.return_value = [ ['/tmp', ['tmp2', '.ignore', 'ignore'], ['first.yaml']], ['/tmp/tmp2', [], ['second.yaml', 'third.yaml']], - ['/tmp/.ignore', [], []], ['/tmp/ignore', [], ['.ignore.yaml']] ] @@ -241,6 +237,13 @@ class TestYaml(unittest.TestCase): "key4": "four" } + @patch('homeassistant.util.yaml.open', create=True) + def test_load_yaml_encoding_error(self, mock_open): + """Test raising a UnicodeDecodeError.""" + mock_open.side_effect = UnicodeDecodeError('', b'', 1, 0, '') + self.assertRaises(HomeAssistantError, yaml.load_yaml, 'test') + + FILES = {}