python: If a SimObject already has a parent, don't add it as a child.

The SimObject will have already been added to the other object's list
of children, and if it's given a new parent, it will be added as a
child to the new parent as well. The object will then be the child of
two other SimObjects which will cause a delightful variety of hard to
debug problems.

Another slightly better way to handle this situation would be to both
move to the new parent and also remove the SimObject from the original
parent's list of children. Unfortunately there isn't a simple way to
figure out what the parent called the child, and so we would have to
look through its children one by one until we found the right one.

Change-Id: I8f43dfab7adf58a43f806390a0c7c35a2efde11a
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/43905
Reviewed-by: Daniel Carvalho <odanrc@yahoo.com.br>
Reviewed-by: Jason Lowe-Power <power.jg@gmail.com>
Maintainer: Gabe Black <gabe.black@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Gabe Black
2021-03-30 06:22:11 -07:00
parent 1525ee8cdd
commit 6334ef4701

View File

@@ -1478,8 +1478,9 @@ class SimObject(object, metaclass=MetaSimObject):
child = coerceSimObjectOrVector(child)
if child.has_parent():
warn(f"{self}.{name} already has parent (Previously declared as "
f"{child._parent}.{name}).\n"
f"{child._parent}.{name}), not resetting parent.\n"
f"\tNote: {name} is not a parameter of {type(self).__name__}")
return
if name in self._children:
# This code path had an undiscovered bug that would make it fail
# at runtime. It had been here for a long time and was only