Remove line continuations before applying dedent_block. Fixes #35765.

PiperOrigin-RevId: 290602586
Change-Id: I262db91fc869e0d69bc52b35f5a74da8d3157cf4
This commit is contained in:
Dan Moldovan 2020-01-20 05:39:12 -08:00 committed by TensorFlower Gardener
parent c10283ae80
commit 8e3adf77b2
2 changed files with 31 additions and 0 deletions

View File

@ -43,9 +43,16 @@ STANDARD_PREAMBLE_LEN = 2
_LEADING_WHITESPACE = re.compile(r'\s*')
def _unfold_continuations(code_string):
"""Removes any backslash line continuations from the code."""
return code_string.replace('\\\n', '')
def dedent_block(code_string):
"""Dedents a code so that its first line starts at row zero."""
code_string = _unfold_continuations(code_string)
token_gen = tokenize.generate_tokens(six.StringIO(code_string).readline)
block_indentation = None

View File

@ -129,6 +129,30 @@ string""")
f = self._eval_code(parser.dedent_block(code), 'f')
self.assertEqual(f(), (1, 2, 3))
def test_dedent_block_continuation(self):
code = r"""
def f():
a = \
1
return a
"""
f = self._eval_code(parser.dedent_block(code), 'f')
self.assertEqual(f(), 1)
def test_dedent_block_continuation_in_string(self):
code = r"""
def f():
a = "a \
b"
return a
"""
f = self._eval_code(parser.dedent_block(code), 'f')
self.assertEqual(f(), 'a b')
def test_parse_expression(self):
node = parser.parse_expression('a.b')
self.assertEqual('a', node.value.id)