From 2fc94fa6adb9a73e5078d1bedeb9f905f4953aa5 Mon Sep 17 00:00:00 2001 From: Taro Sato Date: Tue, 21 Apr 2020 22:56:37 -0700 Subject: [PATCH] Ensure reading from empty config file results in empty dict --- src/resconfig/io/paths.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/resconfig/io/paths.py b/src/resconfig/io/paths.py index 74f7714..eaf313c 100644 --- a/src/resconfig/io/paths.py +++ b/src/resconfig/io/paths.py @@ -1,3 +1,4 @@ +from logging import getLogger from pathlib import Path from ..typing import FilePath @@ -6,6 +7,8 @@ from . import toml from . import yaml +log = getLogger(__name__) + class ConfigPath(type(Path())): """Path for configuration file.""" @@ -18,7 +21,12 @@ def dump(self, content): def load(self): with open(self) as f: - return self.module.load(f) + try: + content = self.module.load(f) or {} + except Exception: + log.exception("Error occured loading from %s; assume empty...", self) + content = {} + return content @classmethod def from_extension(cls, filename: FilePath) -> "ConfigPath":