From c546d8dfa822b939ba3da3e529da69e805e28c51 Mon Sep 17 00:00:00 2001 From: Patrick Donnelly Date: Tue, 28 Jun 2022 16:27:14 -0400 Subject: [PATCH] teuthology/misc: avoid copying references of b when list or dict Otherwise, if b persists in memory (in a cache), it may wrongly accumulate future merges. Signed-off-by: Patrick Donnelly --- teuthology/misc.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/teuthology/misc.py b/teuthology/misc.py index 6a6651e72..85285cc78 100644 --- a/teuthology/misc.py +++ b/teuthology/misc.py @@ -1003,23 +1003,21 @@ def deep_merge(a, b): :param a: object items will be merged into :param b: object items will be merged from """ - if a is None: - return b if b is None: return a - if isinstance(a, list): + elif isinstance(a, list): assert isinstance(b, list) a.extend(b) return a - if isinstance(a, dict): + elif isinstance(a, dict): assert isinstance(b, dict) for (k, v) in b.items(): - if k in a: - a[k] = deep_merge(a[k], v) - else: - a[k] = v + a[k] = deep_merge(a.get(k), v) return a - return b + elif isinstance(b, dict) or isinstance(b, list): + return deep_merge(b.__class__(), b) + else: + return b def get_valgrind_args(testdir, name, preamble, v, exit_on_first_error=True): -- 2.47.3