]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
python: Use builtin len() function for network interfaces
authorStéphane Graber <stgraber@ubuntu.com>
Thu, 22 Nov 2012 23:01:40 +0000 (18:01 -0500)
committerStéphane Graber <stgraber@ubuntu.com>
Mon, 26 Nov 2012 17:32:50 +0000 (12:32 -0500)
Use our own len() function for network interfaces as doing
len(container.get_config_item("lxc.network")) will fail when the
list is empty.

Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
src/python-lxc/lxc/__init__.py

index c572e10cc87ddaa5589d6292477812fb320a389b..78852ec0778163d34a2b1e2e0f05f26998f0fd0b 100644 (file)
@@ -115,23 +115,27 @@ class ContainerNetworkList():
         self.container = container
 
     def __getitem__(self, index):
-        count = len(self.container.get_config_item("lxc.network"))
-        if index >= count:
+        if index >= len(self):
             raise IndexError("list index out of range")
 
         return ContainerNetwork(self.container, index)
 
     def __len__(self):
-        return len(self.container.get_config_item("lxc.network"))
+        values = self.container.get_config_item("lxc.network")
+
+        if values:
+            return len(values)
+        else:
+            return 0
 
     def add(self, network_type):
-        index = len(self.container.get_config_item("lxc.network"))
+        index = len(self)
 
         return self.container.set_config_item("lxc.network.%s.type" % index,
                                               network_type)
 
     def remove(self, index):
-        count = len(self.container.get_config_item("lxc.network"))
+        count = len(self)
         if index >= count:
             raise IndexError("list index out of range")