[Python-checkins] r74152 - python/trunk/Doc/reference/simple_stmts.rst

Nick Coghlan ncoghlan at gmail.com
Wed Jul 22 14:37:20 CEST 2009


benjamin.peterson wrote:
> Author: benjamin.peterson
> Date: Wed Jul 22 02:03:43 2009
> New Revision: 74152
> 
> Log:
> simplify
> 
> Modified:
>    python/trunk/Doc/reference/simple_stmts.rst
> 
> Modified: python/trunk/Doc/reference/simple_stmts.rst
> ==============================================================================
> --- python/trunk/Doc/reference/simple_stmts.rst	(original)
> +++ python/trunk/Doc/reference/simple_stmts.rst	Wed Jul 22 02:03:43 2009
> @@ -282,13 +282,13 @@
>  
>  The simple form, ``assert expression``, is equivalent to ::
>  
> -   if __debug__:
> -      if not expression: raise AssertionError
> +   if __debug__ and not expression:
> +       raise AssertionError
>  
>  The extended form, ``assert expression1, expression2``, is equivalent to ::
>  
> -   if __debug__:
> -      if not expression1: raise AssertionError(expression2)
> +   if __debug__ and not expression1:
> +       raise AssertionError(expression2)

This change is incorrect as the compiler only optimises away statements
specifically of the form "if __debug__:". As soon as you put anything
else in the expression, the compiler no longer detects it as a candidate
for elimination.

(example below was run under "python -O")

.>>> def f():
....   if __debug__:
....     return 1
....
.>>> f()
.>>> from dis import dis
.>>> dis(f)
  2           0 LOAD_CONST               0 (None)
              3 RETURN_VALUE
.>>> def f():
....   if __debug__ and 1:
....     return 1
....
.>>> f()
.>>> dis(f)
  2           0 LOAD_GLOBAL              0 (__debug__)
              3 JUMP_IF_FALSE           15 (to 21)
              6 POP_TOP
              7 LOAD_CONST               1 (1)
             10 JUMP_IF_FALSE            8 (to 21)
             13 POP_TOP

  3          14 LOAD_CONST               1 (1)
             17 RETURN_VALUE
             18 JUMP_FORWARD             1 (to 22)
        >>   21 POP_TOP
        >>   22 LOAD_CONST               0 (None)
             25 RETURN_VALUE

Cheers.
Nick

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------


More information about the Python-checkins mailing list