1

A similar question

When developing some Windows desktop stuff in Delphi, I had a habit of inserting
asm(int 3);.

If a debugger is running, that acts like a breakpoint. If no debugger is running, it does nothing (NOOP).

I would like to do something similar for my Python scripts. Is there some Python function like HaltDebugger()? Cross platform, of course.


[Update] I am looking for something that will work with PyCharm

Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551

1 Answers1

2

This is the most portable solution. A more difficult one is sketched below for PyCharm.

Kind of depends on the debugger, but if you use pdb (which would be cross platform), the docs state:

The typical usage to break into the debugger from a running program is to insert

import pdb; pdb.set_trace()

you want this to be conditional, so you can paste at each breakpoint:

try: pdb.set_trace()
except NameError: pass

and when you want to debug just import pdb at the top. If it must be one line you cannot use duck typing. Instead:

if 'pdb' in globals(): pdb.set_trace()

PyCharm only

Assuming you insist on not marking debugging lines with the mouse this might work:

Using exception breakpoints:

PyCharm provides exception breakpoints for Python, Django, and JavaScript.

Exception breakpoints are triggered when the specified exception is thrown. Unlike the line breakpoints, which require specific source references, exception breakpoints apply globally to the exception condition, rather than to a particular code reference.

Depending on the type of processing an exception, the debugging can break when a process terminates with an exception, or as soon as an exception occurs.

You could:

  1. Create a custom exception in your project, DebugException
  2. Set the exception breakpoint as per the link above. Make sure it is set to trigger immediately, not when program exits.
  3. Finally,

paste

    try: raise DebugException()
    Exception: pass

wherever you want to break. This seems like a lot of trouble to not double click with your mouse to mark a breakpoint.

kabanus
  • 24,623
  • 6
  • 41
  • 74
  • My bad. I forgot to say that I am working with PyCharm (but upvote for a good answer). I want this to act like I hit a breakpoint. When I use that in the PyCharm debugger, I get `PYDEV DEBUGGER WARNING: sys.settrace() should not be used when the debugger is being used. This may cause the debugger to stop working correctly.` and no pseudo-breakpoint :-( – Mawg says reinstate Monica Jul 31 '18 at 12:37
  • Maybe I don't understand the question about PyCharm...? In PyCharm just click between the line number and line content to set the breakpoint. However the "import pdb; pdb.set_trace()" style should be perfectly valid too. In Py3.7+ you can use breakpoint() instead. – mirek Jul 31 '18 at 12:47
  • @Mawg I second what mirek said, I added a way that might work, you are welcome to try it and let us know how it goes, but I'm not sure it's easier than just using the mouse. – kabanus Jul 31 '18 at 12:59
  • The idea is that I have lots of exception handling, once per function, for a few dozen functions. I would like the debugger to halt, as if it had hit a breakpoint, whenever I catch an exception. I also want to be able to delete all breakpoints without having to manually reset breakpoints in all exceptioo handlers – Mawg says reinstate Monica Jul 31 '18 at 13:35
  • 1
    @Mawg Well, the exception thing is in the docs I linked, you could use that to catch on every exception throw relatively simply. Deleting all breakpoints can be done from the breakpoint dialog box. Maybe play around with the in application dialogues and see if something fits. – kabanus Jul 31 '18 at 13:39
  • I can see that that will work - after a one time update to my code - and thank you for it. If nothing better comes up, this will certainly be the accepted answer. Thanks – Mawg says reinstate Monica Jul 31 '18 at 13:47