diff --git a/tensorflow/python/autograph/pyct/common_transformers/anf.py b/tensorflow/python/autograph/pyct/common_transformers/anf.py index 009ae2b4417..15ceefcbdd4 100644 --- a/tensorflow/python/autograph/pyct/common_transformers/anf.py +++ b/tensorflow/python/autograph/pyct/common_transformers/anf.py @@ -36,11 +36,11 @@ from tensorflow.python.autograph.pyct import templates from tensorflow.python.autograph.pyct import transformer +# TODO(mdan): Replace with naming.Namer. class DummyGensym(object): """A dumb gensym that suffixes a stem by sequential numbers from 1000.""" - def __init__(self, ctx): - del ctx + def __init__(self): # A proper implementation needs to account for: # * ctx.info.namespace # * all the symbols defined in the AST @@ -105,14 +105,12 @@ class AnfTransformer(transformer.Base): # processing the `body` and the `orelse` need to be kept together with them, # and not accidentally lifted out of the `if`. - def __init__(self, ctx, config, gensym_source=None): + def __init__(self, ctx, config): """Creates an ANF transformer. Args: ctx: transformer.Context config: Configuration - gensym_source: An optional object with the same interface as `DummyGensym` - for generating unique names """ super(AnfTransformer, self).__init__(ctx) if config is None: @@ -137,10 +135,7 @@ class AnfTransformer(transformer.Base): (ASTEdgePattern(ANY, ANY, gast.expr), REPLACE)] else: self._overrides = config - if gensym_source is None: - self._gensym = DummyGensym(ctx) - else: - self._gensym = gensym_source(ctx) + self._gensym = DummyGensym() self._pending_statements = [] def _consume_pending_statements(self): @@ -529,7 +524,7 @@ def _is_trivial(node): return False -def transform(node, ctx, config=None, gensym_source=None): +def transform(node, ctx, config=None): """Converts the given node to A-normal form (ANF). The general idea of A-normal form: https://en.wikipedia.org/wiki/A-normal_form @@ -605,7 +600,5 @@ def transform(node, ctx, config=None, gensym_source=None): argument provide? config: Optional ANF configuration. If omitted, ANF replaces all expression expect literal constants. - gensym_source: An optional object with the same interface as `DummyGensym` - for generating unique names. """ - return AnfTransformer(ctx, config, gensym_source=gensym_source).visit(node) + return AnfTransformer(ctx, config).visit(node) diff --git a/tensorflow/python/autograph/pyct/common_transformers/anf_test.py b/tensorflow/python/autograph/pyct/common_transformers/anf_test.py index 80715f115be..ced2ee3a975 100644 --- a/tensorflow/python/autograph/pyct/common_transformers/anf_test.py +++ b/tensorflow/python/autograph/pyct/common_transformers/anf_test.py @@ -29,30 +29,8 @@ from tensorflow.python.autograph.pyct.common_transformers import anf from tensorflow.python.platform import test -class DummyGensym(object): - """A dumb gensym that suffixes a stem by sequential numbers from 1000.""" - - def __init__(self, ctx): - del ctx - # A proper implementation needs to account for: - # * ctx.info.namespace - # * all the symbols defined in the AST - # * the symbols generated so far - self._idx = 0 - - def new_name(self, stem='tmp'): - self._idx += 1 - return stem + '_' + str(1000 + self._idx) - - -# These two test functions have to be top-level, not nested, for compatibility -# with some unknown version of Python 2.7 preceding 2.7.15. Why? Because -# `exec` and nested function definitions _incompatibly_ change the -# representation of local variables, such that `exec` inside a nested function -# definition is a syntax error in that version. The tuple form of `exec` fixes -# this problem, but apparently that was introduced in some unknown version of -# Python that's more recent than at least one version that we wish to be -# compatible with. +# TODO(mdan): These two functions no longer need to be at the top level. +# TODO(mdan): Don't use exec. def exec_test_function(): # The point is to test A-normal form conversion of exec # pylint: disable=exec-used @@ -88,9 +66,7 @@ class AnfTestBase(test.TestCase): # statements. exp_node, _ = parser.parse_entity(expected_fn, future_features=()) node, _ = parser.parse_entity(test_fn, future_features=()) - node = anf.transform( - node, self._simple_context(), - config=config, gensym_source=DummyGensym) + node = anf.transform(node, self._simple_context(), config=config) exp_name = exp_node.name # Ignoring the function names in the result because they can't be # the same (because both functions have to exist in the same scope @@ -98,8 +74,7 @@ class AnfTestBase(test.TestCase): node.name = exp_name self.assert_same_ast(exp_node, node) # Check that ANF is idempotent - node_repeated = anf.transform( - node, self._simple_context(), gensym_source=DummyGensym) + node_repeated = anf.transform(node, self._simple_context()) self.assert_same_ast(node_repeated, node) @@ -466,9 +441,7 @@ class AnfNonTransformationTest(AnfTransformerTest): orig_source = parser.unparse(node, indentation=' ') orig_str = textwrap.dedent(orig_source).strip() config = [(anf.ANY, anf.LEAVE)] # Configuration to transform nothing - node = anf.transform( - node, self._simple_context(), - config=config, gensym_source=DummyGensym) + node = anf.transform(node, self._simple_context(), config=config) new_source = parser.unparse(node, indentation=' ') new_str = textwrap.dedent(new_source).strip() self.assertEqual(orig_str, new_str)