Saturday, November 26, 2022
HomeArtificial IntelligenceSetting Breakpoints and Exception Hooks in Python

Setting Breakpoints and Exception Hooks in Python


Final Up to date on Might 6, 2022

There are other ways of debugging code in Python, certainly one of which is to introduce breakpoints into the code at factors the place one want to invoke a Python debugger. The statements used to enter a debugging session at completely different name websites depend upon the model of the Python interpreter that one is working with, as we will see on this tutorial. 

On this tutorial, you’ll uncover varied methods of setting breakpoints in numerous variations of Python. 

After finishing this tutorial, you’ll know:

  • The way to invoke the pdb debugger in earlier variations of Python
  • The way to use the brand new, built-in
    breakpoint() perform launched in Python 3.7
  • The way to write your personal
    breakpoint() perform to simplify the debugging course of in earlier variations of Python
  • The way to use a autopsy debugger

Let’s get began. 

Setting Breakpoints in Totally different Variations of Python
Photograph by Josh Withers, some rights reserved.

Tutorial Overview

This tutorial is split into three elements; they’re:

  • Setting Breakpoints in Python Code
    • Invoking the pdb Debugger in Earlier Variations of Python
    • Utilizing the breakpoint() Perform in Python 3.7
  • Writing One’s Personal breakpoint() Perform for Earlier Variations of Python
  • Limitations of the breakpoint() Perform

Setting Breakpoints in Python Code

Now we have beforehand seen that a technique of debugging a Python script is to run it within the command line with the Python debugger. 

So as to take action, we would wish to make use of the
m pdb command that masses the pdb module earlier than executing the Python script. In the identical command-line interface, we might then observe this by a particular debugger command of alternative, similar to
n to maneuver to the subsequent line or
s if we intend to step right into a perform. 

This methodology may turn into cumbersome rapidly because the size of the code will increase. One approach to tackle this downside and achieve higher management over the place to interrupt your code is to insert a breakpoint instantly into the code. 

Invoking the pdb Debugger in Earlier Variations of Python

Invoking the pdb debugger previous to Python 3.7 would require you to
import pdb and name
pdb.set_trace() on the level in your code the place you want to enter an interactive debugging session. 

If we rethink, for example, the code for implementing the overall consideration mechanism, we are able to break into the code as follows:

Executing the script now opens up the pdb debugger proper earlier than we compute the variable scores, and we are able to proceed to challenge any debugger instructions of alternative, similar to
n to maneuver to the subsequent line or
c to proceed execution:

Though useful, this isn’t essentially the most elegant and intuitive method of inserting a breakpoint into your code. Python 3.7 implements a extra simple method of doing so, as we will see subsequent.

Utilizing the breakpoint() Perform in Python 3.7 

Python 3.7 comes with a built-in
breakpoint() perform that enters the Python debugger on the name web site (or the purpose within the code at which the
breakpoint() assertion is positioned). 

When known as, the default implementation of the
breakpoint() perform will name
sys.breakpointhook(), which in flip calls the
pdb.set_trace() perform. That is handy as a result of we won’t have to
import pdb and name
pdb.set_trace() explicitly ourselves. 

Let’s rethink the code for implementing the overall consideration mechanism and now introduce a breakpoint through the
breakpoint() assertion:

One benefit of utilizing the
breakpoint() perform is that, in calling the default implementation of
sys.breakpointhook(), the worth of a brand new atmosphere variable,
PYTHONBREAKPOINT, is consulted. This atmosphere variable can take varied values, based mostly on which completely different operations may be carried out. 

For instance, setting the worth of
PYTHONBREAKPOINT to 0 disables all breakpoints. Therefore, your code may include as many breakpoints as mandatory, however these may be simply stopped from halting the execution of the code with out having to take away them bodily. If (for instance) the identify of the script containing the code is most important.py, we might disable all breakpoints by calling it within the command line interface as follows:

In any other case, we are able to obtain the identical final result by setting the atmosphere variable within the code itself:

The worth of
PYTHONBREAKPOINT is consulted each time that
sys.breakpointhook() known as. Which means that the worth of this atmosphere variable may be modified throughout the code execution, and the
breakpoint() perform would reply accordingly.  

