Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of Breakpoint Method

I am new to python and am unsure of how the breakpoint method works. Does it open the debugger for the IDE or some built-in debugger?

Additionally, I was wondering how that debugger would be able to be operated.

For example, I use Spyder, does that mean that if I use the breakpoint() method, Spyder's debugger will open, through which I could the Debugger dropdown menu, or would some other debugger open?

I would also like to know how this function works in conjunction with the breakpointhook() method.

like image 273
H. Khan Avatar asked Dec 11 '18 00:12

H. Khan


People also ask

What is the use of breakpoint?

Breakpoints are one of the most important debugging techniques in your developer's toolbox. You set breakpoints wherever you want to pause debugger execution. For example, you may want to see the state of code variables or look at the call stack at a certain breakpoint.

What breakpoint means?

Definition of breaking point 1 : the point at which a person gives way under stress. 2 : the point at which a situation becomes critical. 3 : the point at which something loses force or validity stretch the rules to the breaking point.

What breakpoint is used in Python?

breakpoint() is an alias that works in Python 3.7 and newer for import pdb; pdb. set_trace() , which works in all Python versions. pdb is Python's built in debugger and helps you step line by line through your code. If you want to learn more, check out Python Debugging With pdb.


2 Answers

No, debugger will not open itself automatically as a consequence of setting a breakpoint.

So you have first set a breakpoint (or more of them), and then manually launch a debugger.

After this, the debugger will perform your code as usually, but will stop performing instructions when it reaches a breakpoint - the instruction at the breakpoint itself it will not perform. It will pause just before it, given you an opportunity to perform some debug tasks, as

  • inspect variable values,
  • set variables manually to other values,
  • continue performing instructions step by step (i. e. only the next instruction),
  • continue performing instructions to the next breakpoint,
  • prematurely stop debugging your program.

This is the common scenario for all debuggers of all programming languages (and their IDEs).

For IDEs, launching a debugger will

  • enable or reveal debugging instructions in their menu system,
  • show a toolbar for them and will,
  • enable hot keys for them.

Without setting at least one breakpoint, most debuggers perform the whole program without a pause (as launching it without a debugger), so you will have no opportunity to perform any debugging task.

(Some IDEs have an option to launch a debugger in the "first instruction, then a pause" mode, so you need not set breakpoints in advance in this case.)


Yes, the breakpoint() built-in function (introduced in Python 3.7) stops executing your program, enters it in the debugging mode, and you may use Spyder's debugger drop-down menu.

(It isn't a Spyders' debugger, only its drop-down menu; the used debugger will be still the pdb, i. e. the default Python DeBugger.)

The connection between the breakpoint() built-in function and the breakpointhook() function (from the sys built-in module) is very straightforward - the first one directly calls the second one.

The natural question is why we need two functions with the exactly same behavior?

The answer is in the design - the breakpoint() function may be changed indirectly, by changing the behavior of the breakpointhook() function.

For example, IDE creators may change the behavior of the breakpointhook() function so that it will launch their own debugger, not the pdb one.

like image 126
MarianD Avatar answered Oct 26 '22 12:10

MarianD


The default behavior of the breakpoint() builtin is to open the pdb debugger at that point.

That is, by default the line

breakpoint()

Should behave identically to

import pdb; pdb.set_trace()

The behavior can be customized (e.g. to open a different debugger) by modifying sys.breakpointhook. Generally the only time you would do this is if you were implementing a debugger or something that functioned like a debugger. If you're running code from an IDE, the IDE itself should modify sys.breakpointhook so that it opens the IDE debugger. (I don't know if all Python IDEs actually do this, but they should.)

For more information, including the rationale of why this function was added, see the PEP 553 proposal. The actual implementation was landed into Python 3.7.

like image 29
Daniel Pryden Avatar answered Oct 26 '22 13:10

Daniel Pryden