Skip to content

bpo-28936: Detect lexically first syntax error first#4097

Merged
serhiy-storchaka merged 5 commits into
python:masterfrom
ilevkivskyi:fix-globak-first
Oct 26, 2017
Merged

bpo-28936: Detect lexically first syntax error first#4097
serhiy-storchaka merged 5 commits into
python:masterfrom
ilevkivskyi:fix-globak-first

Conversation

@ilevkivskyi

@ilevkivskyi ilevkivskyi commented Oct 23, 2017

Copy link
Copy Markdown
Member

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

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

Comment thread Python/symtable.c Outdated
VISIT_QUIT(st, 0);
if (cur & (DEF_LOCAL | USE | DEF_ANNOT)) {
if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
char* msg;

Copy link
Copy Markdown
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?

Comment thread 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) {

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member Author

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

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

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.

Comment thread Lib/test/test_syntax.py

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.

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

Comment thread Lib/test/test_syntax.py
global a # SyntaxError
def warning():
def error2():
b = 1

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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