The
PYTHONBREAKPOINT atmosphere variable can be set to different values, such because the identify of a callable. Say, as an example, that we’d like to make use of a distinct Python debugger aside from pdb, similar to ipdb (run
pip set up ipdb first if the debugger has not but been put in). On this case, we might name the most important.py script within the command line interface and hook the debugger with out making any modifications to the code itself:

In doing so, the
breakpoint() perform enters the ipdb debugger on the subsequent name web site:

The perform may take enter arguments as
breakpoint(*args, **kws), that are then handed on to
sys.breakpointhook(). It’s because any callable (similar to a third-party debugger module) would possibly settle for non-compulsory arguments, which may be handed by way of the
breakpoint() perform. 

Writing Your Personal breakpoint() Perform in Earlier Variations of Python

Let’s return to the truth that variations of Python sooner than v3.7 don’t include the
breakpoint() perform readily in-built. We will write our personal. 

Equally to how the
breakpoint() perform is carried out from Python 3.7 onwards, we are able to implement a perform that checks the worth of an atmosphere variable and:

  • Skips all breakpoints within the code if the worth of the atmosphere variable is ready to 0.
  • Enters into the default Python pdb debugger if the atmosphere variable is an empty string.
  • Enters into one other debugger as specified by the worth of the atmosphere variable. 

We will embody this perform into the code and run it (utilizing a Python 2.7 interpreter, on this case). If we set the worth of the atmosphere variable to an empty string, we discover that the pdb debugger stops on the level within the code at which we’ve got positioned our
breakpoint() perform. We will then challenge debugger instructions into the command line from there onwards:

Equally, if we set the atmosphere variable to:

The
breakpoint() perform that we’ve got carried out now enters the ipdb debugger and stops on the name web site:

Setting the atmosphere variable to 0 merely skips all breakpoints, and the computed consideration output is returned within the command line, as anticipated:

This facilitates the method of breaking into the code for Python variations sooner than v3.7 as a result of it now turns into a matter of setting the worth of an atmosphere variable moderately than having to manually introduce (or take away) the
import pdb; pdb.set_trace() assertion at completely different name websites within the code. 

Limitations of the breakpoint() Perform

The breakpoint() perform permits you to carry within the debugger sooner or later in this system. You want to discover the precise place that you simply want the debugger to place the breakpoint into it. For those who think about the next code:

It will carry you the debugger when the perform func() raised exceptions. It may be triggered by the perform itself or deep inside another features that it calls. However the debugger will begin on the line print("exception!") above, which might not be very helpful.

The way in which that we are able to carry up the debugger on the level of exception known as the autopsy debugger. It really works by asking Python to register the debugger pdb.pm() because the exception handler when an uncaught exception is raised. When it’s known as, it would search for the final exception raised and begin the debugger at that time. To make use of the autopsy debugger, we simply want so as to add the next code earlier than this system is run:

That is useful as a result of nothing else must be modified in this system. For instance, assume we wish to consider the common of $1/x$ utilizing the next program. It’s fairly straightforward to miss some nook circumstances, however we are able to catch the difficulty when an exception is raised:

Once we run the above program, this system could terminate, or it could increase a division by zero exception, relying on whether or not the random quantity generator ever produces zero within the loop. In that case, we may even see the next:

the place we discovered the exception is raised at which line, and we are able to examine the worth of the variables as we are able to normally do in pdb.

In actual fact, it’s extra handy to print the traceback and the exception when the autopsy debugger is launched:

And the debugger session might be began as follows:

Additional Studying

This part supplies extra sources on the subject if you’re trying to go deeper.

Web sites

Abstract

On this tutorial, you found varied methods of setting breakpoints in numerous variations of Python. 

Particularly, you discovered:

  • The way to invoke the pdb debugger in earlier variations of Python
  • The way to make use of the brand new, built-in
    breakpoint() perform launched in Python 3.7
  • The way to write your personal
    breakpoint() perform to simplify the debugging course of in earlier variations of Python

Do you’ve gotten any questions?

Ask your questions within the feedback under, and I’ll do my greatest to reply.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments