[Python-Dev] PEP 553

Barry Warsaw barry at python.org
Wed Oct 4 20:50:47 EDT 2017


On Oct 4, 2017, at 20:22, Yarko Tymciurak <yarkot1 at gmail.com> wrote:

> I've recently started using a simple conditional breakpoint in ipython, and wonder if  - in addition to Nick Coghlan's request for the env 'PYTHONBREAKPOINT'  (boolean?), it would make sense (I _think_ so) to add a condition parameter to the breakpoint() call.  This does raise several questions, but it seems that it could make for a simple unified way to conditionally call an arbitrary debugger.  What I found useful (in the contecxt of ipython - but general enough) you can see in this gist: https://gist.github.com/yarko/bdaa9d3178a6db03e160fdbabb3a9885
> 
> If PEP 553's breakpoint() were to follow this sort of interface (with "condition"), it raises a couple of questions:
> - how would a missing (default) parameter be done?
> - how would parameters to be passed to the debugger "of record" be passed in (named tuple? - sort of ugly)
> - would PYTHONBREAKPOINT be a global switch (I think yes), vs a `condition` default.
> 
> I have no dog in the fight, but to raise the possibility (?) of having PEP 553 implement simple conditional breakpoint processing.

Thanks for bringing this up Yarko.  I think this could be done with the current specification for PEP 553 and an additional API from the various debuggers.  I don’t think it needs to be part of PEP 553 explicitly, given the additional complications you describe above.

Remember that both built-in breakpoint() and sys.breakpointhook() accept *args, **kws, and it is left up to the actual debugger API to interpret/accept those additional arguments.  So let’s say you wanted to implement this behavior with pdb.  I think you could do something as simple as:

def conditional_set_trace(*, condition=True):
    if condition:
        pdb.set_trace()

sys.breakpointhook = conditional_set_trace

Then in your code, you would just write:

def foo(value):
    breakpoint(condition=(value < 0))

With the IPython gist you referenced, you wouldn’t even need that convenience function.  Just set sys.breakpointhook=conditional_breakpoint.breakpoint_ and viola!

You could also PYTHONBREAKPOINT=conditional_breakpoint.breakpoint_ python3.7 … and it should Just Work.

Cheers,
-Barry

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: Message signed with OpenPGP
URL: <http://mail.python.org/pipermail/python-dev/attachments/20171004/a8c6e066/attachment.sig>


More information about the Python-Dev mailing list