Skip to content

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

Merged
merged 5 commits into from
Oct 26, 2017

Conversation

ilevkivskyi
Copy link
Member

@ilevkivskyi ilevkivskyi commented Oct 23, 2017

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a 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.
Copy link
Member

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."

@@ -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;
Copy link
Member

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?

@@ -467,8 +473,7 @@ analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
if (flags & DEF_GLOBAL) {
if (flags & DEF_PARAM) {
Copy link
Member

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?

Copy link
Member Author

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.

@ilevkivskyi
Copy link
Member Author

@serhiy-storchaka Thanks for review! I also added the doctests you mentioned.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a 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.

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):
Copy link
Member

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.

def warning():
def error2():
b = 1
global b
Copy link
Member

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.
Copy link
Member

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.

@ilevkivskyi
Copy link
Member Author

@serhiy-storchaka Thanks! I fixed the remaining things.

@serhiy-storchaka serhiy-storchaka merged commit 8c83c23 into python:master Oct 26, 2017
@ilevkivskyi ilevkivskyi deleted the fix-globak-first branch October 26, 2017 21:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants