-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-28936: Detect lexically first syntax error first #4097
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
Conversation
…ld be detected first
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are doctests for misusing of the nonlocal and global statement at lines 400-438. It seems to me that the case "name '%U' is parameter and global" is not covered here. It is worth to add a doctest for it and for other cases if there are other missed cases.
@@ -0,0 +1,2 @@ | |||
Ensure that lexically first syntax error involving ``global`` or | |||
``nonlocal`` is detected first at a given scope. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add "Patch by Ivan Levkivskyi."
Python/symtable.c
Outdated
@@ -1284,9 +1288,11 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s) | |||
long cur = symtable_lookup(st, name); | |||
if (cur < 0) | |||
VISIT_QUIT(st, 0); | |||
if (cur & (DEF_LOCAL | USE | DEF_ANNOT)) { | |||
if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) { | |||
char* msg; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we are here, maybe add the "const" qualifier?
Python/symtable.c
Outdated
@@ -467,8 +473,7 @@ analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags, | |||
if (flags & DEF_GLOBAL) { | |||
if (flags & DEF_PARAM) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this check still needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, indeed this looks redundant. Initially I thought that it is used to prohibit:
def f(x):
def g():
global x
But this is not an error, and after thinking more it looks like it should not be an error, since global
and nonlocal
have unwanted interference with being parameter only in the same scope.
I therefore remove this check.
@serhiy-storchaka Thanks for review! I also added the doctests you mentioned. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you Ivan. Now I completely understand what this patch does, and it LGTM.
Added few minor comments.
Lib/test/test_syntax.py
Outdated
def test_global_err_then_warn(self): | ||
# Bug #763201: The SyntaxError raised for one global statement | ||
# shouldn't be clobbered by a SyntaxWarning issued for a later one. | ||
def test_global_err_first(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is better to name this test test_global_param_err_first
since it is related to using global
with a parameter.
Lib/test/test_syntax.py
Outdated
def warning(): | ||
def error2(): | ||
b = 1 | ||
global b |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add # SyntaxError
?
@@ -0,0 +1,2 @@ | |||
Ensure that lexically first syntax error involving ``global`` or | |||
``nonlocal`` is detected first at a given scope. Patch by Ivan Levkivskyi. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mention that this is specific to parameters? Other errors already are detected first.
@serhiy-storchaka Thanks! I fixed the remaining things. |
Attn: @serhiy-storchaka
https://bugs.python.org/issue28936