server: Rework methods to avoid mutable default arguments
Mutable objects as default arguments in function or method declarations
are only assigned once over the lifetime of the function declaration
(i.e. are not assigned per function/method *invocation*). Rework such
declarations to assign None (which is immutable), and test for None in
the function/method body to assign a per-instance object.
The side-effects of mutable default arguments are explored here:
http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments
Change-Id: Id963cdecba63f7072e621208e1a3173e1646ddf1
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/obmc/mapper/server.py b/obmc/mapper/server.py
index 1d13c5a..24e5bbb 100644
--- a/obmc/mapper/server.py
+++ b/obmc/mapper/server.py
@@ -483,7 +483,10 @@
subtree_match=self.path_match,
iface_match=self.interface_match)
- def discover(self, owners=[]):
+ def discover(self, owners=None):
+ if owners is None:
+ owners = []
+
def get_owner(name):
try:
return (name, self.bus.get_name_owner(name))
@@ -524,7 +527,9 @@
return [str(x) for x in interfaces if self.interface_match(x)]
@staticmethod
- def interfaces_get(item, owner, default=[]):
+ def interfaces_get(item, owner, default=None):
+ if default is None:
+ default = []
return item.get(owner, default)
@staticmethod