[lxc-devel] [PATCH 2/3] python: Use builtin len() function for network interfaces

Stéphane Graber stgraber at ubuntu.com
Mon Nov 26 16:50:01 UTC 2012


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 at ubuntu.com>
---
 src/python-lxc/lxc/__init__.py | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/src/python-lxc/lxc/__init__.py b/src/python-lxc/lxc/__init__.py
index c572e10..78852ec 100644
--- a/src/python-lxc/lxc/__init__.py
+++ b/src/python-lxc/lxc/__init__.py
@@ -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")
 
-- 
1.8.0





More information about the lxc-devel mailing list