You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
global declaration in except has incorrect prior use #111123
I also don't know what the exact semantic of global/nonlocal inside statements like if/while/try/... is. I would like to know more about it because I'm currently writing pysource-codegen where I generate such cases.
With Irit's fix, this would instead be a SyntaxError.
I don't know if any code in existence would rely on this weird behavior, but we should probably fix this only on main (not the bugfix branches) and call out the change in behavior in What's New.
global is not affected by the actual order of execution. It is more "the line" in which it was declared.
The syntax error for prior use exists mainly to safe the user from thinking that he could put the global behind the use of some variable and to expect a different behavior.
Leaving out a lot of other wired uses like the if above.
A SyntaxWarning might be useful here, but I also don't know if this generated code is representative for real world problem. Hard to tell if such a SyntaxWarning could help someone.
@iritkatriel I saw your fix and I think TryStar is also affected by this bug.
Activity
sobolevn commentedon Oct 20, 2023
I feel like this issue might be interesting to @iritkatriel and @pablogsal.
iritkatriel commentedon Oct 20, 2023
I think this is because
symtable_visit_stmtvisits the else clause before the handlers:pythongh-111123: symtable should visit exception handlers before the …
JelleZijlstra commentedon Oct 20, 2023
The converse of this bug is that this code currently works (and prints 1):
With Irit's fix, this would instead be a SyntaxError.
I don't know if any code in existence would rely on this weird behavior, but we should probably fix this only on main (not the bugfix branches) and call out the change in behavior in What's New.
15r10nk commentedon Oct 21, 2023
I agree @JelleZijlstra
I think I understood how the logic behind
globalworks.output (Python 3.12.0):
global is not affected by the actual order of execution. It is more "the line" in which it was declared.
The syntax error for prior use exists mainly to safe the user from thinking that he could put the global behind the use of some variable and to expect a different behavior.
Leaving out a lot of other wired uses like the
ifabove.A
SyntaxWarningmight be useful here, but I also don't know if this generated code is representative for real world problem. Hard to tell if such aSyntaxWarningcould help someone.@iritkatriel I saw your fix and I think
TryStaris also affected by this bug.iritkatriel commentedon Oct 21, 2023
Right, thanks.
gh-111123: symtable should visit exception handlers before the else b…
fix: workaround for python/cpython#111123
fix: workaround for python/cpython#111123
fix: workaround for python/cpython#111123
fix: workaround for python/cpython#111123
pythongh-111123: symtable should visit exception handlers before the …
pythongh-111123: symtable should visit exception handlers before the …
[syntax-errors] Document behavior of `global` declarations in `try` n…
[syntax-errors] Document behavior of `global` declarations in `try` n…
[syntax-errors] Document behavior of `global` declarations in `try` n…