Newer versions of Python require a different iterator function. This
change will make the iterator classes work with all Python versions.

In addition, it adds a fix for python3 as it does not support the
long() type. The fix guaranties the script still works on Python
2.7.

Signed-off-by: Eelco Chaudron <[email protected]>
---

v2: Adding fix for python3 compatibility as it does not support long()

 utilities/gdb/ovs_gdb.py | 49 ++++++++++++++++++++++++++++++++--------
 1 file changed, 39 insertions(+), 10 deletions(-)

diff --git a/utilities/gdb/ovs_gdb.py b/utilities/gdb/ovs_gdb.py
index cb9778c69..74663e577 100644
--- a/utilities/gdb/ovs_gdb.py
+++ b/utilities/gdb/ovs_gdb.py
@@ -56,6 +56,7 @@
 #    ...
 #
 import gdb
+import sys
 import uuid
 
 
@@ -65,6 +66,13 @@ import uuid
 N_UMAPS = 512
 
 
+#
+# For Python2-3 compatibility define long as int
+#
+if sys.version_info[0] == 3:
+    long = int
+
+
 #
 # The container_of code below is a copied from the Linux kernel project file,
 # scripts/gdb/linux/utils.py. It has the following copyright header:
@@ -192,7 +200,7 @@ class ForEachCMAP(object):
 
         raise StopIteration
 
-    def next(self):
+    def __next__(self):
         ipml = self.cmap['impl']['p']
         if ipml['n'] == 0:
             raise StopIteration
@@ -206,6 +214,9 @@ class ForEachCMAP(object):
                             gdb.lookup_type(self.typeobj).pointer(),
                             self.member)
 
+    def next(self):
+        return self.__next__()
+
 
 #
 # Class that will provide an iterator over an OVS hmap.
@@ -229,7 +240,7 @@ class ForEachHMAP(object):
 
         raise StopIteration
 
-    def next(self):
+    def __next__(self):
         #
         # In the real implementation the n values is never checked,
         # however when debugging we do, as we might try to access
@@ -253,6 +264,9 @@ class ForEachHMAP(object):
                             gdb.lookup_type(self.typeobj).pointer(),
                             self.member)
 
+    def next(self):
+        return self.__next__()
+
 
 #
 # Class that will provide an iterator over an Netlink attributes
@@ -268,7 +282,7 @@ class ForEachNL():
     def round_up(self, val, round_to):
         return int(val) + (round_to - int(val)) % round_to
 
-    def next(self):
+    def __next__(self):
         if self.attr is None or \
            self.attr_len < 4 or self.attr['nla_len'] < 4 or  \
            self.attr['nla_len'] > self.attr_len:
@@ -286,6 +300,9 @@ class ForEachNL():
 
         return attr
 
+    def next(self):
+        return self.__next__()
+
 
 #
 # Class that will provide an iterator over an OVS shash.
@@ -298,14 +315,17 @@ class ForEachSHASH(ForEachHMAP):
         super(ForEachSHASH, self).__init__(shash['map'],
                                            "struct shash_node", "node")
 
-    def next(self):
-        node = super(ForEachSHASH, self).next()
+    def __next__(self):
+        node = super(ForEachSHASH, self).__next__()
 
         if self.data_typeobj is None:
             return node
 
         return node['data'].cast(gdb.lookup_type(self.data_typeobj).pointer())
 
+    def next(self):
+        return self.__next__()
+
 
 #
 # Class that will provide an iterator over an OVS simap.
@@ -315,10 +335,13 @@ class ForEachSIMAP(ForEachHMAP):
         super(ForEachSIMAP, self).__init__(shash['map'],
                                            "struct simap_node", "node")
 
-    def next(self):
-        node = super(ForEachSIMAP, self).next()
+    def __next__(self):
+        node = super(ForEachSIMAP, self).__next__()
         return node['name'], node['data']
 
+    def next(self):
+        return self.__next__()
+
 
 #
 # Class that will provide an iterator over an OVS smap.
@@ -328,10 +351,13 @@ class ForEachSMAP(ForEachHMAP):
         super(ForEachSMAP, self).__init__(shash['map'],
                                           "struct smap_node", "node")
 
-    def next(self):
-        node = super(ForEachSMAP, self).next()
+    def __next__(self):
+        node = super(ForEachSMAP, self).__next__()
         return node['key'], node['value']
 
+    def next(self):
+        return self.__next__()
+
 
 #
 # Class that will provide an iterator over an OVS list.
@@ -346,7 +372,7 @@ class ForEachLIST():
     def __iter__(self):
         return self
 
-    def next(self):
+    def __next__(self):
         if self.list.address == self.node['next']:
             raise StopIteration
 
@@ -359,6 +385,9 @@ class ForEachLIST():
                             gdb.lookup_type(self.typeobj).pointer(),
                             self.member)
 
+    def next(self):
+        return self.__next__()
+
 
 #
 # Implements the GDB "ovs_dump_bridges" command
-- 
2.17.2

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to