]> git.rmz.io Git - dotfiles.git/blob - gdb/helper.py
nvim: add FPP copyright snippet
[dotfiles.git] / gdb / helper.py
1 # -*- coding: utf-8 -*-
2 # Helper module for pretty-printers
3
4 # Copyright (C) 2013 Kevin Funk <kfunk@kde.org>
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 import sys
20
21 # BEGIN: Utilities for wrapping differences of Python 2.x and Python 3
22 # Inspired by http://pythonhosted.org/six/
23
24 # Useful for very coarse version differentiation.
25 PY2 = sys.version_info[0] == 2
26 PY3 = sys.version_info[0] == 3
27
28 # create Python 2.x & 3.x compatible iterator base
29 if PY3:
30 Iterator = object
31 else:
32 class Iterator(object):
33
34 def next(self):
35 return type(self).__next__(self)
36 if PY3:
37 unichr = chr
38 else:
39 unichr = unichr
40
41 # END
42
43 # BEGIN: Helper functions for pretty-printers
44
45 def has_field(val, name):
46 """Check whether @p val (gdb.Value) has a field named @p name"""
47 try:
48 val[name]
49 return True
50 except Exception:
51 return False
52
53 def default_iterator(val):
54 for field in val.type.fields():
55 yield field.name, val[field.name]
56
57 class FunctionLookup:
58
59 def __init__(self, gdb, pretty_printers_dict):
60 self.gdb = gdb
61 self.pretty_printers_dict = pretty_printers_dict
62
63 def __call__(self, val):
64 "Look-up and return a pretty-printer that can print val."
65
66 # Get the type.
67 type = val.type;
68
69 # If it points to a reference, get the reference.
70 if type.code == self.gdb.TYPE_CODE_REF:
71 type = type.target ()
72
73 # Get the unqualified type, stripped of typedefs.
74 type = type.unqualified ().strip_typedefs ()
75
76 # Get the type name.
77 typename = type.tag
78 if typename == None:
79 return None
80
81 # Iterate over local dictionary of types to determine
82 # if a printer is registered for that type. Return an
83 # instantiation of the printer if found.
84 for function in self.pretty_printers_dict:
85 if function.search (typename):
86 return self.pretty_printers_dict[function](val)
87
88 # Cannot find a pretty printer. Return None.
89 return None
90
91 # END