Skip to content

Commit

Permalink
Add unicode string support to pyct pretty printer.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 237161597
  • Loading branch information
brilee authored and tensorflower-gardener committed Mar 7, 2019
1 parent 87cd62e commit 9fa3d27
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions tensorflow/python/autograph/pyct/pretty_printer.py
Expand Up @@ -18,7 +18,9 @@
from __future__ import division
from __future__ import print_function


import gast
import six
import termcolor


Expand Down Expand Up @@ -106,9 +108,12 @@ def generic_visit(self, node, name=None):
self._print('%s%s=()' % (self._indent(), self._field(f)))
elif isinstance(v, gast.AST):
self.generic_visit(v, f)
elif isinstance(v, str):
elif isinstance(v, six.binary_type):
self._print('%s%s=%s' % (self._indent(), self._field(f),
self._value('b"%s"' % v)))
elif isinstance(v, six.text_type):
self._print('%s%s=%s' % (self._indent(), self._field(f),
self._value('"%s"' % v)))
self._value('u"%s"' % v)))
else:
self._print('%s%s=%s' % (self._indent(), self._field(f),
self._value(v)))
Expand Down
9 changes: 9 additions & 0 deletions tensorflow/python/autograph/pyct/pretty_printer_test.py
Expand Up @@ -19,13 +19,22 @@
from __future__ import print_function

import ast
import textwrap

from tensorflow.python.autograph.pyct import pretty_printer
from tensorflow.python.platform import test


class PrettyPrinterTest(test.TestCase):

def test_unicode_bytes(self):
source = textwrap.dedent('''
def f():
return b'b', u'u', 'depends_py2_py3'
''')
node = ast.parse(source)
self.assertIsNotNone(pretty_printer.fmt(node))

def test_format(self):
node = ast.FunctionDef(
name='f',
Expand Down

0 comments on commit 9fa3d27

Please sign in to comment.