summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Larsson <alexl@redhat.com>2009-09-21 12:26:23 +0200
committerAlexander Larsson <alexl@redhat.com>2009-09-21 15:39:00 +0200
commit2b8943237f137f287b0b0854f80198de54fd26ea (patch)
treefc9952e8f883afa97139778d8207308cfc96031f
parent2e8768d9a556afd2b2e6c974dcbcf24fee5ba6ff (diff)
Add pretty printer for hashtables
-rw-r--r--glib/glib.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/glib/glib.py b/glib/glib.py
index df000bffd..aa71c20e9 100644
--- a/glib/glib.py
+++ b/glib/glib.py
@@ -69,6 +69,68 @@ class GListPrinter:
def display_hint (self):
return "array"
+class GHashPrinter:
+ "Prints a GHashTable"
+
+ class _iterator:
+ def __init__(self, ht, keys_are_strings):
+ self.ht = ht
+ if ht != 0:
+ self.array = ht["nodes"]
+ self.size = ht["size"]
+ self.pos = 0
+ self.keys_are_strings = keys_are_strings
+ self.value = None
+
+ def __iter__(self):
+ return self
+
+ def next(self):
+ if self.ht == 0:
+ raise StopIteration
+ if self.value != None:
+ v = self.value
+ self.value = None
+ return v
+ while long(self.pos) < long(self.size):
+ node = self.array[self.pos]
+ self.pos = self.pos + 1
+ if long (node["key_hash"]) >= 2:
+ key = node["key"]
+ val = node["value"]
+
+ if self.keys_are_strings:
+ key = key.cast (gdb.lookup_type("char").pointer())
+
+ # Queue value for next result
+ self.value = ('[%dv]'% (self.pos), val)
+
+ # Return key
+ return ('[%dk]'% (self.pos), key)
+ raise StopIteration
+
+ def __init__ (self, val):
+ self.val = val
+ self.keys_are_strings = False
+ try:
+ string_hash = read_global_var ("g_str_hash")
+ except:
+ try:
+ string_hash = read_global_var ("IA__g_str_hash")
+ except:
+ string_hash = None
+ if self.val != 0 and string_hash != None and self.val["hash_func"] == string_hash:
+ self.keys_are_strings = True
+
+ def children(self):
+ return self._iterator(self.val, self.keys_are_strings)
+
+ def to_string (self):
+ return "0x%x" % (long(self.val))
+
+ def display_hint (self):
+ return "map"
+
def pretty_printer_lookup (val):
if is_g_type_instance (val):
return GTypePrettyPrinter (val)
@@ -89,6 +151,8 @@ def pretty_printer_lookup (val):
return GListPrinter(val, "GList")
if t == "GSList":
return GListPrinter(val, "GSList")
+ if t == "GHashTable":
+ return GHashPrinter(val)
else:
t = str(type)
if t == "GList":