1# Lint as: python3 2# Copyright 2017 The TensorFlow Authors. All Rights Reserved. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# ============================================================================== 16"""Converting AST to code and Python entities. 17 18Adapted from Tangent. 19""" 20 21from __future__ import absolute_import 22from __future__ import division 23from __future__ import print_function 24 25import atexit 26import errno 27import importlib 28import os 29import sys 30import tempfile 31 32from tensorflow.python.autograph.pyct import origin_info 33from tensorflow.python.autograph.pyct import parser 34 35 36def _remove_file(file_name): 37 """Remove a file, if it exists.""" 38 try: 39 os.remove(file_name) 40 except OSError as e: 41 if e.errno == errno.ENOENT: 42 # The file disappeared. Ignore this. Temporary files might get 43 # cleaned up, especially if they reside in /tmp. 44 pass 45 else: 46 raise 47 48 49def load_source(source, delete_on_exit): 50 """Loads the given source code as a Python module.""" 51 with tempfile.NamedTemporaryFile( 52 mode='w', 53 suffix='.py', 54 prefix='__autograph_generated_file', 55 delete=False, 56 encoding='utf-8') as f: 57 module_name = os.path.basename(f.name[:-3]) 58 file_name = f.name 59 f.write(source) 60 61 if delete_on_exit: 62 atexit.register(lambda: _remove_file(file_name)) 63 64 spec = importlib.util.spec_from_file_location(module_name, file_name) 65 module = importlib.util.module_from_spec(spec) 66 spec.loader.exec_module(module) 67 # TODO(mdan): Use our own garbage-collected cache instead of sys.modules. 68 sys.modules[module_name] = module 69 return module, file_name 70 71 72def load_ast(nodes, 73 indentation=' ', 74 include_source_map=False, 75 delete_on_exit=True): 76 """Loads the given AST as a Python module. 77 78 Compiling the AST code this way ensures that the source code is readable by 79 e.g. `pdb` or `inspect`. 80 81 Args: 82 nodes: Union[ast.AST, Iterable[ast.AST]], the code to compile, as an AST 83 object. 84 indentation: Text, the string to use for indentation. 85 include_source_map: bool, whether return a source map. 86 delete_on_exit: bool, whether to delete the temporary file used for 87 compilation on exit. 88 89 Returns: 90 Tuple[module, Text, Dict[LineLocation, OriginInfo]], containing: 91 the module containing the unparsed nodes, the source code corresponding to 92 nodes, and the source map. Is include_source_map is False, the source map 93 will be None. 94 """ 95 if not isinstance(nodes, (list, tuple)): 96 nodes = (nodes,) 97 98 source = parser.unparse(nodes, indentation=indentation) 99 module, _ = load_source(source, delete_on_exit) 100 101 if include_source_map: 102 source_map = origin_info.create_source_map(nodes, source, module.__file__) 103 else: 104 source_map = None 105 106 # TODO(mdan): Return a structured object. 107 return module, source, source_map 108