File indexing completed on 2024-05-12 04:39:56
0001 # -*- coding: utf-8 -*- 0002 # Helper module for pretty-printers 0003 0004 # SPDX-FileCopyrightText: 2013 Kevin Funk <kfunk@kde.org> 0005 # 0006 # SPDX-License-Identifier: GPL-2.0-or-later 0007 0008 import sys 0009 0010 # BEGIN: Utilities for wrapping differences of Python 2.x and Python 3 0011 # Inspired by http://pythonhosted.org/six/ 0012 0013 # Useful for very coarse version differentiation. 0014 PY2 = sys.version_info[0] == 2 0015 PY3 = sys.version_info[0] == 3 0016 0017 # create Python 2.x & 3.x compatible iterator base 0018 if PY3: 0019 Iterator = object 0020 else: 0021 class Iterator(object): 0022 0023 def next(self): 0024 return type(self).__next__(self) 0025 if PY3: 0026 unichr = chr 0027 else: 0028 unichr = unichr 0029 0030 # END 0031 0032 # BEGIN: Helper functions for pretty-printers 0033 0034 def has_field(val, name): 0035 """Check whether @p val (gdb.Value) has a field named @p name""" 0036 try: 0037 val[name] 0038 return True 0039 except Exception: 0040 return False 0041 0042 def default_iterator(val): 0043 for field in val.type.fields(): 0044 yield field.name, val[field.name] 0045 0046 class FunctionLookup: 0047 0048 def __init__(self, gdb, pretty_printers_dict): 0049 self.gdb = gdb 0050 self.pretty_printers_dict = pretty_printers_dict 0051 0052 def __call__(self, val): 0053 "Look-up and return a pretty-printer that can print val." 0054 0055 # Get the type. 0056 type = val.type; 0057 0058 # If it points to a reference, get the reference. 0059 if type.code == self.gdb.TYPE_CODE_REF: 0060 type = type.target () 0061 0062 # Get the unqualified type, stripped of typedefs. 0063 type = type.unqualified ().strip_typedefs () 0064 0065 # Get the type name. 0066 typename = type.tag 0067 if typename == None: 0068 return None 0069 0070 # Iterate over local dictionary of types to determine 0071 # if a printer is registered for that type. Return an 0072 # instantiation of the printer if found. 0073 for function in self.pretty_printers_dict: 0074 if function.search (typename): 0075 return self.pretty_printers_dict[function](val) 0076 0077 # Cannot find a pretty printer. Return None. 0078 return None 0079 0080 # END