diff options
author | A. Unique TensorFlower <gardener@tensorflow.org> | 2018-09-17 13:11:51 -0700 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2018-09-17 13:15:39 -0700 |
commit | 5da7359a9e0b832f608dc66d7a22e647f09ec035 (patch) | |
tree | 8ef298f62ba68dda1dec52df2e15835278d4e993 /tensorflow/python/autograph | |
parent | de3fa499bb05c595f9e341c7d507b47b8d44ae90 (diff) |
Raise error on encountering bad indentation during Autograph parsing.
PiperOrigin-RevId: 213324570
Diffstat (limited to 'tensorflow/python/autograph')
-rw-r--r-- | tensorflow/python/autograph/pyct/parser.py | 15 | ||||
-rw-r--r-- | tensorflow/python/autograph/pyct/parser_test.py | 16 |
2 files changed, 30 insertions, 1 deletions
diff --git a/tensorflow/python/autograph/pyct/parser.py b/tensorflow/python/autograph/pyct/parser.py index 112ed46a1e..63686350d5 100644 --- a/tensorflow/python/autograph/pyct/parser.py +++ b/tensorflow/python/autograph/pyct/parser.py @@ -31,8 +31,21 @@ from tensorflow.python.util import tf_inspect def parse_entity(entity): """Returns the AST of given entity.""" source = tf_inspect.getsource(entity) + # Comments and multiline strings can appear at arbitrary indentation levels, + # causing textwrap.dedent to not correctly dedent source code. + # TODO(b/115884650): Automatic handling of comments/multiline strings. source = textwrap.dedent(source) - return parse_str(source), source + try: + return parse_str(source), source + except IndentationError: + # Because we are parsing the source code of entities that have already + # successfully parsed once, any IndentationErrors are guaranteed to be + # caused by insufficient dedenting. + raise ValueError( + 'Failed to dedent prior to parsing source code. If you have comments ' + 'or multiline strings in your code, try indenting them. ' + 'Multiline strings can be rewritten using textwrap.dedent.\n' + 'Offending source code: \n %s' % source) def parse_str(src): diff --git a/tensorflow/python/autograph/pyct/parser_test.py b/tensorflow/python/autograph/pyct/parser_test.py index d0b465eb73..d3a7b7a014 100644 --- a/tensorflow/python/autograph/pyct/parser_test.py +++ b/tensorflow/python/autograph/pyct/parser_test.py @@ -42,6 +42,22 @@ class ParserTest(test.TestCase): """)) self.assertEqual('f', mod.body[0].name) + def test_parse_comments(self): + def f(): +# unindented comment + pass + with self.assertRaises(ValueError): + parser.parse_entity(f) + + def test_parse_multiline_strings(self): + def f(): + print(""" +some +multiline +string""") + with self.assertRaises(ValueError): + parser.parse_entity(f) + def test_parse_expression(self): node = parser.parse_expression('a.b') self.assertEqual('a', node.value.id) |