Skip to contentSkip to conversation

usertools: fix CPU layout with python 3

PATCH In review Thomas Monjalon 2 participants 3 messages Apr 30, 2017 – May 1, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usertools: fix CPU layout with python 3

These differences in Python 3 were causing errors:
- xrange is replaced by range
- dict values are a view (instead of list)
- has_key is removed

Fixes deb87e6777c0 ("usertools: use sysfs for CPU layout")
Fixes 63985c5f104e ("usertools: fix CPU layout for more than 2 threads")
---
 usertools/cpu_layout.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
usertools/cpu_layout.py +6 -2
index 99152a2ec..74da928df 100755
@@ -35,6 +35,10 @@
 #
 from __future__ import print_function
 import sys
+try:
+    xrange # Python 2
+except NameError:
+    xrange = range # Python 3
 
 sockets = []
 cores = []
@@ -72,7 +76,7 @@
 print("")
 
 max_processor_len = len(str(len(cores) * len(sockets) * 2 - 1))
-max_thread_count = len(core_map.values()[0])
+max_thread_count = len(core_map)
 max_core_map_len = (max_processor_len * max_thread_count)  \
                       + len(", ") * (max_thread_count - 1) \
                       + len('[]') + len('Socket ')
@@ -92,7 +96,7 @@
 for c in cores:
     output = "Core %s" % str(c).ljust(max_core_id_len)
     for s in sockets:
-        if core_map.has_key((s,c)):
+        if (s,c) in core_map:
             output += " " + str(core_map[(s, c)]).ljust(max_core_map_len)
         else:
             output += " " * (max_core_map_len + 1)
Signature · 1 line
2.12.2
#1 lore raw
Quoted 4 lines · Thomas Monjalon

These differences in Python 3 were causing errors:
- xrange is replaced by range
- dict values are a view (instead of list)
- has_key is removed

Thanks Thomas.

Please include minor correction in your patch as below.

Quoted 24 lines
Fixes deb87e6777c0 ("usertools: use sysfs for CPU layout")
Fixes 63985c5f104e ("usertools: fix CPU layout for more than 2 threads")
---
  usertools/cpu_layout.py | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
usertools/cpu_layout.py +4 -0
index 99152a2ec..74da928df 100755
@@ -35,6 +35,10 @@
  #
  from __future__ import print_function
  import sys
+try:
+    xrange # Python 2
+except NameError:
+    xrange = range # Python 3
 
  sockets = []
  cores = []

usertools/cpu_layout.py line 79

Quoted 5 lines
usertools/cpu_layout.py +1 -1
@@ -72,7 +76,7 @@
  print("")
 
  max_processor_len = len(str(len(cores) * len(sockets) * 2 - 1))
-max_thread_count = len(core_map.values()[0])
+max_thread_count = len(core_map)

We need to retain thread count calculated from any of core_map values (index 0 as in patch), hence keeping ljust() in output same as w/o this change. So,

max_thread_count = len(list(core_map.values())[0])

Above list() ensures return values are in list always irrespective of python 2x / 3x.

Regards,
Gowrishankar

Quoted 12 lines
usertools/cpu_layout.py +1 -1
  max_core_map_len = (max_processor_len * max_thread_count)  \
                        + len(", ") * (max_thread_count - 1) \
                        + len('[]') + len('Socket ')
@@ -92,7 +96,7 @@
  for c in cores:
      output = "Core %s" % str(c).ljust(max_core_id_len)
      for s in sockets:
-        if core_map.has_key((s,c)):
+        if (s,c) in core_map:
              output += " " + str(core_map[(s, c)]).ljust(max_core_map_len)
          else:
              output += " " * (max_core_map_len + 1)
#2 lore raw

01/05/2017 08:29, gowrishankar muthukrishnan:

Quoted 5 lines · Thomas Monjalon
Quoted 4 lines

These differences in Python 3 were causing errors:
- xrange is replaced by range
- dict values are a view (instead of list)
- has_key is removed

Quoted 7 lines

Thanks Thomas.

Please include minor correction in your patch as below.

Quoted 4 lines
Fixes deb87e6777c0 ("usertools: use sysfs for CPU layout")
Fixes 63985c5f104e ("usertools: fix CPU layout for more than 2 threads")

Applied with suggested change