1# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Checkers for detecting unsupported Python features."""
16
17import gast
18
19from tensorflow.python.autograph.pyct import errors
20
21
22class UnsupportedFeaturesChecker(gast.NodeVisitor):
23 """Quick check for Python features we know we don't support.
24
25 Any features detected will cause AutoGraph to not compile a function.
26 """
27
28 def visit_Attribute(self, node):
29 if (node.attr is not None
30 and node.attr.startswith('__') and not node.attr.endswith('__')):
31 raise errors.UnsupportedLanguageElementError(
32 'mangled names are not yet supported')
33 self.generic_visit(node)
34
35 def visit_For(self, node):
36 if node.orelse:
37 raise errors.UnsupportedLanguageElementError(
38 'for/else statement not yet supported')
39 self.generic_visit(node)
40
41 def visit_While(self, node):
42 if node.orelse:
43 raise errors.UnsupportedLanguageElementError(
44 'while/else statement not yet supported')
45 self.generic_visit(node)
46
47 # These checks could potentially be replaced with inspect.isgeneratorfunction
48 # to avoid a getsource/parse/ast-walk round trip.
49 def visit_Yield(self, node):
50 raise errors.UnsupportedLanguageElementError('generators are not supported')
51
52 def visit_YieldFrom(self, node):
53 raise errors.UnsupportedLanguageElementError('generators are not supported')
54
55
56def verify(node):
57 UnsupportedFeaturesChecker().visit(node)