-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
ast.parse outputs ast.Strs which do not differentiate between the ASCII codepoint 12 (literal new line) and the ASCII codepoints 134 and 156 ("\n") #81092
Comments
reproducing case: file.py:
And then in a REPL (2.7 or 3+):
Expected behaviour:
|
I believe this one is even before the ast, in the tokenizer. Though the AST is also doing some normalisation in identifiers (“ε” U+03B5 Greek Small Letter Epsilon Unicode Character , and “ϵ” U+03F5 Greek Lunate Epsilon Symbol Unicode Character get normalized to the same for example, which is problematic as the look different, but end up being same identifier). I'd be interested in an opt-in flag to not do this normalisation (I have a prototype with this for the identifier normalisation in ast, but I have not looked at the tokenizer), which might be useful for some linting tools. |
The existing behavior is what I'd expect. Using python3: >>> import ast
>>> s = open('file.py', 'rb').read()
>>> s
b'"""\nHello \\n blah.\n"""\n'
>>> ast.dump(ast.parse(s))
"Module(body=[Expr(value=Str(s='\\nHello \\n blah.\\n'))])"
>>> eval(s)
'\nHello \n blah.\n' As always with the AST, some information is lost. It's not designed to be able to round-trip back to the source text. |
There's a difference between round-tripping back to the source text and correctly representing the text in the source, though. Since I'm using this module to perform static analysis of a Python module to retrieve class/function definitions and their docstrings to create API documentation, the string being the same as what it is in the file is important to me. |
The AST _does_ correctly represent the Python string object in the source, though. After: >>> s = """
... Hello \n world
... """ we have a Python object >>> s == '\nHello \n world\n'
True If the AST Str node value were '\nHello \\n world\n' as you suggest, that would represent a different string to If you need to operate directly on the source as text, then the AST representation probably isn't what you want. |
I agree with Mark: the string is being correctly interpreted by the AST parser, per Python's tokenizer rules. You might want to look at lib2to3, which I think is also used by black. It's also possible that mypy or another static analyzer would be using some library you can leverage. |
Note: 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: