Prepare for gast 0.3, to which TensorFlow will upgrade in version 2.2. Since TF pins the version, all libraries that depend on it need to support both gast versions to avoid dependency conflicts.
PiperOrigin-RevId: 287200227 Change-Id: I40b10e99ccf0d386aca393d3cb0a061ebc6d9744
This commit is contained in:
parent
87f69493d2
commit
107bd3e40b
@ -27,6 +27,7 @@ py_library(
|
||||
"cfg.py",
|
||||
"error_utils.py",
|
||||
"errors.py",
|
||||
"gast_util.py",
|
||||
"inspect_utils.py",
|
||||
"loader.py",
|
||||
"origin_info.py",
|
||||
|
78
tensorflow/python/autograph/pyct/gast_util.py
Normal file
78
tensorflow/python/autograph/pyct/gast_util.py
Normal file
@ -0,0 +1,78 @@
|
||||
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ==============================================================================
|
||||
"""Gast compatibility library. Supports 0.2.2 and 0.3.2."""
|
||||
# TODO(mdan): Remove this file once it's safe to break compatibility.
|
||||
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
import functools
|
||||
|
||||
import gast
|
||||
|
||||
|
||||
GAST2 = hasattr(gast, 'Str')
|
||||
GAST3 = not GAST2
|
||||
|
||||
|
||||
def _is_constant_gast_2(node):
|
||||
return isinstance(node, (gast.Num, gast.Str, gast.Bytes, gast.Ellipsis,
|
||||
gast.NameConstant))
|
||||
|
||||
|
||||
def _is_constant_gast_3(node):
|
||||
return isinstance(node, gast.Constant)
|
||||
|
||||
|
||||
def is_literal(node):
|
||||
"""Tests whether node represents a Python literal."""
|
||||
# Normal literals, True/False/None/Etc. in Python3
|
||||
if is_constant(node):
|
||||
return True
|
||||
|
||||
# True/False/None/Etc. in Python2
|
||||
if isinstance(node, gast.Name) and node.id in ['True', 'False', 'None']:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def _is_ellipsis_gast_2(node):
|
||||
return isinstance(node, gast.Ellipsis)
|
||||
|
||||
|
||||
def _is_ellipsis_gast_3(node):
|
||||
return isinstance(node, gast.Constant) and node.value == Ellipsis
|
||||
|
||||
|
||||
if GAST2:
|
||||
is_constant = _is_constant_gast_2
|
||||
is_ellipsis = _is_ellipsis_gast_2
|
||||
|
||||
Module = gast.Module
|
||||
Name = gast.Name
|
||||
Str = gast.Str
|
||||
|
||||
elif GAST3:
|
||||
is_constant = _is_constant_gast_3
|
||||
is_ellipsis = _is_ellipsis_gast_3
|
||||
|
||||
Module = functools.partial(gast.Module, type_ignores=None) # pylint:disable=invalid-name
|
||||
Name = functools.partial(gast.Name, type_comment=None) # pylint:disable=invalid-name
|
||||
Str = functools.partial(gast.Constant, kind=None) # pylint:disable=invalid-name
|
||||
|
||||
else:
|
||||
assert False
|
Loading…
Reference in New Issue
Block a user