[Python-Dev] Assigning to __debug__
Guido van Rossum
guido@digicool.com
Fri, 30 Mar 2001 08:30:18 -0500
> After the recent change that assignments to __debug__ are disallowed,
> I noticed that IDLE stops working (see SF bug report), since it was
> assigning to __debug__.
I checked in a fix to IDLE too, but it seems you were using an
externally-installed version of IDLE.
> Simply commenting-out the assignment (to zero) did no good: Inside the
> __debug__ blocks, IDLE would try to perform print statements, which
> would write to the re-assigned sys.stdout, which would invoke the code
> that had the __debug__, which would give up thanks to infinite
> recursion. So essentially, you either have to remove the __debug__
> blocks, or rewrite them to writing to save_stdout - in which case all
> the ColorDelegator debug message appear in the terminal window.
IDLE was totally abusing the __debug__ variable -- in the fix, I
simply changed all occurrences of __debug__ to DEBUG.
> So anybody porting to Python 2.1 will essentially have to remove all
> __debug__ blocks that were previously disabled by assigning 0 to
> __debug__. I think this is undesirable.
Assigning to __debug__ was never well-defined. You used it at your
own risk.
> As I recall, in the original description of __debug__, being able to
> assign to it was reported as one of its main features, so that you
> still had a run-time option (unless the interpreter was running with
> -O, which eliminates the __debug__ blocks).
The manual has always used words that suggest that there is something
special about __debug__. And there was: the compiler assumed it could
eliminate blocks started with "if __debug__:" when compiling in -O
mode. Also, assert statements have always used LOAD_GLOBAL to
retrieve the __debug__ variable.
> So in short, I think this change should be reverted.
It's possible that it breaks more code, and it's possible that we end
up having to change the error into a warning for now. But I insist
that assignment to __debug__ should become illegal. You can *use* the
variable (to determine whether -O is on or not), but you can't *set*
it.
> Regards,
> Martin
>
> P.S. What was the motivation for that change, anyway?
To enforce a restriction that was always intended: __debug__ should be
a read-only variable.
--Guido van Rossum (home page: http://www.python.org/~guido/)