Skip to content
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

bpo-40726: handle uninitalized end_lineno on ast.increment_lineno #20312

Merged
merged 2 commits into from
Aug 5, 2020

Conversation

isidentical
Copy link
Member

@isidentical isidentical commented May 22, 2020

end_lineno is declared as an optional attribute on the ASDL spec. This patch makes increment_lineno aware that node's end_lineno might be None.

https://bugs.python.org/issue40726

@laloch
Copy link

laloch commented May 22, 2020

I think this also needs some treatment:

cpython/Lib/ast.py

Lines 180 to 184 in c102a14

for attr in 'lineno', 'col_offset', 'end_lineno', 'end_col_offset':
if attr in old_node._attributes and attr in new_node._attributes:
value = getattr(old_node, attr, None)
if value is not None:
setattr(new_node, attr, value)

@remilapeyre
Copy link
Contributor

Hi @isidentical, thanks for fixing this.

It seems to me that if all nodes have all their attributes defined, the calls to getattr() and hasattr() should be removed all together, for example:

        if (
            "end_lineno" in child._attributes
            and (end_lineno := getattr(child, "end_lineno", 0)) is not None
        ):
            child.end_lineno = end_lineno + n

could be

        if child.end_lineno is not None:
            child.end_lineno += n

?

@isidentical
Copy link
Member Author

I think this also needs some treatment:

Sorry, but I couldn't catch the reason. There is already a verification for None in there

cpython/Lib/ast.py

Lines 183 to 184 in c102a14

if value is not None:
setattr(new_node, attr, value)

It seems to me that if all nodes have all their attributes defined, the calls to getattr() and hasattr() should be removed all together, for example:

That is just the current implementation, and in theory it might not exist at all as-well, which would prevent us from backporting this patch to 3.8 as it is.

@isidentical isidentical requested a review from pablogsal May 22, 2020 11:17
@laloch
Copy link

laloch commented May 22, 2020

I think this also needs some treatment:

Sorry, but I couldn't catch the reason. There is already a verification for None in there

Sorry, I should have been more verbose. I know this is not a real-world use case, but here's an example:

>>> new = ast.Call(end_lineno=1); old = ast.Call()
>>> type(old.end_lineno)
<class 'NoneType'>
>>> ast.copy_location(new, old)
<ast.Call object at 0x7fb9931cd5e0>
>>> new.end_lineno
1

In the end, the value of new.end_lineno is expected to be None.

@isidentical
Copy link
Member Author

I think this also needs some treatment:

Sorry, but I couldn't catch the reason. There is already a verification for None in there

Sorry, I should have been more verbose. I know this is not a real-world use case, but here's an example:

>>> new = ast.Call(end_lineno=1); old = ast.Call()
>>> type(old.end_lineno)
<class 'NoneType'>
>>> ast.copy_location(new, old)
<ast.Call object at 0x7fb9931cd5e0>
>>> new.end_lineno
1

In the end, the value of new.end_lineno is expected to be None.

Oh, this is an interesting case. Nice catch!

@isidentical isidentical force-pushed the bpo-40726 branch 2 times, most recently from 7ab5685 to 2444b63 Compare May 22, 2020 13:10
@laloch
Copy link

laloch commented May 22, 2020

I can confirm that this does fix xonsh/xonsh#3581.
Thank you @isidentical!

@laloch
Copy link

laloch commented Aug 5, 2020

Is this planed to get merged for v3.9.0? This currently blocks Python 3.9 adoption in Xonsh.

@miss-islington
Copy link
Contributor

Thanks @isidentical for the PR, and @pablogsal for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8, 3.9.
🐍🍒⛏🤖

@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Aug 5, 2020
@bedevere-bot
Copy link

GH-21741 is a backport of this pull request to the 3.9 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Aug 5, 2020
…thonGH-20312)

(cherry picked from commit 8f4380d)

Co-authored-by: Batuhan Taskaya <batuhanosmantaskaya@gmail.com>
@miss-islington
Copy link
Contributor

Sorry, @isidentical and @pablogsal, I could not cleanly backport this to 3.8 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 8f4380d2f5839a321475104765221a7394a9d649 3.8

@bedevere-bot
Copy link

GH-21742 is a backport of this pull request to the 3.8 branch.

@pablogsal
Copy link
Member

Is this planed to get merged for v3.9.0? This currently blocks Python 3.9 adoption in Xonsh.

I think we are still on time to get this into 3.9 but we should confirm with @ambv

@laloch
Copy link

laloch commented Aug 5, 2020

Thanks @pablogsal!

miss-islington added a commit that referenced this pull request Aug 5, 2020
…-20312)

(cherry picked from commit 8f4380d)

Co-authored-by: Batuhan Taskaya <batuhanosmantaskaya@gmail.com>
pablogsal pushed a commit to pablogsal/cpython that referenced this pull request Aug 5, 2020
…no (pythonGH-20312).

(cherry picked from commit 8f4380d)

Co-authored-by: Batuhan Taskaya <batuhanosmantaskaya@gmail.com>
isidentical added a commit to isidentical/cpython that referenced this pull request Aug 5, 2020
…no (pythonGH-20312).

(cherry picked from commit 8f4380d)

Co-authored-by: Batuhan Taskaya <batuhanosmantaskaya@gmail.com>
miss-islington pushed a commit that referenced this pull request Aug 5, 2020
…no (GH-21745)

…no (GH-20312).

(cherry picked from commit 8f4380d)

Co-authored-by: Batuhan Taskaya <batuhanosmantaskaya@gmail.com>

Automerge-Triggered-By: @pablogsal
@ambv ambv added the needs backport to 3.9 only security fixes label Aug 11, 2020
@miss-islington
Copy link
Contributor

Thanks @isidentical for the PR, and @pablogsal for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry, @isidentical and @pablogsal, I could not cleanly backport this to 3.9 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 8f4380d2f5839a321475104765221a7394a9d649 3.9

@ambv
Copy link
Contributor

ambv commented Aug 11, 2020

Ah, never mind, #21741 is the backport that's already merged.

@ambv ambv removed the needs backport to 3.9 only security fixes label Aug 11, 2020
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.

8 participants