From: Patrick Donnelly Date: Tue, 28 Jun 2022 20:27:14 +0000 (-0400) Subject: teuthology/misc: avoid copying references of b when list or dict X-Git-Tag: 1.2.0~154^2~4 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=c546d8dfa822b939ba3da3e529da69e805e28c51;p=teuthology.git 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 --- 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):