views:

129

answers:

1

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".

+2  A: 

If you put

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

in your program, then you will be dumped to a python interpreter. (See http://stackoverflow.com/questions/1637198/method-to-peek-at-a-running-python-program/1637277#1637277)

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
unutbu
That's exactly what I wanted, but I noted it seems there's no call stack :(
devoured elysium
I've edit my response to address the call stack issue and using ctrl-d to resume.
unutbu