Debugging with breakpoints
Python has a built-in debugger called pdb
. Stopping the execution of the code at any point is possible by setting a breakpoint. A breakpoint will jump into command-line mode. From the command line, the current status can be analyzed. Given that Python is interpreted, any new code can be executed from this stage. This is very flexible and allows you to create flexible breakpoints and change the current state to analyze the behavior of the program.
Let's see how to do it.
Getting ready
Download the debug_algorithm.py
script, available from GitHub: https://github.com/PacktPublishing/Python-Automation-Cookbook-Second-Edition/blob/master/Chapter13/debug_algorithm.py.
In the next section, we will analyze the execution of the code in detail. The code checks whether numbers fulfil certain criteria:
def valid(candidate):
if candidate <= 1:
return False
lower = candidate - 1
while lower > 1:
if candidate...