[ python-Bugs-1167751 ] Argument genexp corner case

SourceForge.net noreply at sourceforge.net
Tue Oct 11 06:10:45 CEST 2005


Bugs item #1167751, was opened at 2005-03-21 09:47
Message generated for change (Comment added) made by nnorwitz
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1167751&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: Open
Resolution: None
>Priority: 8
Submitted By: John Ehresman (jpe)
Assigned to: Nobody/Anonymous (nobody)
Summary: Argument genexp corner case

Initial Comment:
The following raises an unexpected exception; I would
expect it to either work or raise a SyntaxError.  It
seems that the grammar parses it, but the compiler does
not do the right thing.

>>> def foo(a): pass

>>> foo(a = i for i in range(10))

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'i' is not defined

foo(a = (i for i in range(10)) works.

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

>Comment By: Neal Norwitz (nnorwitz)
Date: 2005-10-10 21:10

Message:
Logged In: YES 
user_id=33168

I definitely agree this is a big problem.

Here's what the code above generates:
2           0 LOAD_GLOBAL              0 (foo)
              3 LOAD_CONST               1 ('a')
              6 LOAD_GLOBAL              1 (i)
              9 CALL_FUNCTION          256
             12 POP_TOP
             13 LOAD_CONST               0 (None)
             16 RETURN_VALUE

If I put parens around the genexp, I get:
2           0 LOAD_GLOBAL              0 (foo)
              3 LOAD_CONST               1 ('a')
              6 LOAD_CONST               2 (<code object
<generator expression> at 0x2a960baae8, file "<stdin>", line 2>)
              9 MAKE_FUNCTION            0
             12 LOAD_GLOBAL              1 (range)
             15 LOAD_CONST               3 (10)
             18 CALL_FUNCTION            1
             21 GET_ITER
             22 CALL_FUNCTION            1
             25 CALL_FUNCTION          256
             28 POP_TOP
             29 LOAD_CONST               0 (None)
             32 RETURN_VALUE


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

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


More information about the Python-bugs-list mailing list