[ python-Bugs-1501934 ] incorrect LOAD/STORE_GLOBAL generation

SourceForge.net noreply at sourceforge.net
Sun Jul 9 18:17:21 CEST 2006


Bugs item #1501934, was opened at 2006-06-06 23:57
Message generated for change (Comment added) made by nascheme
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1501934&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Parser/Compiler
Group: Python 2.5
>Status: Closed
>Resolution: Fixed
Priority: 8
Submitted By: Thomas Wouters (twouters)
>Assigned to: Neil Schemenauer (nascheme)
Summary: incorrect LOAD/STORE_GLOBAL generation

Initial Comment:
Python 2.5 compiles the following piece of code
differently than Python 2.4:

g = 1
def f():
    g += 1

In Python 2.4, this raises an UnboundLocalError. In
current svn trunk, it will increment the global g by 1.
(dis.dis shows that it actually compiles into
LOAD/STORE_GLOBAL opcodes.) It seems the compiler
doesn't treat augmented assignment as assignment for
the purpose of determining locals, as this still fails
correctly:

g = 1
def f():
    g += 1
    g = 5

I can't find where this optimization happens nowadays,
but it feels like a short fix.


----------------------------------------------------------------------

>Comment By: Neil Schemenauer (nascheme)
Date: 2006-07-09 16:17

Message:
Logged In: YES 
user_id=35752

Checked in as SVN rev 50493.

----------------------------------------------------------------------

Comment By: Neil Schemenauer (nascheme)
Date: 2006-06-30 22:03

Message:
Logged In: YES 
user_id=35752

Adding a patch to "fix" test_ast.py.  I have no idea what
the test is trying to verify.  It looks like it was written
by martin.v.loewis so maybe he can comment.

----------------------------------------------------------------------

Comment By: Neil Schemenauer (nascheme)
Date: 2006-06-30 17:51

Message:
Logged In: YES 
user_id=35752

I've got a simple fix that seems to work.  I feel this part
of the compiler could use some more serious cleanups but
probably not for 2.5.  Note that test_ast fails after
applying my patch.  I haven't had time to look into that yet
but I think it's shallow.

----------------------------------------------------------------------

Comment By: Neil Schemenauer (nascheme)
Date: 2006-06-30 16:22

Message:
Logged In: YES 
user_id=35752

Here are some notes in case I wear out before finding a fix.
 analyze_name() gets to the SET_SCOPE(dict, name,
GLOBAL_IMPLICIT) line because the name does not have the
DEF_LOCAL flag set as it should.  It does not have DEF_LOCAL
because Name.ctx for 'g' is Load.  I believe there should be
a set_context() call in ast_for_expr_stmt, as flagged as
TODO by  Jeremy.  Maybe set_context(..., Store, ...) would
work or maybe things need to be changed to allow ctx to have
AugAssign as a value.

----------------------------------------------------------------------

Comment By: Thomas Wouters (twouters)
Date: 2006-06-19 17:44

Message:
Logged In: YES 
user_id=34209

Possibly related is the discovery of free variables (used
when forming closures) and optimized-out codeblocks:

>>> def foo(x):
...     def bar():
...         if 0:
...             print x
...     return bar

In 2.4, there is no closure:
>>> foo.func_code.co_cellvars
()
>>> foo(5).func_closure
>>>

In 2.5, there is:
>>> foo.func_code.co_cellvars
('x',)
>>> foo(5).func_closure
(<cell at 0x2b9abf6d7e30: int object at 0x6b6580>,)

(I don't think it's unreasonable to declare the old
behaviour bugged, though :-)


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1501934&group_id=5470


More information about the Python-bugs-list mailing list