def main(): path, args = process_options() try: mainfile = open(path) text = mainfile.read() mainfile.close() except IOError as ioerr: fail("mypy: can't read file '{}': {}".format(path, ioerr.strerror)) try: outputdir = os.path.join(os.path.dirname(path), '__mycache__') tempdir = False if not os.path.isdir(outputdir): try: os.mkdir(outputdir) except OSError: # Could not create a directory under program directory; must # fall back to a temp directory. It will be removed later. outputdir = tempfile.mkdtemp() tempdir = True try: # Parse and type check the program and dependencies. trees, symtable, infos, types = build(text, path, False, None, True) # Translate each file in the program to Python. # TODO support packages for t in trees: if not is_stub(t.path): out_path = os.path.join(outputdir, os.path.basename(t.path)) log('translate {} to {}'.format(t.path, out_path)) v = PythonGenerator(pyversion) t.accept(v) outfile = open(out_path, 'w') outfile.write(v.output()) outfile.close() # Run the translated program. status = subprocess.call( [interpreter, '{}/{}'.format(outputdir,os.path.basename(path))] + args) sys.exit(status) finally: if tempdir: shutil.rmtree(outputdir) except CompileError as e: for m in e.messages: print(m) sys.exit(1)
def test_python_generation(testcase): """Perform a mypy-to-Python source code transformation test case.""" any a expected = testcase.output # By default, assume an identity translation. This is useful for # dynamically typed code. if expected == []: expected = testcase.input try: src = '\n'.join(testcase.input) # Parse and semantically analyze the source program. trees, symtable, infos, types = build(src, 'main', True, test_temp_dir) a = [] first = True # Produce an output containing the pretty-printed forms (with original # formatting) of all the relevant source files. for t in trees: # Omit the builtins module and files marked for omission. if not t.path.endswith(os.sep + 'builtins.py') and '-skip.' not in t.path: # Add file name + colon for files other than the first. if not first: a.append('{}:'.format( fix_path(remove_prefix(t.path, test_temp_dir)))) ver = 3 # Generate Python 2 instead of 3? if '-2' in testcase.name: ver = 2 v = PythonGenerator(ver) t.accept(v) s = v.output() if s != '': a += s.split('\n') first = False except CompileError as e: a = e.messages assert_string_arrays_equal( expected, a, 'Invalid source code output ({}, line {})'.format( testcase.file, testcase.line))
outputdir = tempfile.mkdtemp() tempdir = True try: # Parse and type check the program and dependencies. trees, symtable, infos, types = build(text, path, False, None, True) # Translate each file in the program to Python. # TODO support packages for t in trees: if not is_stub(t.path): out_path = os.path.join(outputdir, os.path.basename(t.path)) log('translate {} to {}'.format(t.path, out_path)) v = PythonGenerator(pyversion) t.accept(v) outfile = open(out_path, 'w') outfile.write(v.output()) outfile.close() # Run the translated program. status = subprocess.call( [interpreter, '{}/{}'.format(outputdir,os.path.basename(path))] + args) sys.exit(status) finally: if tempdir: shutil.rmtree(outputdir) except CompileError as e: