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

2006-06-07 Thread Armin Rigo
Hi,

On Wed, Jun 07, 2006 at 02:07:48AM +0200, Thomas Wouters wrote:
> 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:

No, no, it's an underground move by Jeremy to allow assignment to
variables of enclosing scopes:

[in 2.5]
def f():
x = 5
def g():
x += 1
g()
print x # 6

The next move is to say that this is an expected feature of the
augmented assignment operators, from which it follows naturally that we
need a pseudo-augmented assignment that doesn't actually read the old
value:

[in 2.6]
def f():
x = 5
def g():
x := 6
g()
print x# 6

Credits to Samuele's evil side for the ideas.  His non-evil side doesn't
agree, and neither does mine, of course :-)

More seriously, a function with a variable that is only written to as
the target of augmented assignments cannot possibly be something else
than a newcomer's mistake: the augmented assignments will always raise
UnboundLocalError.  Maybe this should be a SyntaxWarning?


A bientot,

Armin
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


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

2006-06-06 Thread Thomas Wouters
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()>>> g2It 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 "", line 1, in   File "", line 2, in f2
UnboundLocalError: local variable 'g' referenced before assignmentThe 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 <
[EMAIL PROTECTED]>Hi! I'm a .signature virus! copy me into your .signature file to help me spread!
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com