From 7cf76b03d88efcbdba39e1ebd92315a30bd5f1d4 Mon Sep 17 00:00:00 2001 From: Laurent Monin Date: Wed, 16 Jun 2021 10:51:17 +0200 Subject: [PATCH] Make duplicated option declaration logging more reliable Ensure logging is working, even if stack cannot be retrieved or has no code context --- picard/config.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/picard/config.py b/picard/config.py index 8b66bd472..983e6a841 100644 --- a/picard/config.py +++ b/picard/config.py @@ -320,10 +320,17 @@ class Option(QtCore.QObject): def __init__(self, section, name, default): key = (section, name) if key in self.registry: - f = inspect.stack()[1] - log.error("Option %s/%s already declared\nat %s:%d: in %s\n%s" - % (section, name, f.filename, f.lineno, f.function, - "\n".join(f.code_context).rstrip())) + stack = inspect.stack() + fmt = "Option %s/%s already declared" + args = [section, name] + if len(stack) > 1: + f = stack[1] + fmt += "\nat %s:%d: in %s" + args.extend((f.filename, f.lineno, f.function)) + if f.code_context: + fmt += "\n%s" + args.append("\n".join(f.code_context).rstrip()) + log.error(fmt, *args) super().__init__() self.section = section self.name = name