読者です 読者をやめる 読者になる 読者になる

runtime-gdb.pyを Python3で動かすためのパッチ

go python

Go言語のOS X上でのGDBデバッグ環境構築 - unknownplace.org

パッチ

go-1.1.2のソースに対するパッチです.


https://gist.github.com/syohex/7207967#file-support-python3-diff

diff --git a/runtime-gdb.py b/runtime-gdb.py
index cb70ca0..ac940c3 100644
--- a/runtime-gdb.py
+++ b/runtime-gdb.py
@@ -17,7 +17,7 @@ path to this file based on the path to the runtime package.
 
 import sys, re
 
-print >>sys.stderr, "Loading Go Runtime support."
+sys.stderr.write("Loading Go Runtime support.\n")
 
 # allow to manually reload while developing
 goobjfile = gdb.current_objfile() or gdb.objfiles()[0]
@@ -355,7 +355,7 @@ class GoroutinesCmd(gdb.Command):
 			pc = ptr['sched']['pc'].cast(vp)
 			sp = ptr['sched']['sp'].cast(vp)
 			blk = gdb.block_for_pc(long((pc)))
-			print s, ptr['goid'], "%8s" % sts[long((ptr['status']))], blk.function
+			print(s, ptr['goid'], "{0:8s}".format(sts[long((ptr['status']))]), blk.function)
 
 def find_goroutine(goid):
 	vp = gdb.lookup_type('void').pointer()
@@ -363,7 +363,7 @@ def find_goroutine(goid):
 		if ptr['status'] == 6:	# 'gdead'
 			continue
 		if ptr['goid'] == goid:
-			return [ptr['sched'][x].cast(vp) for x in 'pc', 'sp']
+			return [ptr['sched'][x].cast(vp) for x in ['pc', 'sp']]
 	return None, None
 
 
@@ -387,7 +387,7 @@ class GoroutineCmd(gdb.Command):
 		goid = gdb.parse_and_eval(goid)
 		pc, sp = find_goroutine(int(goid))
 		if not pc:
-			print "No such goroutine: ", goid
+			print("No such goroutine: ", goid)
 			return
 		save_frame = gdb.selected_frame()
 		gdb.parse_and_eval('$save_pc = $pc')
@@ -413,8 +413,8 @@ class GoIfaceCmd(gdb.Command):
 			try:
 				#TODO fix quoting for qualified variable names
 				obj = gdb.parse_and_eval("%s" % obj)
-			except Exception, e:
-				print "Can't parse ", obj, ": ", e
+			except Exception as e:
+				print("Can't parse ", obj, ": ", e)
 				continue
 
 			if obj['data'] == 0:
@@ -423,10 +423,10 @@ class GoIfaceCmd(gdb.Command):
 				dtype = iface_dtype(obj)
 				
 			if dtype is None:
-				print "Not an interface: ", obj.type
+				print("Not an interface: ", obj.type)
 				continue
 
-			print "%s: %s" % (obj.type, dtype)
+			print("{0}: {1}".format(obj.type, dtype))
 
 # TODO: print interface's methods and dynamic type's func pointers thereof.
 #rsc: "to find the number of entries in the itab's Fn field look at itab.inter->numMethods
@@ -436,6 +436,7 @@ class GoIfaceCmd(gdb.Command):
 #
 # Register all convenience functions and CLI commands
 #
-for k in vars().values():
+var_dict = vars().copy()
+for k in var_dict.values():
 	if hasattr(k, 'invoke'):
 		k()

おわりに

Pythonはド素人なので, 問題があればお知らせください.