-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
col_offset is -1 and lineno is wrong for multiline string expressions #61010
Comments
Given an input module such as class klass(object):
"""multi line comment
continued on this line
"""
and implementing a custom ast.NodeVisitor such as import as
class CustomVisitor(ast.NodeVisitor):
def visit_ClassDef(self, node):
for childNode in node.body:
self.visit(childNode)
def visit_Expr(self, node):
print(node.col_offset)
print(node.value.col_offset) and feeding it the compiled ast from the module above f = open('./module.py')
source = f.read()
node = ast.parse(source, mode = 'exec')
visitor = CustomVisitor()
visitor.visit(node) should yield -1/-1 for the docstring that is the first it will, however, yield the correct col_offset of 4/4 for the multi line docstring following that will again It believe that this behaviour is not correct and instead |
Please note that, regardless of the indent level, the col_offset for multi line str expressions will always be -1. |
In addition, the reported lineno will be set to the last line of the multi line string instead of the first line where parsing the parse began parsing the string. |
Please see the attached patch that will resolve the issue. It also includes a test case in test_ast.py. What the patch does is as follows:
The included test case ensures that the col_offset and lineno of |
I have created a patch for Python 2.7.3 that fixes the issue for that release, too. |
If this is really an 'enhancement', it will only go in 3.4. If it is a bug/behavior issue, then it should be marked as such and 2.7,3.2,3.3 selected. I have not read the doc and messages well enough to know, so I leave that to you and Benjamin. The patch includes a test. It needs a patch to Misc/ACKS to add Carsten Klein between Reid Kleckner and Bastian Kleineidam |
I left comments on Rietveld a few days ago. |
Any updates on this? I'm running into this as well (still a problem in 3.4)
|
What's the status on this? Anything preventing it getting fixed? Still the same in 3.6.1: >>> import ast
>>> ast.parse("""'''foo\n'''""").body[0].value.col_offset
-1 |
pypy seems to have this right (though I don't know enough about their internals to know if cpython can benefit from their patch) $ venvpypy/bin/pythonPython 2.7.10 (3260adbeba4a, Apr 19 2016, 17:42:20)
[PyPy 5.1.0 with GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>> import ast, astpretty
>>>> astpretty.pprint(ast.parse('"""\n"""'))
Module(
body=[
Expr(
lineno=1,
col_offset=0,
value=Str(lineno=1, col_offset=0, s='\n'),
),
],
) |
Still a problem in 3.7: $ python3.7
Python 3.7.0b2 (default, Feb 28 2018, 06:59:18)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> ast.parse("""x = '''foo\n'''""").body[-1].value
<_ast.Str object at 0x7fcde6898358>
>>> ast.parse("""x = '''foo\n'''""").body[-1].value.col_offset
-1 |
Should we backport this to 3.7? |
I agree -- probably safer to not backport to 3.7 in case someone is relying on this behaviour. |
Fixed in https://bugs.python.org/issue39209 |
lineno
andcol_offset
for multi-line string tokens. #10021Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: