]>
git.rmz.io Git - dotfiles.git/blob - gdb/helper.py
1 # -*- coding: utf-8 -*-
2 # Helper module for pretty-printers
4 # Copyright (C) 2013 Kevin Funk <kfunk@kde.org>
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.
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.
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/>.
21 # BEGIN: Utilities for wrapping differences of Python 2.x and Python 3
22 # Inspired by http://pythonhosted.org/six/
24 # Useful for very coarse version differentiation.
25 PY2
= sys
.version_info
[0] == 2
26 PY3
= sys
.version_info
[0] == 3
28 # create Python 2.x & 3.x compatible iterator base
32 class Iterator(object):
35 return type(self
).__next
__(self
)
43 # BEGIN: Helper functions for pretty-printers
45 def has_field(val
, name
):
46 """Check whether @p val (gdb.Value) has a field named @p name"""
53 def default_iterator(val
):
54 for field
in val
.type.fields():
55 yield field
.name
, val
[field
.name
]
59 def __init__(self
, gdb
, pretty_printers_dict
):
61 self
.pretty_printers_dict
= pretty_printers_dict
63 def __call__(self
, val
):
64 "Look-up and return a pretty-printer that can print val."
69 # If it points to a reference, get the reference.
70 if type.code
== self
.gdb
.TYPE_CODE_REF
:
73 # Get the unqualified type, stripped of typedefs.
74 type = type.unqualified ().strip_typedefs ()
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
)
88 # Cannot find a pretty printer. Return None.