Skip to contentSkip to conversation

scripts/gdb: Fix a python exception in mtree.py

Qemu-devel Yang Wei 2 participants 3 messages Dec 2, 2015 – Dec 4, 2015

lore
Share & tools
lore

Yang Wei

author
#0 lore raw

The following exception is threw:
Python Exception <class 'NameError'> name 'long' is not defined:
Error occurred in Python command: name 'long' is not defined

In python3, long is rename to int

---
 scripts/qemugdb/mtree.py | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
scripts/qemugdb/mtree.py +12 -5
index 06011c3..460d4b6 100644
@@ -16,12 +16,19 @@
 # 'qemu mtree' -- display the memory hierarchy
 
 import gdb
+import sys
 
 def isnull(ptr):
     return ptr == gdb.Value(0).cast(ptr.type)
 
+# PEP 0237: long renamed to int. That is, there is only one built-integral
+# integral type, named int; but it behaves mostly like the old long type.
+# https://docs.python.org/3.3/whatsnew/3.0.html#integers
+def intptr(p):
+    return long(p) if sys.version_info.major == 2 else int(p)
+
 def int128(p):
-    return long(p['lo']) + (long(p['hi']) << 64)
+    return intptr(p['lo']) + (intptr(p['hi']) << 64)
 
 class MtreeCommand(gdb.Command):
     '''Display the memory tree hierarchy'''
@@ -40,11 +47,11 @@ class MtreeCommand(gdb.Command):
     def process_queue(self):
         while self.queue:
             ptr = self.queue.pop(0)
-            if long(ptr) in self.seen:
+            if intptr(ptr) in self.seen:
                 continue
             self.print_item(ptr)
     def print_item(self, ptr, offset = gdb.Value(0), level = 0):
-        self.seen.add(long(ptr))
+        self.seen.add(intptr(ptr))
         addr = ptr['addr']
         addr += offset
         size = int128(ptr['size'])
@@ -58,8 +65,8 @@ class MtreeCommand(gdb.Command):
             klass = ' (RAM)'
         gdb.write('%s%016x-%016x %s%s (@ %s)\n'
                   % ('  ' * level,
-                     long(addr),
-                     long(addr + (size - 1)),
+                     intptr(addr),
+                     intptr(addr + (size - 1)),
                      ptr['name'].string(),
                      klass,
                      ptr,
Signature · 1 line
1.9.1
author
#1 lore raw

Ping.

Add Peter Maydell.

Thanks
Wei
---------- Forwarded message ----------
From: Yang Wei <[email protected]>
Date: 2015-12-02 15:56 GMT+08:00
Subject: [Qemu-devel] scripts/gdb: Fix a python exception in mtree.py
To: [email protected], [email protected], [email protected]
Cc: [email protected]

The following exception is threw:
Python Exception <class 'NameError'> name 'long' is not defined:
Error occurred in Python command: name 'long' is not defined

In python3, long is rename to int

---
 scripts/qemugdb/mtree.py | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
scripts/qemugdb/mtree.py +12 -5
index 06011c3..460d4b6 100644
@@ -16,12 +16,19 @@
 # 'qemu mtree' -- display the memory hierarchy
 
 import gdb
+import sys
 
 def isnull(ptr):
     return ptr == gdb.Value(0).cast(ptr.type)
 
+# PEP 0237: long renamed to int. That is, there is only one built-integral
+# integral type, named int; but it behaves mostly like the old long type.
+# https://docs.python.org/3.3/whatsnew/3.0.html#integers
+def intptr(p):
+    return long(p) if sys.version_info.major == 2 else int(p)
+
 def int128(p):
-    return long(p['lo']) + (long(p['hi']) << 64)
+    return intptr(p['lo']) + (intptr(p['hi']) << 64)
 
 class MtreeCommand(gdb.Command):
     '''Display the memory tree hierarchy'''
@@ -40,11 +47,11 @@ class MtreeCommand(gdb.Command):
     def process_queue(self):
         while self.queue:
             ptr = self.queue.pop(0)
-            if long(ptr) in self.seen:
+            if intptr(ptr) in self.seen:
                 continue
             self.print_item(ptr)
     def print_item(self, ptr, offset = gdb.Value(0), level = 0):
-        self.seen.add(long(ptr))
+        self.seen.add(intptr(ptr))
         addr = ptr['addr']
         addr += offset
         size = int128(ptr['size'])
@@ -58,8 +65,8 @@ class MtreeCommand(gdb.Command):
             klass = ' (RAM)'
         gdb.write('%s%016x-%016x %s%s (@ %s)\n'
                   % ('  ' * level,
-                     long(addr),
-                     long(addr + (size - 1)),
+                     intptr(addr),
+                     intptr(addr + (size - 1)),
                      ptr['name'].string(),
                      klass,
                      ptr,

--
1.9.1

#2 lore raw
In reply to Yang Wei
Quoted 10 lines · Yang Wei

The following exception is threw:
Python Exception <class 'NameError'> name 'long' is not defined:
Error occurred in Python command: name 'long' is not defined

In python3, long is rename to int

---
 scripts/qemugdb/mtree.py | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

Even in Python 2.4+, int()/long() have been unified to an extent:
https://www.python.org/dev/peps/pep-0237/
https://docs.python.org/2/whatsnew/2.4.html

If the value is too large for the int() constructor, it returns a long().

Can this patch be simplified to s/\<int\>/long/g ?

That way no Python version checks are necessary.

Quoted 54 lines
scripts/qemugdb/mtree.py +12 -5
index 06011c3..460d4b6 100644
@@ -16,12 +16,19 @@
 # 'qemu mtree' -- display the memory hierarchy
 
 import gdb
+import sys
 
 def isnull(ptr):
     return ptr == gdb.Value(0).cast(ptr.type)
 
+# PEP 0237: long renamed to int. That is, there is only one built-integral
+# integral type, named int; but it behaves mostly like the old long type.
+# https://docs.python.org/3.3/whatsnew/3.0.html#integers
+def intptr(p):
+    return long(p) if sys.version_info.major == 2 else int(p)
+
 def int128(p):
-    return long(p['lo']) + (long(p['hi']) << 64)
+    return intptr(p['lo']) + (intptr(p['hi']) << 64)
 
 class MtreeCommand(gdb.Command):
     '''Display the memory tree hierarchy'''
@@ -40,11 +47,11 @@ class MtreeCommand(gdb.Command):
     def process_queue(self):
         while self.queue:
             ptr = self.queue.pop(0)
-            if long(ptr) in self.seen:
+            if intptr(ptr) in self.seen:
                 continue
             self.print_item(ptr)
     def print_item(self, ptr, offset = gdb.Value(0), level = 0):
-        self.seen.add(long(ptr))
+        self.seen.add(intptr(ptr))
         addr = ptr['addr']
         addr += offset
         size = int128(ptr['size'])
@@ -58,8 +65,8 @@ class MtreeCommand(gdb.Command):
             klass = ' (RAM)'
         gdb.write('%s%016x-%016x %s%s (@ %s)\n'
                   % ('  ' * level,
-                     long(addr),
-                     long(addr + (size - 1)),
+                     intptr(addr),
+                     intptr(addr + (size - 1)),
                      ptr['name'].string(),
                      klass,
                      ptr,
Signature · 1 line
1.9.1