[ python-Bugs-1186195 ] [AST] genexps get scoping wrong

SourceForge.net noreply at sourceforge.net
Tue Apr 19 21:02:15 CEST 2005


Bugs item #1186195, was opened at 2005-04-19 12:02
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1186195&group_id=5470

Category: Parser/Compiler
Group: AST
Status: Open
Resolution: None
Priority: 5
Submitted By: Brett Cannon (bcannon)
Assigned to: Nick Coghlan (ncoghlan)
Summary: [AST] genexps get scoping wrong

Initial Comment:
test_genexps is failing because it is unable to find a
global defined in a genexp that is returned.  Here is
the problem simplified:

def f(n): return (i for i in xrange(n))
list(f(10))

Leads to ``SystemError: no locals when loading 'xrange'``.

Comparing Python 2.4 bytecode:

  1           0 LOAD_CONST               1 (<code
object <generator expression> at 0x3931e0, file
"<stdin>", line 1>)
              3 MAKE_FUNCTION            0
              6 LOAD_GLOBAL              0 (xrange)
              9 LOAD_FAST                0 (n)
             12 CALL_FUNCTION            1
             15 GET_ITER            
             16 CALL_FUNCTION            1
             19 RETURN_VALUE        
             20 LOAD_CONST               0 (None)
             23 RETURN_VALUE        

to AST bytecode:

  1           0 LOAD_CLOSURE             0 (n)
              3 BUILD_TUPLE              1
              6 LOAD_CONST               1 (<code
object <generator expression> at 0x5212e8, file
"<stdin>", line 1>)
              9 MAKE_CLOSURE             0
             12 LOAD_NAME                0 (xrange)
             15 LOAD_DEREF               0 (n)
             18 CALL_FUNCTION            1
             21 GET_ITER            
             22 CALL_FUNCTION            1
             25 RETURN_VALUE        
             26 LOAD_CONST               0 (None)
             29 RETURN_VALUE

makes it obvious something is off (no peepholer; turned
it off in my build of 2.4).

Looks like extraneous work is being done in making a
closure.  Seems like it will still work, though.

Plus the usage of LOAD_NAME is wrong in the AST;
LOAD_NAME gets an object from the local namespace based
on its name instead of offset.


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

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


More information about the Python-bugs-list mailing list