More cleanup of dead Python 2 code.

PiperOrigin-RevId: 338246558
Change-Id: Id26f592e991446cebc5b9eced9028c971b283b0e
This commit is contained in:
Dan Moldovan 2020-10-21 05:44:58 -07:00 committed by TensorFlower Gardener
parent 673b993983
commit b39bc5f1f0
6 changed files with 84 additions and 89 deletions
tensorflow/python/autograph

View File

@ -33,10 +33,6 @@ from tensorflow.python.autograph.pyct.static_analysis import annos
from tensorflow.python.autograph.pyct.static_analysis import liveness
from tensorflow.python.autograph.pyct.static_analysis import reaching_definitions
from tensorflow.python.autograph.pyct.static_analysis import reaching_fndefs
from tensorflow.python.autograph.utils import compat_util
# TODO(mdan): Refactor functions to make them smaller.
class _Function(object):
@ -419,6 +415,3 @@ def transform(node, ctx):
node = ControlFlowTransformer(ctx).visit(node)
return node
compat_util.deprecated_py2_support(__name__)

View File

@ -1216,6 +1216,3 @@ def _tf_if_stmt(
def _py_if_stmt(cond, body, orelse):
"""Overload of if_stmt that executes a Python if statement."""
return body() if cond else orelse()
compat_util.deprecated_py2_support(__name__)

View File

@ -89,38 +89,8 @@ py_test(
testonly = True,
srcs = ["liveness_test.py"],
python_version = "PY3",
srcs_version = "PY2AND3",
deps = [
":static_analysis",
"//tensorflow/python:client_testlib",
"//tensorflow/python/autograph/pyct",
],
)
py_library(
name = "liveness_test_lib",
srcs = ["liveness_test.py"],
srcs_version = "PY2AND3",
deps = [
":static_analysis",
"//tensorflow/python:client_testlib",
"//tensorflow/python/autograph/pyct",
"@gast_archive//:gast",
],
)
py_test(
name = "liveness_py3_test",
srcs = ["liveness_py3_test.py"],
python_version = "PY3",
srcs_version = "PY3",
tags = [
"no_oss_py2",
"no_pip",
"nopip",
],
deps = [
":liveness_test_lib",
":static_analysis",
"//tensorflow/python:client_testlib",
"//tensorflow/python/autograph/pyct",
@ -131,38 +101,8 @@ py_test(
name = "reaching_definitions_test",
srcs = ["reaching_definitions_test.py"],
python_version = "PY3",
srcs_version = "PY2AND3",
deps = [
":static_analysis",
"//tensorflow/python:client_testlib",
"//tensorflow/python/autograph/pyct",
],
)
py_library(
name = "reaching_definitions_test_lib",
srcs = ["reaching_definitions_test.py"],
srcs_version = "PY2AND3",
deps = [
":static_analysis",
"//tensorflow/python:client_testlib",
"//tensorflow/python/autograph/pyct",
"@gast_archive//:gast",
],
)
py_test(
name = "reaching_definitions_py3_test",
srcs = ["reaching_definitions_py3_test.py"],
python_version = "PY3",
srcs_version = "PY3",
tags = [
"no_oss_py2",
"no_pip",
"nopip",
],
deps = [
":reaching_definitions_test_lib",
":static_analysis",
"//tensorflow/python:client_testlib",
"//tensorflow/python/autograph/pyct",

View File

@ -30,25 +30,6 @@ NodeAnno = annos.NodeAnno
class LivenessAnalyzerTest(liveness_test.LivenessAnalyzerTestBase):
"""Tests which can only run in Python 3."""
def test_nonlocal_symbol(self):
nonlocal_a = 3
nonlocal_b = 13
def test_fn(c):
nonlocal nonlocal_a
nonlocal nonlocal_b
if nonlocal_a:
nonlocal_b = c
else:
nonlocal_b = c
return nonlocal_b
node = self._parse_and_analyze(test_fn)
fn_body = node.body
self.assertHasLiveOut(fn_body[2], ('nonlocal_b',))
self.assertHasLiveIn(fn_body[2], ('nonlocal_a', 'c'))
if __name__ == '__main__':
test.main()

View File

@ -552,6 +552,25 @@ class LivenessAnalyzerTest(LivenessAnalyzerTestBase):
self.assertHasLiveOut(fn_body[2], ('global_b',))
self.assertHasLiveIn(fn_body[2], ('global_a', 'c'))
def test_nonlocal_symbol(self):
nonlocal_a = 3
nonlocal_b = 13
def test_fn(c):
nonlocal nonlocal_a
nonlocal nonlocal_b
if nonlocal_a:
nonlocal_b = c
else:
nonlocal_b = c
return nonlocal_b
node = self._parse_and_analyze(test_fn)
fn_body = node.body
self.assertHasLiveOut(fn_body[2], ('nonlocal_b',))
self.assertHasLiveIn(fn_body[2], ('nonlocal_a', 'c'))
if __name__ == '__main__':
test.main()

View File

@ -465,6 +465,71 @@ class ReachingDefinitionsAnalyzerTest(ReachingDefinitionsAnalyzerTestBase):
self.assertHasDefinedIn(fn_body[2], ('global_a', 'global_b'))
def test_nonlocal(self):
a = 3
b = 13
def test_fn():
nonlocal a
nonlocal b
if a:
b = []
return a, b
node = self._parse_and_analyze(test_fn)
fn_body = node.body
self.assertHasDefs(fn_body[2].test, 1)
self.assertHasDefs(fn_body[2].body[0].targets[0], 1)
self.assertHasDefs(fn_body[3].value.elts[0], 1)
self.assertHasDefs(fn_body[3].value.elts[1], 2)
self.assertSameDef(fn_body[2].test, fn_body[3].value.elts[0])
self.assertHasDefinedIn(fn_body[2], ('a', 'b'))
def test_nonlocal_in_nested_function(self):
a = 3
b = 13
def test_fn():
a = 3
b = 13
def local_fn():
nonlocal a, b
if a:
b = []
return a, b
return local_fn()
node = self._parse_and_analyze(test_fn)
local_body = node.body[2].body
self.assertHasDefs(local_body[1].test, 1)
self.assertHasDefs(local_body[1].body[0].targets[0], 1)
self.assertHasDefs(local_body[2].value.elts[0], 1)
self.assertHasDefs(local_body[2].value.elts[1], 2)
self.assertSameDef(local_body[1].test, local_body[2].value.elts[0])
# Note: the function name is is visible inside the function body. But it's
# a closure variable, not a local.
#
# Example:
#
# >>> def f():
# ... print(f)
# >>> g = f
# >>> f = 'something else'
# >>> g()
# something else
#
self.assertHasDefinedIn(local_body[1], ('a', 'b'))
if __name__ == '__main__':
test.main()