Search code examples
pythonkomodokomodoedit

Komodo - watch variables and execute code while on pause in the program


With c# in the Visual Studio IDE I can pause at anytime a program and watch its variables, inspect whatever I want. I noticed that with the Komodo IDE when something crashes and it stops the flow of the program, I can do exactly the same. But for some reason, it seems that when I try to do the same when I manually pause the program, the same cannot be achieved. Am I doing something wrong or it just isn't possible? In the later case, could anyone care to explain me why? Is it IDE related or Python related?

Thanks

edit: Other question, how can I then continue the program? From what I see, after I call code.interact(local = locals()), it behaves as the program is still running so I can't click in the "Run" button, only on "Pause" or "Close".


Solution

  • If you put

    import code
    code.interact(local=locals())
    

    in your program, then you will be dumped to a python interpreter. (See Method to peek at a Python program running right now)

    This is a little different than pausing Komodo, but perhaps you can use it to achieve the same goal.

    Pressing Ctrl-d exits the python interpreter and allows your program to resume.

    You can inspect the call stack using the traceback module:

    import traceback
    traceback.extract_stack()
    

    For example, here is a decorator which prints the call stack:

    def print_trace(func):
        '''This decorator prints the call stack
        '''
        def wrapper(*args,**kwargs):
            stacks=traceback.extract_stack()
            print('\n'.join(
                ['  '*i+'%s %s:%s'%(text,line_number,filename)
                 for i,(filename,line_number,function_name,text) in enumerate(stacks)]))
            res = func(*args,**kwargs)
            return res
        return wrapper
    

    Use it like this:

    @print_trace
    def f():
        pass