[glib] Add pretty printer for hashtables
- From: Alexander Larsson <alexl src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [glib] Add pretty printer for hashtables
- Date: Mon, 21 Sep 2009 13:39:55 +0000 (UTC)
commit 2b8943237f137f287b0b0854f80198de54fd26ea
Author: Alexander Larsson <alexl redhat com>
Date: Mon Sep 21 12:26:23 2009 +0200
Add pretty printer for hashtables
glib/glib.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 64 insertions(+), 0 deletions(-)
---
diff --git a/glib/glib.py b/glib/glib.py
index df000bf..aa71c20 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":
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]