[Python-Dev] if __debug__: except Exception, e: pdb.set_trace()

Josiah Carlson jcarlson at uci.edu
Mon Oct 9 09:45:39 CEST 2006

"Calvin Spealman" <ironfroggy at gmail.com> wrote:
> I know I can not do this, but what are the chances on changing the
> rules so that we can? Basically, since the if __debug__: lines are
> processed before runtime, would it be possible to allow them to be
> used to control the inclusion or omission or entire blocks (except,
> else, elif, etc.) with them being included as if they were at the same
> level as the 'if __debug__:' above them?

I would say very low.  try/except/finally, if/elif/else, for/else,
while/else, etc., pairings of statements historically have only been
grouped together when they share indent levels.  If one makes two
statements that don't share indent levels paired in this way, then what
is stopping us from doing the following monstronsity?

if ...:
if __debug__:
    elif ...:

Remember, Special cases aren't special enough to break the rules.  This
would be a bad special case that doesn't generalize in a satisfactory

> I want to allow this:
> try:
>     foo()
> if __debug__:
>     except Exception, e:
>         import pdb
>         pdb.set_trace()
> So that when __debug__ is false, the except block doesn't even exist at all.

And if the except clause doesn't exist at all, then unless you are
following it with the finally clause of a 2.5+ unified
try/except/finally, it is a syntax error.

Regardless, it would be easier to read to have the following...

except Exception, e:
    if __debug__:
        import pdb

 - Josiah

More information about the Python-Dev mailing list