[issue36911] 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")

Mark Dickinson report at bugs.python.org
Tue May 14 17:00:29 EDT 2019


Mark Dickinson <dickinsm at gmail.com> added the comment:

The AST _does_ correctly represent the Python string object in the source, though. After:

>>> s = """
... Hello \n world
... """

we have a Python object `s` of type `str`, which contains exactly three newlines, zero "n" characters, and zero backslashes. So:

>>> 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 `s`: one containing two newline characters, one "n" and one backslash.

If you need to operate directly on the source as text, then the AST representation probably isn't what you want.

----------
nosy: +mark.dickinson

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36911>
_______________________________________


More information about the Python-bugs-list mailing list