Warning, file /kdevelop/kdev-python/debugger/__kdevpython_debugger_utils.py was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 # This file is imported from within the debugger
0002 
0003 # SPDX-FileCopyrightText: 2014 Sven Brauch <svenbrauch@gmail.com>
0004 # SPDX-License-Identifier: GPL-2.0-or-later
0005 
0006 from kdevpdb import kdevOutputFormatter
0007 
0008 import sys
0009 
0010 __kdevpython_builtin_locals = locals
0011 
0012 try:
0013     from numpy import ndarray
0014 except:
0015     class ndarray: pass
0016 
0017 # TODO: weakref those, but python can't in general :(
0018 objectTable = {}
0019 
0020 def cleanup():
0021     objectTable.clear()
0022 
0023 def obj_to_string(value):
0024     if type(value) == ndarray:
0025         value = "numpy.array, shape={0}".format(value.shape)
0026     value = str(value).replace('\n', r'\n')
0027     if len(value) > 120:
0028         value = value[:120] + "..."
0029     return value
0030 
0031 def format_locals(locals_):
0032     '''Print local variables in a machine-readable format'''
0033     cleanup()
0034     for key, value in locals_.items():
0035         if key == '__kdevpython_debugger_utils':
0036             continue
0037         value = obj_to_string(value)
0038         print("%s => %s" % (key, value))
0039 
0040 def format_ptr_children(ptr):
0041     try:
0042         expr = objectTable[ptr]
0043     except KeyError:
0044         print("Address of object not in memory any more")
0045         return
0046     format_object_children(expr)
0047 
0048 def format_object_children(expr):
0049     if type(expr) == set:
0050         expr = list(expr)
0051 
0052     output = []
0053     if type(expr) == list or type(expr) == ndarray:
0054         for i in range(len(expr)):
0055             identifier = id(expr[i])
0056             obj = expr[i]
0057             output.append('ptr:<%s> [%s] => %s' % (identifier, i, obj_to_string(obj)))
0058             objectTable[identifier] = obj
0059     elif type(expr) == dict:
0060         for k, v in expr.items():
0061             output.append('ptr:<%s> [%s] => %s' % (id(v), obj_to_string(k), obj_to_string(v)))
0062             objectTable[id(v)] = v
0063     else:
0064         for i in dir(expr):
0065             obj = getattr(expr, i)
0066             output.append('ptr:<%s> .%s => %s' % (id(obj), i, obj_to_string(obj)))
0067             objectTable[id(obj)] = obj
0068     print('\n'.join(output))
0069