Example #1
0
 def test_translate_cpp_to_python(self, input_path):
     reader = CodeReader()
     cpp_code = reader.read_file(input_path)
     language_from = Language.find('C++')
     language_to = Language.find('Python')
     translator = AutoTranslator(language_from, language_to,
                                 ast_generalizer_kwargs={'scope': {'path': input_path}})
     python_code = translator.translate(cpp_code, input_path)
     basic_check_python_code(self, input_path, python_code)
Example #2
0
 def test_translate_python_to_cpp(self, input_path):
     reader = CodeReader()
     python_code = reader.read_file(input_path)
     language_from = Language.find('Python')
     language_to = Language.find('C++')
     translator = AutoTranslator(language_from, language_to)
     with _TIME.measure('translate.python3_to_cpp14.{}'
                        .format(input_path.name.replace('.', '_'))) as timer:
         cpp_code = translator.translate(python_code)
     basic_check_cpp_code(self, input_path, cpp_code)
     _LOG.info('translated "%s" to C++ in %fs', input_path, timer.elapsed)
Example #3
0
 def test_translate_c_to_python(self, input_path):
     reader = CodeReader()
     c_code = reader.read_file(input_path)
     language_from = Language.find('C11')
     language_to = Language.find('Python')
     translator = AutoTranslator(language_from, language_to)
     with _TIME.measure('translate.c11_to_python3.{}'
                        .format(input_path.name.replace('.', '_'))) as timer:
         python_code = translator.translate(c_code, input_path)
     basic_check_python_code(self, input_path, python_code)
     _LOG.info('translated "%s" to Python in %fs', input_path, timer.elapsed)
Example #4
0
 def test_auto_translator_init(self):
     for (_, language_from_name), (_, language_to_name) \
             in itertools.product(EXAMPLES_LANGS_NAMES.items(), EXAMPLES_LANGS_NAMES.items()):
         if language_from_name in NOT_PARSED_LANGS or language_to_name in NOT_UNPARSED_LANGS:
             continue
         with self.subTest(language_from_name=language_from_name,
                           language_to_name=language_to_name):
             from_language = Language.find(language_from_name)
             self.assertIsInstance(from_language, Language)
             to_language = Language.find(language_to_name)
             self.assertIsInstance(to_language, Language)
             translator = AutoTranslator(from_language, to_language)
             self.assertIsInstance(translator, Translator)
 def test_python_to_cpp(self):
     language_from = Language.find('Python')
     language_to = Language.find('C++')
     reader = CodeReader()
     root = EXAMPLES_ROOTS['python3']
     for input_path in {
             root.joinpath('do_nothing.py'),
             root.joinpath('gemm.py')
     }:
         translator = AutoTranslator(language_from, language_to)
         with self.subTest(input_path=input_path):
             python_code = reader.read_file(input_path)
             cpp_code = translator.translate(python_code)
             basic_check_cpp_code(self, input_path, cpp_code)
Example #6
0
    def test_transpile_with_openmp(self, input_path):
        output_dir = make_f2py_tmp_folder(input_path)

        transpiler = AutoTranspiler(Language.find('Python'), Language.find('Fortran'))
        self.assertIsNotNone(transpiler)
        reader = CodeReader()

        with tempfile.NamedTemporaryFile(suffix='.f90', delete=False) as output_file:
            # TODO: this leaves garbage behind in /tmp/ but is neeeded
            # by subsequent transpiler passes

            # code_writer = CodeWriter('.py')
            # target_inlined_path = pathlib.Path(output_file.name)
            # code_writer.write_file(target_inlined_code, target_inlined_path)
            output_path = pathlib.Path(output_file.name)

        compiled_path = transpiler.transpile(
            reader.read_file(input_path), input_path, output_path, output_dir)
Example #7
0
 def test_class_finding(self):
     for language_codename, language_name in EXAMPLES_LANGS_NAMES.items():
         if language_name in NOT_UNPARSED_LANGS:
             continue
         with self.subTest(language_codename=language_codename, language_name=language_name):
             language = Language.find(language_name)
             self.assertIsInstance(language, Language)
             parser = Parser.find(language)()
             self.assertIsInstance(parser, Parser)
             ast_generalizer = AstGeneralizer.find(language)()
             self.assertIsInstance(ast_generalizer, AstGeneralizer)
             unparser = Unparser.find(language)()
             self.assertIsInstance(unparser, Unparser)
Example #8
0
 def test_auto_parser(self):
     for language_codename, paths in EXAMPLES_FILES.items():
         language_name = EXAMPLES_LANGS_NAMES[language_codename]
         if language_name in NOT_PARSED_LANGS:
             continue
         language = Language.find(language_name)
         self.assertIsInstance(language, Language, msg=(language_codename, language_name))
         auto_parser = GeneralizingAutoParser(language)
         reader = CodeReader()
         for path in paths:
             code = reader.read_file(path)
             with self.subTest(language_name=language_name, language=language):
                 pair1 = auto_parser.parse_and_generalize(code, path)
                 pair2 = auto_parser.parse_and_generalize_file(path)
                 self.assertEqual(horast.dump(pair1), horast.dump(pair2))
Example #9
0
 def test_auto_processing(self):
     for language_codename, paths in EXAMPLES_FILES.items():
         language_name = EXAMPLES_LANGS_NAMES[language_codename]
         if language_name in NOT_PARSED_LANGS:
             continue
         language = Language.find(language_name)
         self.assertIsInstance(language, Language, msg=(language_codename, language_name))
         reader = CodeReader()
         for path in paths:
             code = reader.read_file(path)
             with self.subTest(language_name=language_name, language=language):
                 parser = Parser.find(language)()
                 specific_ast = parser.parse(code, path)
                 ast_generalizer = AstGeneralizer.find(language)()
                 general_ast = ast_generalizer.generalize(specific_ast)