Skip to content

bpo-18374: fix wrong col_offset of some ast.BinOp instances#14607

Merged
ilevkivskyi merged 1 commit into
python:masterfrom
cfbolz:bpo-18374-fix-col-offset-binop
Jul 8, 2019
Merged

bpo-18374: fix wrong col_offset of some ast.BinOp instances#14607
ilevkivskyi merged 1 commit into
python:masterfrom
cfbolz:bpo-18374-fix-col-offset-binop

Conversation

@cfbolz

@cfbolz cfbolz commented Jul 5, 2019

Copy link
Copy Markdown
Contributor

Nested BinOp instances (e.g. a+b+c) had a wrong col_offset for the second BinOp (e.g. 2 instead of 0 in the example). Fix it by using the correct st node to copy the line and col_offset from in ast.c.

https://bugs.python.org/issue18374

Nested BinOp instances (e.g. a+b+c) had a wrong col_offset for the
second BinOp (e.g. 2 instead of 0 in the example). Fix it by using the
correct st node to copy the line and col_offset from in ast.c.
@the-knights-who-say-ni

Copy link
Copy Markdown

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Our records indicate we have not received your CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for your contribution, we look forward to reviewing it!

@asottile asottile left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the output looks correct

validating this change using astpretty

before

$ astpretty /dev/stdin <<< '1 + 2 + 3'
Module(
    body=[
        Expr(
            lineno=1,
            col_offset=0,
            end_lineno=1,
            end_col_offset=9,
            value=BinOp(
                lineno=1,
                col_offset=6,
                end_lineno=1,
                end_col_offset=9,
                left=BinOp(
                    lineno=1,
                    col_offset=0,
                    end_lineno=1,
                    end_col_offset=5,
                    left=Constant(lineno=1, col_offset=0, end_lineno=1, end_col_offset=1, value=1, kind=None),
                    op=Add(),
                    right=Constant(lineno=1, col_offset=4, end_lineno=1, end_col_offset=5, value=2, kind=None),
                ),
                op=Add(),
                right=Constant(lineno=1, col_offset=8, end_lineno=1, end_col_offset=9, value=3, kind=None),
            ),
        ),
    ],
    type_ignores=[],
)

roughly looks like this

1 + 2 + 3
      AAA        # outer BinOp
BBBBB            # inner BinOp

after

$ astpretty /dev/stdin <<< '1 + 2 + 3'
Module(
    body=[
        Expr(
            lineno=1,
            col_offset=0,
            end_lineno=1,
            end_col_offset=9,
            value=BinOp(
                lineno=1,
                col_offset=0,
                end_lineno=1,
                end_col_offset=9,
                left=BinOp(
                    lineno=1,
                    col_offset=0,
                    end_lineno=1,
                    end_col_offset=5,
                    left=Constant(lineno=1, col_offset=0, end_lineno=1, end_col_offset=1, value=1, kind=None),
                    op=Add(),
                    right=Constant(lineno=1, col_offset=4, end_lineno=1, end_col_offset=5, value=2, kind=None),
                ),
                op=Add(),
                right=Constant(lineno=1, col_offset=8, end_lineno=1, end_col_offset=9, value=3, kind=None),
            ),
        ),
    ],
    type_ignores=[],
)

roughly looks like this

1 + 2 + 3
AAAAAAAAA        # outer BinOp
BBBBB            # inner BinOp

@ilevkivskyi

Copy link
Copy Markdown
Member

@cfbolz Did you sign the CLA?

@cfbolz

cfbolz commented Jul 8, 2019

Copy link
Copy Markdown
Contributor Author

@ilevkivskyi yes, signed it last Friday when I opened the pull request.

@ilevkivskyi ilevkivskyi 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.

Thanks! LGTM.

@miss-islington

Copy link
Copy Markdown
Contributor

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

@bedevere-bot

Copy link
Copy Markdown

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

@miss-islington

Copy link
Copy Markdown
Contributor

Sorry, @cfbolz and @ilevkivskyi, I could not cleanly backport this to 3.7 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 110a47c4f42cf4db88edc1876899fff8f05190fb 3.7

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 8, 2019
…-14607)

Nested BinOp instances (e.g. a+b+c) had a wrong col_offset for the
second BinOp (e.g. 2 instead of 0 in the example). Fix it by using the
correct st node to copy the line and col_offset from in ast.c.
(cherry picked from commit 110a47c)

Co-authored-by: Carl Friedrich Bolz-Tereick <cfbolz@gmx.de>
Comment thread Lib/test/test_ast.py
self.assertEqual(child_binop.col_offset, 0)
self.assertEqual(parent_binop.lineno, 1)
self.assertEqual(child_binop.end_col_offset, 2)
self.assertEqual(parent_binop.end_lineno, 2)

@ilevkivskyi ilevkivskyi Jul 8, 2019

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.

Oh, after merging this I noticed this block of asserts checks parent for lineno and end_lineno.

Comment thread Lib/test/test_ast.py
self.assertEqual(grandchild_binop.col_offset, 0)
self.assertEqual(parent_binop.lineno, 1)
self.assertEqual(grandchild_binop.end_col_offset, 3)
self.assertEqual(parent_binop.end_lineno, 2)

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.

Same in this block, it should be granchild_binop in two asserts in this block. @cfbolz could you please make another PR with a fix?

miss-islington added a commit that referenced this pull request Jul 8, 2019
Nested BinOp instances (e.g. a+b+c) had a wrong col_offset for the
second BinOp (e.g. 2 instead of 0 in the example). Fix it by using the
correct st node to copy the line and col_offset from in ast.c.
(cherry picked from commit 110a47c)

Co-authored-by: Carl Friedrich Bolz-Tereick <cfbolz@gmx.de>
@cfbolz

cfbolz commented Jul 9, 2019

Copy link
Copy Markdown
Contributor Author

Thanks a lot, Ivan and Anthony!

@cfbolz

cfbolz commented Jul 9, 2019

Copy link
Copy Markdown
Contributor Author

Yep, will fix the problems and ping you.

@cfbolz cfbolz deleted the bpo-18374-fix-col-offset-binop branch July 9, 2019 15:25
lisroach pushed a commit to lisroach/cpython that referenced this pull request Sep 10, 2019
…-14607)

Nested BinOp instances (e.g. a+b+c) had a wrong col_offset for the
second BinOp (e.g. 2 instead of 0 in the example). Fix it by using the
correct st node to copy the line and col_offset from in ast.c.
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
…-14607)

Nested BinOp instances (e.g. a+b+c) had a wrong col_offset for the
second BinOp (e.g. 2 instead of 0 in the example). Fix it by using the
correct st node to copy the line and col_offset from in ast.c.
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.

6 participants