Loads the given source code as a Python module.
(source, delete_on_exit)
| 87 | |
| 88 | |
| 89 | def source_to_entity(source, delete_on_exit): |
| 90 | """Loads the given source code as a Python module.""" |
| 91 | if six.PY2: |
| 92 | source = source.encode('utf-8') |
| 93 | f = tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) |
| 94 | else: |
| 95 | f = tempfile.NamedTemporaryFile( # pylint:disable=unexpected-keyword-arg |
| 96 | mode='w', suffix='.py', delete=False, encoding='utf-8') |
| 97 | |
| 98 | with f: |
| 99 | module_name = os.path.basename(f.name[:-3]) |
| 100 | f.write(source) |
| 101 | |
| 102 | if delete_on_exit and ag_logging.get_verbosity() < 3: |
| 103 | atexit.register(lambda: os.remove(f.name)) |
| 104 | return imp.load_source(module_name, f.name), f.name |
| 105 | |
| 106 | |
| 107 | # TODO(mdan): Rename: ast_to_entity |
no test coverage detected