Browse Source

dgui: Make copy of mutable default value

Fixes #1587
rdb 1 year ago
parent
commit
8c74919a8b
2 changed files with 21 additions and 1 deletions
  1. 6 1
      direct/src/gui/DirectGuiBase.py
  2. 15 0
      tests/gui/test_DirectButton.py

+ 6 - 1
direct/src/gui/DirectGuiBase.py

@@ -224,7 +224,12 @@ class DirectGuiBase(DirectObject.DirectObject):
                         del keywords[name]
                     else:
                         # Use optionDefs value
-                        optionInfo[name] = [default, default, function]
+                        value = default
+                        if isinstance(value, list):
+                            value = list(value)
+                        elif isinstance(value, dict):
+                            value = dict(value)
+                        optionInfo[name] = [default, value, function]
                 elif optionInfo[name][FUNCTION] is None:
                     # Only override function if not defined by derived class
                     optionInfo[name][FUNCTION] = function

+ 15 - 0
tests/gui/test_DirectButton.py

@@ -1,6 +1,21 @@
 from direct.gui.DirectButton import DirectButton
 
 
+def test_button_default_extraArgs():
+    btn = DirectButton()
+
+    assert btn.configure('extraArgs') == ('extraArgs', [], [])
+    assert btn._optionInfo['extraArgs'] == [[], [], None]
+
+    # Changing will not affect default value
+    btn['extraArgs'].append(1)
+    assert btn.configure('extraArgs') == ('extraArgs', [], [1])
+
+    # Changing this does
+    btn.configure('extraArgs')[1].append(2)
+    assert btn.configure('extraArgs') == ('extraArgs', [2], [1])
+
+
 def test_button_destroy():
     btn = DirectButton(text="Test")
     btn.destroy()