Tutorial: Debugging from the Python Shell

In addition to launching code to debug from Wing's menu bar and Debug menu, it is also
possible to debug code that is entered into the PythonShell (and in Wing Pro the
DebugProbe).

Enable this now by clicking on the bug icon in the top right of the PythonShell. Once
this is done, the status message at the top of the PythonShell should change to
include Commandswillbedebugged and an extra margin is shown in which you can set
breakpoints. Wing will reach those breakpoints, as well as any breakpoints in editors for
code that is invoked. Any exceptions will also be reported in the debugger.

Let's try this out. Paste the following into the PythonShell and press Enter
so that you are returned to the >>> prompt:

def test_function():
x = 10
print(x)
x += 5
y = 20
print(x+y)

Then place a breakpoint on the line that reads print(x) by clicking in the breakpoint
margin at the left of the line text and prompt.

Next type this into the PythonShell and press Enter:

test_function()

Wing should reach the breakpoint on the print(x) line.

You can now work with the debugger in the same way that you would if you have launched
code from the toolbar or Debug menu. Try stepping and viewing the values of x
and y as they change, either in the StackData tool or by hovering the mouse over
the variable names.

Take a look at the stack in the CallStack or StackData tool to see how stack
frames that occur withing the PythonShell are listed. You can move up and down the
stack just as you would if your stack frames were in an editor.

Notice that if you step off the end of the call, you will return to the shell prompt. If
you press the red Stop button in the toolbar or select StopDebugging from the
Debug menu, Wing will complete execution of the code without debug and return you to
the >>> prompt. Note that the code is still executed to completion in this case
becaused there is no way to simply abandon a number of stack frames in the Python
interpreter.

Recursive Debugging

By default Wing will not return you to the >>> prompt until your code has finished
executing. In Wing Pro, it is possible to enable recursive debugging but this can be quite confusing,
so it is disabled by default.

To try this out, check the EnableRecursiveDebug item in the Options menu in the
PythonShell. Then type test_function() again in the PythonShell. You will
see that the shell returns immediately to the >>> prompt even though you are now at
the breakpoint you set earlier on print(x). Note that the message area in the PythonShell indicates that you are debugging recursively and gives you the level to which you
have recursed (for example Debuggingrecursively(R=2) indicates two levels of
recursive debugging).

Try typing test_function() again (or just press the Up arrow on the keyboard) and
press Enter. This is essentially the same thing as invoking test_function()
from the line at which the debugger is currently paused, in this case within
test_function itself.

Try doing this several times. Each time you do this, another level of recursive debugging
is entered. Look at the CallStack tool and go up and down the stack to better
understand what is happening.

Now if you press Continue in the toolbar or use Start/Continue in the Debug
menu you will exit one level of recursion. Similarly, Stop exits one level of
recursion without debugging the remainder of that recursive invocation.