summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorhut <hut@lepus.uberspace.de>2016-08-21 23:29:09 +0200
committerhut <hut@lepus.uberspace.de>2016-08-21 23:29:09 +0200
commit8132885c2fbddc9e686f2285b307e28a1f29f2a7 (patch)
treeb2be10f89df91117aa34412dde10dc960e29315c /ranger
parent40d6cf586b0c634c542f9808247aa90817ca3786 (diff)
downloadranger-8132885c2fbddc9e686f2285b307e28a1f29f2a7.tar.gz
gui.displayable: more safe DisplayableContainer.replace_child
Diffstat (limited to 'ranger')
-rw-r--r--ranger/gui/displayable.py16
1 files changed, 13 insertions, 3 deletions
diff --git a/ranger/gui/displayable.py b/ranger/gui/displayable.py
index 62eb5300..4a5bcde6 100644
--- a/ranger/gui/displayable.py
+++ b/ranger/gui/displayable.py
@@ -292,9 +292,19 @@ class DisplayableContainer(Displayable):
         obj.parent = self
 
     def replace_child(self, old_obj, new_obj):
-        """Replace the old object with the new instance in the container."""
-        self.container[self.container.index(old_obj)] = new_obj
-        new_obj.parent = self
+        """
+        Replace the old object with the new instance in the container.
+
+        The new object will have the same position in the list as old_obj.
+        If old_obj is not in the list, new_obj will simply be appended.
+        """
+        try:
+            index = self.container.index(old_obj)
+        except ValueError:
+            self.add_child(new_obj)
+        else:
+            self.container[index] = new_obj
+            new_obj.parent = self
 
     def remove_child(self, obj):
         """Remove the object from the container."""