Message99978
I also notice that the Grammar in the PEP is more complicated:
nonlocal_stmt ::=
"nonlocal" identifier ("," identifier)*
["=" (target_list "=")+ expression_list]
| "nonlocal" identifier augop expression_list
The Grammar in the patch is:
+global_stmt: 'global' NAME (',' NAME)* [','] ['=' testlist]
+nonlocal_stmt: 'nonlocal' NAME (',' NAME)* [','] ['=' testlist]
It appears that the PEP is trying to support:
nonlocal x = y = z = 1
nonlocal a, b = c, d = 1
If we're going to support the PEP as written, I think we need to
modify Global() and Nonlocal() to look exactly like Assign(), but add
an extra check to verify that all of the expressions in the targets
are Name, List, or Tuple. You'd probably want to check this at the
time you are generating the AST, so that you're not stuck with some
extra state in the compiler traversal about whether you are generating
code for a Global() or an Assign().
This approach makes the compiler code very simple. We use exactly the
same code for Global(), Nonlocal(), and Assign(). It does have the
downside that you need to enforce this unwritten constraint of the AST
in ast.c and in the ast module.
It also means that the AST will change in a non-backwards compatible
way. I don't see how to do that given that we're also changing the
language spec. (Do you want to include the spec change in your
patch?)
Jeremy
On Tue, Feb 23, 2010 at 6:41 PM, Jeremy Hylton <jeremy@alum.mit.edu> wrote:
> On Sat, Dec 6, 2008 at 1:28 PM, Benjamin Peterson
> <report@bugs.python.org> wrote:
>>
>> Benjamin Peterson <musiccomposition@gmail.com> added the comment:
>>
>> I think I may have been merging add_ast_fields when I wrote the patch.
>>
>> Here's a new patch that handles "global x, = (5,)". To do it, I added a
>> new flag* to the Global and Nonlocal AST objects that indicates whether
>> the value needs to be unpacked or not.
>
> You shouldn't need to do this. The unpack is implied if the number of
> identifiers is greater than 1.
>
> Jeremy
>
>
>
>> * The flag is an int. The bool AST type was removed in py3k.
>>
>> Added file: http://bugs.python.org/file12254/global_nonlocal_shorthand2.patch
>>
>> _______________________________________
>> Python tracker <report@bugs.python.org>
>> <http://bugs.python.org/issue4199>
>> _______________________________________
>> _______________________________________________
>> Python-bugs-list mailing list
>> Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/jeremy%40alum.mit.edu
>>
>>
> |
|
Date |
User |
Action |
Args |
2010-02-23 23:50:09 | jhylton | set | recipients:
+ jhylton, akuchling, georg.brandl, amaury.forgeotdarc, benjamin.peterson |
2010-02-23 23:50:08 | jhylton | link | issue4199 messages |
2010-02-23 23:50:06 | jhylton | create | |
|