Author eryksun
Recipients SilentGhost, arigo, eryksun
Date 2014-07-27.12:56:27
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1406465787.31.0.167467834776.issue22091@psf.upfronthosting.co.za>
In-reply-to
Content
If __debug__ were referenced from the code object's co_consts instead of checking locals, globals and builtins (LOAD_NAME), then optimize=1 would work consistently for a given code object. 

Currently in 3.4.1:

    >>> dis.dis(compile("if __debug__: x = 1", "", "exec", optimize=0))
      1           0 LOAD_CONST               0 (1)
                  3 STORE_NAME               0 (x)
                  6 LOAD_CONST               1 (None)
                  9 RETURN_VALUE

    >>> dis.dis(compile("if __debug__: x = 1", "", "exec", optimize=1))
      1           0 LOAD_CONST               0 (None)
                  3 RETURN_VALUE

    >>> dis.dis(compile("x = __debug__", "", "exec", optimize=0))
      1           0 LOAD_NAME                0 (__debug__)
                  3 STORE_NAME               1 (x)
                  6 LOAD_CONST               0 (None)
                  9 RETURN_VALUE

    >>> dis.dis(compile("x = __debug__", "", "exec", optimize=1))
      1           0 LOAD_NAME                0 (__debug__)
                  3 STORE_NAME               1 (x)
                  6 LOAD_CONST               0 (None)
                  9 RETURN_VALUE

With the current design, an exec can override builtins.__debug__ in globals or locals:

    >>> exec("print(__debug__)", globals(), {"__debug__": "spam"})
    spam
History
Date User Action Args
2014-07-27 12:56:27eryksunsetrecipients: + eryksun, arigo, SilentGhost
2014-07-27 12:56:27eryksunsetmessageid: <1406465787.31.0.167467834776.issue22091@psf.upfronthosting.co.za>
2014-07-27 12:56:27eryksunlinkissue22091 messages
2014-07-27 12:56:27eryksuncreate