diff mbox series

[ovs-dev,v2,1/1] utilities: Update gdb script so it works with all python versions

Message ID aa67c96ecb777b11daad7dad237f799a99331670.1546600955.git.echaudro@redhat.com
State Superseded
Headers show
Series [ovs-dev,v2,1/1] utilities: Update gdb script so it works with all python versions | expand

Commit Message

Eelco Chaudron Jan. 4, 2019, 11:26 a.m. UTC
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 <echaudro@redhat.com>
---

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(-)

Comments

solomon Jan. 6, 2019, 8:36 a.m. UTC | #1
Eelco Chaudron wrote:
> 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 <echaudro@redhat.com>

Hi Eeclo:

Test your new patch with all commands, and another two error messages
are reported as following:
(gdb) ovs_dump_netdev
Traceback (most recent call last):
  File "/root/solomon/ovs/utilities/gdb/ovs_gdb.py", line 640, in invoke
    self.display_single_netdev(netdev)
  File "/root/solomon/ovs/utilities/gdb/ovs_gdb.py", line 632, in display_single_netdev
    netdev['auto_classified'], netdev['netdev_class']))
TypeError: unsupported format string passed to gdb.Value.__format__
Error occurred in Python command: unsupported format string passed to gdb.Value.__format__
(gdb)

(gdb) ovs_show_fdb
Traceback (most recent call last):
  File "/root/solomon/ovs/utilities/gdb/ovs_gdb.py", line 1106, in invoke
    for name in sorted(all_name.iterkeys()):
AttributeError: 'dict' object has no attribute 'iterkeys'
Error occurred in Python command: 'dict' object has no attribute 'iterkeys'
(gdb)



> ---
> 
> 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
>
Eelco Chaudron Jan. 7, 2019, 2:48 p.m. UTC | #2
Thanks for reporting this. I sent out a v3, and also tested all commands 
on Python2 and 3. We should be good to go now ;)

//Eelco


On 6 Jan 2019, at 9:36, solomon wrote:

> Eelco Chaudron wrote:
>> 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 <echaudro@redhat.com>
>
> Hi Eeclo:
>
> Test your new patch with all commands, and another two error messages
> are reported as following:
> (gdb) ovs_dump_netdev
> Traceback (most recent call last):
>   File "/root/solomon/ovs/utilities/gdb/ovs_gdb.py", line 640, in 
> invoke
>     self.display_single_netdev(netdev)
>   File "/root/solomon/ovs/utilities/gdb/ovs_gdb.py", line 632, in 
> display_single_netdev
>     netdev['auto_classified'], netdev['netdev_class']))
> TypeError: unsupported format string passed to gdb.Value.__format__
> Error occurred in Python command: unsupported format string passed to 
> gdb.Value.__format__
> (gdb)
>
> (gdb) ovs_show_fdb
> Traceback (most recent call last):
>   File "/root/solomon/ovs/utilities/gdb/ovs_gdb.py", line 1106, in 
> invoke
>     for name in sorted(all_name.iterkeys()):
> AttributeError: 'dict' object has no attribute 'iterkeys'
> Error occurred in Python command: 'dict' object has no attribute 
> 'iterkeys'
> (gdb)
>
>
>
>> ---
>>
>> 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
>>
>
> -- 
>
> Thanks
> Solomon
diff mbox series

Patch

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