From 4c44940ec12a9178efc0cffcd5b0cc61bec3f39a Mon Sep 17 00:00:00 2001 From: Lauri Tirkkonen Date: Tue, 11 Jan 2022 20:22:19 +0200 Subject: [PATCH] config & rights: use open() for better error messages ConfigParser().read() doesn't differentiate between different types of failure to read files, causing eg. "No such file" to be logged in all cases, for example if permissions are insufficient. fix that by using open() and ConfigParser().read_file() instead. --- radicale/config.py | 15 ++++++++------- radicale/rights/from_file.py | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/radicale/config.py b/radicale/config.py index 0e119109..4b7ed8c4 100644 --- a/radicale/config.py +++ b/radicale/config.py @@ -276,17 +276,18 @@ def load(paths: Optional[Iterable[Tuple[str, bool]]] = None for path, ignore_if_missing in paths: parser = RawConfigParser() config_source = "config file %r" % path + config: types.CONFIG try: - if not parser.read(path): - config = Configuration.SOURCE_MISSING - if not ignore_if_missing: - raise RuntimeError("No such file: %r" % path) - else: + with open(path, "r") as f: + parser.read_file(f) config = {s: {o: parser[s][o] for o in parser.options(s)} for s in parser.sections()} except Exception as e: - raise RuntimeError("Failed to load %s: %s" % (config_source, e) - ) from e + if isinstance(e, FileNotFoundError) and ignore_if_missing: + config = Configuration.SOURCE_MISSING + else: + raise RuntimeError("Failed to load %s: %s" % (config_source, e) + ) from e configuration.update(config, config_source) return configuration diff --git a/radicale/rights/from_file.py b/radicale/rights/from_file.py index c7e9c0cd..01fa2fb7 100644 --- a/radicale/rights/from_file.py +++ b/radicale/rights/from_file.py @@ -56,8 +56,8 @@ class Rights(rights.BaseRights): escaped_user = re.escape(user) rights_config = configparser.ConfigParser() try: - if not rights_config.read(self._filename): - raise RuntimeError("No such file: %r" % self._filename) + with open(self._filename, "r") as f: + rights_config.read_file(f) except Exception as e: raise RuntimeError("Failed to load rights file %r: %s" % (self._filename, e)) from e