]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
teuthology/misc: avoid copying references of b when list or dict
authorPatrick Donnelly <pdonnell@redhat.com>
Tue, 28 Jun 2022 20:27:14 +0000 (16:27 -0400)
committerPatrick Donnelly <pdonnell@redhat.com>
Mon, 18 Jul 2022 14:10:30 +0000 (10:10 -0400)
Otherwise, if b persists in memory (in a cache), it may wrongly
accumulate future merges.

Signed-off-by: Patrick Donnelly <pdonnell@redhat.com>
teuthology/misc.py

index 6a6651e7222403e357ea127e991b1d675172ffc6..85285cc783711c4a17ec15cd68725fb754bd712d 100644 (file)
@@ -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):