summaryrefslogtreecommitdiff
path: root/python/helpers/pydev/pydevd_save_locals.py
blob: 2808081a586642c69b8b16bf1636dc379b821c7f (plain)
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
"""
Utility for saving locals.
"""
import sys

def is_save_locals_available():
    try:
        if '__pypy__' in sys.builtin_module_names:
            import __pypy__
            save_locals = __pypy__.locals_to_fast
            return True
    except:
        pass

    
    try:
        import ctypes
    except:
        return False #Not all Python versions have it

    try:
        func = ctypes.pythonapi.PyFrame_LocalsToFast
    except:
        return False
    
    return True

def save_locals(frame):
    """
    Copy values from locals_dict into the fast stack slots in the given frame.

    Note: the 'save_locals' branch had a different approach wrapping the frame (much more code, but it gives ideas
    on how to save things partially, not the 'whole' locals).
    """
    try:
        if '__pypy__' in sys.builtin_module_names:
            import __pypy__
            save_locals = __pypy__.locals_to_fast
            save_locals(frame)
            return
    except:
        pass
    

    try:
        import ctypes
    except:
        return #Not all Python versions have it

    try:
        func = ctypes.pythonapi.PyFrame_LocalsToFast
    except:
        return

    #parameter 0: don't set to null things that are not in the frame.f_locals (which seems good in the debugger context).
    func(ctypes.py_object(frame), ctypes.c_int(0))