dgui: Make copy of mutable default value

Fixes #1587
This commit is contained in:
rdb 2024-01-23 14:51:44 +01:00
parent 16afdd2bf9
commit 8c74919a8b
2 changed files with 21 additions and 1 deletions

View File

@ -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

View File

@ -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()