New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SyntaxError for walrus target in a comprehension when the target is a global variable with a private name. #96497
Comments
I think the fix for this is to mangle the name before looking it up here Line 1510 in 8464b75
and here Line 1495 in 8464b75
This seems to fix your code above, and also another related bug. class F:
def a(self):
__x = None
y = [[(__x:=2) for _ in range(2)] for __x in range(2)]
print(y, __x)
F().a() Currently outputs With the proposed change it would error. y = [[(__x:=2) for _ in range(2)] for __x in range(2)]
^^^
SyntaxError: assignment expression cannot rebind comprehension iteration variable '__x' I'll look into opening a PR to make this change. |
Thanks for prompt response. I'm curious... How come _PyST_GetSymbol is being called with the unmangled name in the first place? I thought the name already got mangled when the NamedExpr was examined. |
Which versions of cpython can this fix be applied to? 3.9 would be nice for me, since that's what I am using. |
Reopening because the issue is not fixed yet, there is a PR but that hasn't been merged (or even reviewed) |
My understanding is that the name is accessed directly from the AST here, and the names are not mangled in the AST: Lines 1571 to 1573 in 83539c0
Instead the mangling happens when the names are inserted into the symtable: Line 1035 in 83539c0
So to mirror that whenever looking up a name from the ast in the symbol table you also need to mangle it (if necessary) first. This is similar how it was already done with the
I don't think that would happen as 3.9 is now security-fix only (see the release schedule PEP and status of python versions). |
Bug report
If I have a walrus target with a private name, contained in a comprehension, the compiler mangles the name just fine.
However, if the name is declared global in a function containing the comprehension, the walrus is supposed to assign to the global variable with the mangled name.
Instead, I get a SyntaxError. BTW, if I use the mangled name instead of the private name in the walrus, it works fine.
Example:
Line 4 correctly assigns the global variable
_C__x
= 0, and line 5 assigns it = 1.Disassembly of this program, without line 5:
The text was updated successfully, but these errors were encountered: