[Python-Dev] 'fast locals' in Python 2.5

Thomas Wouters thomas at python.org
Wed Jun 7 02:07:48 CEST 2006

I just submitted http://python.org/sf/1501934 and assigned it to Neal so it
doesn't get forgotten before 2.5 goes out ;) It seems Python 2.5 compiles
the following code incorrectly:

>>> g = 1
>>> def f1():
...     g += 1
>>> f1()
>>> g

It looks like the compiler is not seeing augmented assignment as creating a
local name, as this fails properly:

>>> def f2():
...     g += 1
...     g = 5
>>> f2()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in f2
UnboundLocalError: local variable 'g' referenced before assignment

The dis.dis output confirms this:
>>> dis.dis(f1)
  1           0 LOAD_GLOBAL              0 (g)
              3 LOAD_CONST               1 (1)
              6 INPLACE_ADD
              7 STORE_GLOBAL             0 (g)
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE

If anyone feels like fixing it and happens to remember where the new
compiler does the fast-locals optimization (I recall a few people were
working on extra optimizations and all), please do :-) (I can probably look
at it before 2.5 if no one else does, though.) It may be a good idea to
check for more such cornercases while we're at it (but I couldn't find any
in the fast-locals bit.)

Thomas Wouters <thomas at python.org>

Hi! I'm a .signature virus! copy me into your .signature file to help me
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20060607/d79597bd/attachment.html 

More information about the Python-Dev mailing list