File: __kdevpython_debugger_utils.py

package info (click to toggle)
kdevelop-python 5.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 12,480 kB
  • sloc: python: 183,325; cpp: 17,155; xml: 1,137; sh: 14; makefile: 13
file content (69 lines) | stat: -rw-r--r-- 1,901 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# This file is imported from within the debugger

# Copyright 2014 Sven Brauch
# License: GPL v2+

from kdevpdb import kdevOutputFormatter

import sys

__kdevpython_builtin_locals = locals

try:
    from numpy import ndarray
except:
    class ndarray: pass

# TODO: weakref those, but python can't in general :(
objectTable = {}

def cleanup():
    objectTable.clear()

def obj_to_string(value):
    if type(value) == ndarray:
        value = "numpy.array, shape={0}".format(value.shape)
    value = str(value).replace('\n', r'\n')
    if len(value) > 120:
        value = value[:120] + "..."
    return value

def format_locals(locals_):
    '''Print local variables in a machine-readable format'''
    cleanup()
    for key, value in locals_.items():
        if key == '__kdevpython_debugger_utils':
            continue
        value = obj_to_string(value)
        print("%s => %s" % (key, value))

def format_ptr_children(ptr):
    try:
        expr = objectTable[ptr]
    except KeyError:
        print("Address of object not in memory any more")
        return
    format_object_children(expr)

def format_object_children(expr):
    if type(expr) == set:
        expr = list(expr)

    output = []
    if type(expr) == list or type(expr) == ndarray:
        for i in range(len(expr)):
            identifier = id(expr[i])
            obj = expr[i]
            output.append('ptr:<%s> [%s] => %s' % (identifier, i, obj_to_string(obj)))
            objectTable[identifier] = obj
    elif type(expr) == dict:
        for k, v in expr.items():
            output.append('ptr:<%s> [%s] => %s' % (id(v), obj_to_string(k), obj_to_string(v)))
            objectTable[id(v)] = v
    else:
        for i in dir(expr):
            obj = getattr(expr, i)
            output.append('ptr:<%s> .%s => %s' % (id(obj), i, obj_to_string(obj)))
            objectTable[id(obj)] = obj
    print('\n'.join(output))