def run_case(self, testcase: DataDrivenTestCase) -> None:
        # Kind of hacky. Not sure if we need more structure here.
        options = CompilerOptions(strip_asserts='StripAssert' in testcase.name)
        """Perform a runtime checking transformation test case."""
        with use_custom_builtins(
                os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase):
            expected_output = remove_comment_lines(testcase.output)
            expected_output = replace_native_int(expected_output)
            expected_output = replace_word_size(expected_output)
            name = testcase.name
            # If this is specific to some bit width, always pass if platform doesn't match.
            if name.endswith('_64bit') and IS_32_BIT_PLATFORM:
                return
            if name.endswith('_32bit') and not IS_32_BIT_PLATFORM:
                return
            try:
                ir = build_ir_for_single_file(testcase.input, options)
            except CompileError as e:
                actual = e.messages
            else:
                actual = []
                for fn in ir:
                    if (fn.name == TOP_LEVEL_NAME
                            and not name.endswith('_toplevel')):
                        continue
                    actual.extend(format_func(fn))

            assert_test_output(testcase, actual, 'Invalid source code output',
                               expected_output)
Exemplo n.º 2
0
    def run_case(self, testcase: DataDrivenTestCase) -> None:
        """Perform a runtime checking transformation test case."""
        with use_custom_builtins(
                os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase):
            expected_output = remove_comment_lines(testcase.output)
            try:
                ir = build_ir_for_single_file(testcase.input)
            except CompileError as e:
                actual = e.messages
            else:
                actual = []
                for fn in ir:
                    if (fn.name == TOP_LEVEL_NAME
                            and not testcase.name.endswith('_toplevel')):
                        continue
                    insert_uninit_checks(fn)
                    insert_exception_handling(fn)
                    insert_ref_count_opcodes(fn)
                    actual.extend(format_func(fn))
                    if testcase.name.endswith('_freq'):
                        common = frequently_executed_blocks(fn.blocks[0])
                        actual.append('hot blocks: %s' %
                                      sorted(b.label for b in common))

            assert_test_output(testcase, actual, 'Invalid source code output',
                               expected_output)
Exemplo n.º 3
0
    def run_case(self, testcase: DataDrivenTestCase) -> None:
        """Perform a data-flow analysis test case."""

        with use_custom_builtins(
                os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase):
            # replace native_int with platform specific ints
            int_format_str = 'int32' if IS_32_BIT_PLATFORM else 'int64'
            testcase.output = [
                s.replace('native_int', int_format_str)
                for s in testcase.output
            ]
            try:
                ir = build_ir_for_single_file(testcase.input)
            except CompileError as e:
                actual = e.messages
            else:
                actual = []
                for fn in ir:
                    if (fn.name == TOP_LEVEL_NAME
                            and not testcase.name.endswith('_toplevel')):
                        continue
                    exceptions.insert_exception_handling(fn)
                    actual.extend(format_func(fn))
                    cfg = analysis.get_cfg(fn.blocks)

                    args = set(reg for reg, i in fn.env.indexes.items()
                               if i < len(fn.args))

                    name = testcase.name
                    if name.endswith('_MaybeDefined'):
                        # Forward, maybe
                        analysis_result = analysis.analyze_maybe_defined_regs(
                            fn.blocks, cfg, args)
                    elif name.endswith('_Liveness'):
                        # Backward, maybe
                        analysis_result = analysis.analyze_live_regs(
                            fn.blocks, cfg)
                    elif name.endswith('_MustDefined'):
                        # Forward, must
                        analysis_result = analysis.analyze_must_defined_regs(
                            fn.blocks, cfg, args, regs=fn.env.regs())
                    elif name.endswith('_BorrowedArgument'):
                        # Forward, must
                        analysis_result = analysis.analyze_borrowed_arguments(
                            fn.blocks, cfg, args)
                    else:
                        assert False, 'No recognized _AnalysisName suffix in test case'

                    for key in sorted(analysis_result.before.keys(),
                                      key=lambda x: (x[0].label, x[1])):
                        pre = ', '.join(
                            sorted(reg.name
                                   for reg in analysis_result.before[key]))
                        post = ', '.join(
                            sorted(reg.name
                                   for reg in analysis_result.after[key]))
                        actual.append('%-8s %-23s %s' %
                                      ((key[0].label, key[1]), '{%s}' % pre,
                                       '{%s}' % post))
            assert_test_output(testcase, actual, 'Invalid source code output')
Exemplo n.º 4
0
 def run_case(self, testcase: DataDrivenTestCase) -> None:
     # setup.py wants to be run from the root directory of the package, which we accommodate
     # by chdiring into tmp/
     with use_custom_builtins(
             os.path.join(self.data_prefix, ICODE_GEN_BUILTINS),
             testcase), (chdir_manager('tmp')):
         self.run_case_inner(testcase)
Exemplo n.º 5
0
    def run_case(self, testcase: DataDrivenTestCase) -> None:
        with use_custom_builtins(
                os.path.join(test_data_prefix, ICODE_GEN_BUILTINS), testcase):
            # Build the program.
            text = '\n'.join(testcase.input)

            options = Options()
            options.use_builtins_fixtures = True
            options.show_traceback = True
            options.strict_optional = True
            source = build.BuildSource('prog.py', 'prog', text)

            try:
                ctext = emitmodule.compile_module_to_c(
                    sources=[source],
                    module_name='prog',
                    options=options,
                    alt_lib_path=test_temp_dir)
                out = ctext.splitlines()
            except CompileError as e:
                out = e.messages

            # Verify output.
            assert_string_arrays_equal_wildcards(
                testcase.output, out,
                'Invalid output ({}, line {})'.format(testcase.file,
                                                      testcase.line))
Exemplo n.º 6
0
    def run_case(self, testcase: DataDrivenTestCase) -> None:
        with use_custom_builtins(
                os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase):
            # Build the program.
            text = '\n'.join(testcase.input)

            options = Options()
            options.use_builtins_fixtures = True
            options.show_traceback = True
            options.strict_optional = True
            options.python_version = (3, 6)
            options.export_types = True
            source = build.BuildSource('prog.py', 'prog', text)

            try:
                ctext = emitmodule.compile_modules_to_c(
                    sources=[source],
                    module_names=['prog'],
                    options=options,
                    alt_lib_path=test_temp_dir)
                out = ctext.splitlines()
            except CompileError as e:
                out = e.messages

            # Verify output.
            assert_test_output(testcase, out, 'Invalid output')
Exemplo n.º 7
0
    def run_case(self, testcase: DataDrivenTestCase) -> None:
        """Perform a runtime checking transformation test case."""
        options = infer_ir_build_options_from_test_name(testcase.name)
        if options is None:
            # Skipped test case
            return
        with use_custom_builtins(
                os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase):
            expected_output = remove_comment_lines(testcase.output)
            expected_output = replace_native_int(expected_output)
            expected_output = replace_word_size(expected_output)
            try:
                ir = build_ir_for_single_file(testcase.input, options)
            except CompileError as e:
                actual = e.messages
            else:
                actual = []
                for fn in ir:
                    if (fn.name == TOP_LEVEL_NAME
                            and not testcase.name.endswith('_toplevel')):
                        continue
                    insert_uninit_checks(fn)
                    insert_ref_count_opcodes(fn)
                    actual.extend(format_func(fn))

            assert_test_output(testcase, actual, 'Invalid source code output',
                               expected_output)
Exemplo n.º 8
0
    def run_case(self, testcase: DataDrivenTestCase) -> None:
        # Kind of hacky. Not sure if we need more structure here.
        options = CompilerOptions(strip_asserts='StripAssert' in testcase.name)
        """Perform a runtime checking transformation test case."""
        with use_custom_builtins(
                os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase):
            expected_output = remove_comment_lines(testcase.output)
            # replace native_int with platform specific ints
            int_format_str = 'int32' if IS_32_BIT_PLATFORM else 'int64'
            expected_output = [
                s.replace('native_int', int_format_str)
                for s in expected_output
            ]
            try:
                ir = build_ir_for_single_file(testcase.input, options)
            except CompileError as e:
                actual = e.messages
            else:
                actual = []
                for fn in ir:
                    if (fn.name == TOP_LEVEL_NAME
                            and not testcase.name.endswith('_toplevel')):
                        continue
                    actual.extend(format_func(fn))

            assert_test_output(testcase, actual, 'Invalid source code output',
                               expected_output)
Exemplo n.º 9
0
    def run_case(self, testcase: DataDrivenTestCase) -> None:
        """Perform a data-flow analysis test case."""

        with use_custom_builtins(
                os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase):
            testcase.output = replace_native_int(testcase.output)
            try:
                ir = build_ir_for_single_file(testcase.input)
            except CompileError as e:
                actual = e.messages
            else:
                actual = []
                for fn in ir:
                    if (fn.name == TOP_LEVEL_NAME
                            and not testcase.name.endswith('_toplevel')):
                        continue
                    exceptions.insert_exception_handling(fn)
                    actual.extend(format_func(fn))
                    cfg = dataflow.get_cfg(fn.blocks)
                    args = set(fn.arg_regs)  # type: Set[Value]
                    name = testcase.name
                    if name.endswith('_MaybeDefined'):
                        # Forward, maybe
                        analysis_result = dataflow.analyze_maybe_defined_regs(
                            fn.blocks, cfg, args)
                    elif name.endswith('_Liveness'):
                        # Backward, maybe
                        analysis_result = dataflow.analyze_live_regs(
                            fn.blocks, cfg)
                    elif name.endswith('_MustDefined'):
                        # Forward, must
                        analysis_result = dataflow.analyze_must_defined_regs(
                            fn.blocks,
                            cfg,
                            args,
                            regs=all_values(fn.arg_regs, fn.blocks))
                    elif name.endswith('_BorrowedArgument'):
                        # Forward, must
                        analysis_result = dataflow.analyze_borrowed_arguments(
                            fn.blocks, cfg, args)
                    else:
                        assert False, 'No recognized _AnalysisName suffix in test case'

                    names = generate_names_for_ir(fn.arg_regs, fn.blocks)

                    for key in sorted(analysis_result.before.keys(),
                                      key=lambda x: (x[0].label, x[1])):
                        pre = ', '.join(
                            sorted(names[reg]
                                   for reg in analysis_result.before[key]))
                        post = ', '.join(
                            sorted(names[reg]
                                   for reg in analysis_result.after[key]))
                        actual.append('%-8s %-23s %s' %
                                      ((key[0].label, key[1]), '{%s}' % pre,
                                       '{%s}' % post))
            assert_test_output(testcase, actual, 'Invalid source code output')
Exemplo n.º 10
0
    def run_case(self, testcase: DataDrivenTestCase) -> None:
        """Perform a runtime checking transformation test case."""
        with use_custom_builtins(
                os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase):
            expected_output = remove_comment_lines(testcase.output)

            try:
                ir = build_ir_for_single_file(testcase.input)
            except CompileError as e:
                actual = e.messages
            else:
                actual = []
                for fn in ir:
                    if (fn.name == TOP_LEVEL_NAME
                            and not testcase.name.endswith('_toplevel')):
                        continue
                    actual.extend(format_func(fn))

            assert_test_output(testcase, actual, 'Invalid source code output',
                               expected_output)
Exemplo n.º 11
0
    def run_case(self, testcase: DataDrivenTestCase) -> None:
        """Perform a reference count opcode insertion transform test case."""

        with use_custom_builtins(
                os.path.join(test_data_prefix, ICODE_GEN_BUILTINS), testcase):
            try:
                ir = build_ir_for_single_file(testcase.input)
            except CompileError as e:
                actual = e.messages
            else:
                # Expect one function + module top level function.
                assert len(
                    ir
                ) == 2, "Only 1 function definition expected per test case"
                fn = ir[0]
                insert_exception_handling(fn)
                insert_ref_count_opcodes(fn)
                actual = format_func(fn)
                actual = actual[actual.index('L0:'):]

            assert_test_output(testcase, actual, 'Invalid source code output')
Exemplo n.º 12
0
    def run_case(self, testcase: DataDrivenTestCase) -> None:
        """Perform a runtime checking transformation test case."""
        with use_custom_builtins(
                os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase):
            expected_output = remove_comment_lines(testcase.output)

            program_text = '\n'.join(testcase.input)

            options = Options()
            options.use_builtins_fixtures = True
            options.show_traceback = True
            options.strict_optional = True
            options.python_version = (3, 6)
            options.export_types = True

            source = build.BuildSource('main', '__main__', program_text)
            try:
                # Construct input as a single single.
                # Parse and type check the input program.
                result = build.build(sources=[source],
                                     options=options,
                                     alt_lib_path=test_temp_dir)
            except CompileError as e:
                actual = e.messages
            else:
                if result.errors:
                    actual = result.errors
                else:
                    modules = genops.build_ir([result.files['__main__']],
                                              result.types)
                    module = modules[0][1]
                    actual = []
                    for fn in module.functions:
                        if is_empty_module_top_level(fn):
                            # Skip trivial module top levels that only return.
                            continue
                        actual.extend(format_func(fn))

            assert_test_output(testcase, actual, 'Invalid source code output',
                               expected_output)
Exemplo n.º 13
0
    def run_case(self, testcase: DataDrivenTestCase) -> None:
        """Perform a runtime checking transformation test case."""
        options = infer_ir_build_options_from_test_name(testcase.name)
        if options is None:
            # Skipped test case
            return
        with use_custom_builtins(
                os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase):
            try:
                ir = build_ir_for_single_file2(testcase.input, options)
            except CompileError as e:
                actual = e.messages
            else:
                actual = []
                for cl in ir.classes:
                    if cl.name.startswith('_'):
                        continue
                    actual.append('{}: [{}]'.format(
                        cl.name,
                        ', '.join(sorted(cl._always_initialized_attrs))))

            assert_test_output(testcase, actual, 'Invalid test output',
                               testcase.output)
Exemplo n.º 14
0
    def run_case(self, testcase: DataDrivenTestCase) -> None:
        """Perform a reference count opcode insertion transform test case."""

        with use_custom_builtins(
                os.path.join(test_data_prefix, ICODE_GEN_BUILTINS), testcase):
            try:
                ir = build_ir_for_single_file(testcase.input)
            except CompileError as e:
                actual = e.messages
            else:
                assert len(
                    ir
                ) == 1, "Only 1 function definition expected per test case"
                fn = ir[0]
                insert_ref_count_opcodes(fn)
                actual = format_func(fn)
                actual = actual[actual.index('L0:'):]

            expected_output = testcase.output
            assert_string_arrays_equal_wildcards(
                expected_output, actual,
                'Invalid source code output ({}, line {})'.format(
                    testcase.file, testcase.line))
Exemplo n.º 15
0
    def run_case(self, testcase: DataDrivenTestCase) -> None:
        """Perform a data-flow analysis test case."""

        with use_custom_builtins(
                os.path.join(test_data_prefix, ICODE_GEN_BUILTINS), testcase):
            expected_output = testcase.output
            program_text = '\n'.join(testcase.input)

            options = Options()
            options.use_builtins_fixtures = True
            options.show_traceback = True

            source = build.BuildSource('main', '__main__', program_text)
            try:
                # Construct input as a single single.
                # Parse and type check the input program.
                result = build.build(sources=[source],
                                     options=options,
                                     alt_lib_path=test_temp_dir)
            except CompileError as e:
                actual = e.messages
            else:
                if result.errors:
                    actual = result.errors
                else:
                    module = genops.build_ir(result.files['__main__'],
                                             result.types)
                    assert len(
                        module.functions
                    ) == 1, "Only 1 function definition expected per test case"
                    fn = module.functions[0]
                    actual = format_func(fn)
                    actual = actual[actual.index('L0:'):]
                    cfg = analysis.get_cfg(fn.blocks)

                    args = set([Register(i) for i in range(len(fn.args))])
                    name = testcase.name
                    if name.endswith('_MaybeDefined'):
                        # Forward, maybe
                        analysis_result = analysis.analyze_maybe_defined_regs(
                            fn.blocks, cfg, args)
                    elif name.endswith('_Liveness'):
                        # Backward, maybe
                        analysis_result = analysis.analyze_live_regs(
                            fn.blocks, cfg)
                    elif name.endswith('_MustDefined'):
                        # Forward, must
                        analysis_result = analysis.analyze_must_defined_regs(
                            fn.blocks, cfg, args, num_regs=fn.env.num_regs())
                    elif name.endswith('_BorrowedArgument'):
                        # Forward, must
                        analysis_result = analysis.analyze_borrowed_arguments(
                            fn.blocks, cfg, args)
                    else:
                        assert False, 'No recognized _AnalysisName suffix in test case'

                    actual.append('')
                    for key in sorted(analysis_result.before.keys()):
                        pre = ', '.join(fn.env.names[reg]
                                        for reg in analysis_result.before[key])
                        post = ', '.join(fn.env.names[reg]
                                         for reg in analysis_result.after[key])
                        actual.append('%-8s %-23s %s' %
                                      (key, '{%s}' % pre, '{%s}' % post))
            assert_string_arrays_equal_wildcards(
                expected_output, actual,
                'Invalid source code output ({}, line {})'.format(
                    testcase.file, testcase.line))
Exemplo n.º 16
0
    def run_case(self, testcase: DataDrivenTestCase) -> None:
        bench = testcase.config.getoption(
            '--bench', False) and 'Benchmark' in testcase.name

        with use_custom_builtins(
                os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase):
            text = '\n'.join(testcase.input)

            options = Options()
            options.use_builtins_fixtures = True
            options.show_traceback = True
            options.strict_optional = True
            options.python_version = (3, 6)
            options.export_types = True

            os.mkdir('tmp/py')
            source_path = 'tmp/py/native.py'
            with open(source_path, 'w') as f:
                f.write(text)
            with open('tmp/interpreted.py', 'w') as f:
                f.write(text)

            source = build.BuildSource(source_path, 'native', text)
            sources = [source]
            module_names = ['native']

            # Hard code another module name to compile in the same compilation unit.
            to_delete = []
            for fn, text in testcase.files:
                if os.path.basename(fn).startswith('other'):
                    name = os.path.basename(fn).split('.')[0]
                    module_names.append(name)
                    sources.append(build.BuildSource(fn, name, text))
                    to_delete.append(fn)

            try:
                ctext = emitmodule.compile_modules_to_c(
                    sources=sources,
                    module_names=module_names,
                    options=options,
                    alt_lib_path=test_temp_dir)
            except CompileError as e:
                for line in e.messages:
                    print(line)
                assert False, 'Compile error'

            # If compiling more than one native module, compile a shared
            # library that contains all the modules. Also generate shims that
            # just call into the shared lib.
            use_shared_lib = len(module_names) > 1

            if use_shared_lib:
                common_path = os.path.join(test_temp_dir, '__shared_stuff.c')
                with open(common_path, 'w') as f:
                    f.write(ctext)
                try:
                    shared_lib = buildc.build_shared_lib_for_modules(
                        common_path, module_names)
                except buildc.BuildError as err:
                    show_c_error(common_path, err.output)
                    raise

            for mod in module_names:
                cpath = os.path.join(test_temp_dir, '%s.c' % mod)
                with open(cpath, 'w') as f:
                    f.write(ctext)

                try:
                    if use_shared_lib:
                        native_lib_path = buildc.build_c_extension_shim(
                            mod, shared_lib)
                    else:
                        native_lib_path = buildc.build_c_extension(
                            cpath, mod, preserve_setup=True)
                except buildc.BuildError as err:
                    show_c_error(cpath, err.output)
                    raise

            # # TODO: is the location of the shared lib good?
            # shared_lib = buildc.build_shared_lib_for_modules(cpath)

            for p in to_delete:
                os.remove(p)

            driver_path = os.path.join(test_temp_dir, 'driver.py')
            env = os.environ.copy()
            path = [
                os.path.dirname(native_lib_path),
                os.path.join(PREFIX, 'extensions')
            ]
            env['PYTHONPATH'] = ':'.join(path)
            env['MYPYC_RUN_BENCH'] = '1' if bench else '0'
            env['LD_LIBRARY_PATH'] = os.getcwd()

            proc = subprocess.Popen(['python', driver_path],
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.STDOUT,
                                    env=env)
            output, _ = proc.communicate()
            output = output.decode('utf8')
            outlines = output.splitlines()

            heading('Generated C')
            with open(cpath) as f:
                print(f.read().rstrip())
            heading('End C')
            if proc.returncode != 0:
                print()
                print('*** Exit status: %d' % proc.returncode)

            # Verify output.
            if bench:
                print('Test output:')
                print(output)
            else:
                assert_test_output(testcase, outlines, 'Invalid output')

            assert proc.returncode == 0
Exemplo n.º 17
0
    def run_case(self, testcase: DataDrivenTestCase) -> None:
        with use_custom_builtins(
                os.path.join(test_data_prefix, ICODE_GEN_BUILTINS), testcase):
            text = '\n'.join(testcase.input)

            options = Options()
            options.use_builtins_fixtures = True
            options.show_traceback = True
            options.strict_optional = True
            source = build.BuildSource('native.py', 'native', text)

            try:
                ctext = emitmodule.compile_module_to_c(
                    sources=[source],
                    module_name='native',
                    options=options,
                    alt_lib_path=test_temp_dir)
            except CompileError as e:
                for line in e.messages:
                    print(line)
                assert False, 'Compile error'

            cpath = os.path.join(test_temp_dir, 'native.c')
            with open(cpath, 'w') as f:
                f.write(ctext)

            try:
                native_lib_path = buildc.build_c_extension(cpath)
            except buildc.BuildError as err:
                heading('Generated C')
                with open(cpath) as f:
                    print(f.read().rstrip())
                heading('End C')
                heading('Build output')
                print(err.output.decode('utf8').rstrip('\n'))
                heading('End output')
                raise

            driver_path = os.path.join(test_temp_dir, 'driver.py')
            env = os.environ.copy()
            env['PYTHONPATH'] = os.path.dirname(native_lib_path)
            proc = subprocess.Popen(['python', driver_path],
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.STDOUT,
                                    env=env)
            output, _ = proc.communicate()
            output = output.decode('utf8')
            outlines = output.splitlines()

            heading('Generated C')
            with open(cpath) as f:
                print(f.read().rstrip())
            heading('End C')
            if proc.returncode != 0:
                print()
                print('*** Exit status: %d' % proc.returncode)

            # Verify output.
            assert_string_arrays_equal_wildcards(
                testcase.output, outlines,
                'Invalid output ({}, line {})'.format(testcase.file,
                                                      testcase.line))

            assert proc.returncode == 0
Exemplo n.º 18
0
    def run_case(self, testcase: DataDrivenTestCase) -> None:
        """Perform a data-flow analysis test case."""

        with use_custom_builtins(os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase):
            program_text = '\n'.join(testcase.input)

            options = Options()
            options.use_builtins_fixtures = True
            options.show_traceback = True
            options.python_version = (3, 6)
            options.export_types = True

            source = build.BuildSource('main', '__main__', program_text)
            try:
                # Construct input as a single single.
                # Parse and type check the input program.
                result = build.build(sources=[source],
                                     options=options,
                                     alt_lib_path=test_temp_dir)
            except CompileError as e:
                actual = e.messages
            else:
                if result.errors:
                    actual = result.errors
                else:
                    modules = genops.build_ir([result.files['__main__']], result.types)
                    module = modules[0][1]
                    assert len(module.functions) == 2, (
                        "Only 1 function definition expected per test case")
                    fn = module.functions[0]
                    actual = format_func(fn)
                    actual = actual[actual.index('L0:'):]
                    cfg = analysis.get_cfg(fn.blocks)

                    args = set(reg for reg, i in fn.env.indexes.items() if i < len(fn.args))

                    name = testcase.name
                    if name.endswith('_MaybeDefined'):
                        # Forward, maybe
                        analysis_result = analysis.analyze_maybe_defined_regs(fn.blocks, cfg, args)
                    elif name.endswith('_Liveness'):
                        # Backward, maybe
                        analysis_result = analysis.analyze_live_regs(fn.blocks, cfg)
                    elif name.endswith('_MustDefined'):
                        # Forward, must
                        analysis_result = analysis.analyze_must_defined_regs(
                            fn.blocks, cfg, args,
                            regs=fn.env.regs())
                    elif name.endswith('_BorrowedArgument'):
                        # Forward, must
                        analysis_result = analysis.analyze_borrowed_arguments(fn.blocks, cfg, args)
                    else:
                        assert False, 'No recognized _AnalysisName suffix in test case'

                    actual.append('')
                    for key in sorted(analysis_result.before.keys(),
                                      key=lambda x: (x[0].label, x[1])):
                        pre = ', '.join(sorted(reg.name
                                               for reg in analysis_result.before[key]))
                        post = ', '.join(sorted(reg.name
                                                for reg in analysis_result.after[key]))
                        actual.append('%-8s %-23s %s' % ((key[0].label, key[1]),
                                                         '{%s}' % pre, '{%s}' % post))
            assert_test_output(testcase, actual, 'Invalid source code output')
Exemplo n.º 19
0
    def run_case(self, testcase: DataDrivenTestCase) -> None:
        bench = testcase.config.getoption('--bench', False) and 'Benchmark' in testcase.name

        # setup.py wants to be run from the root directory of the package, which we accommodate
        # by chdiring into tmp/
        with use_custom_builtins(os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase), (
                chdir_manager('tmp')):
            text = '\n'.join(testcase.input)

            options = Options()
            options.use_builtins_fixtures = True
            options.show_traceback = True
            options.strict_optional = True
            # N.B: We try to (and ought to!) run with the current
            # version of python, since we are going to link and run
            # against the current version of python.
            # But a lot of the tests use type annotations so we can't say it is 3.5.
            options.python_version = max(sys.version_info[:2], (3, 6))
            options.export_types = True
            options.preserve_asts = True

            # Avoid checking modules/packages named 'unchecked', to provide a way
            # to test interacting with code we don't have types for.
            options.per_module_options['unchecked.*'] = {'follow_imports': 'error'}

            workdir = 'build'
            os.mkdir(workdir)

            source_path = 'native.py'
            with open(source_path, 'w', encoding='utf-8') as f:
                f.write(text)
            with open('interpreted.py', 'w', encoding='utf-8') as f:
                f.write(text)

            shutil.copyfile(TESTUTIL_PATH, 'testutil.py')

            source = build.BuildSource(source_path, 'native', text)
            sources = [source]
            module_names = ['native']
            module_paths = [os.path.abspath('native.py')]

            # Hard code another module name to compile in the same compilation unit.
            to_delete = []
            for fn, text in testcase.files:
                fn = os.path.relpath(fn, test_temp_dir)

                if os.path.basename(fn).startswith('other'):
                    name = os.path.basename(fn).split('.')[0]
                    module_names.append(name)
                    sources.append(build.BuildSource(fn, name, text))
                    to_delete.append(fn)
                    module_paths.append(os.path.abspath(fn))

                    shutil.copyfile(fn,
                                    os.path.join(os.path.dirname(fn), name + '_interpreted.py'))

            for source in sources:
                options.per_module_options.setdefault(source.module, {})['mypyc'] = True

            if len(module_names) == 1:
                lib_name = None  # type: Optional[str]
            else:
                lib_name = shared_lib_name([source.module for source in sources])

            try:
                result = emitmodule.parse_and_typecheck(
                    sources=sources,
                    options=options,
                    alt_lib_path='.')
                errors = Errors()
                compiler_options = CompilerOptions(multi_file=self.multi_file)
                cfiles = emitmodule.compile_modules_to_c(
                    result,
                    module_names=module_names,
                    shared_lib_name=lib_name,
                    compiler_options=compiler_options,
                    errors=errors,
                )
                if errors.num_errors:
                    errors.flush_errors()
                    assert False, "Compile error"
            except CompileError as e:
                for line in e.messages:
                    print(line)
                assert False, 'Compile error'

            for cfile, ctext in cfiles:
                with open(os.path.join(workdir, cfile), 'w', encoding='utf-8') as f:
                    f.write(ctext)

            setup_file = os.path.abspath(os.path.join(workdir, 'setup.py'))
            with open(setup_file, 'w') as f:
                f.write(setup_format.format(module_paths))

            if not run_setup(setup_file, ['build_ext', '--inplace']):
                if testcase.config.getoption('--mypyc-showc'):
                    show_c(cfiles)
                assert False, "Compilation failed"

            # Assert that an output file got created
            suffix = 'pyd' if sys.platform == 'win32' else 'so'
            assert glob.glob('native.*.{}'.format(suffix))

            for p in to_delete:
                os.remove(p)

            driver_path = 'driver.py'
            env = os.environ.copy()
            env['MYPYC_RUN_BENCH'] = '1' if bench else '0'

            # XXX: This is an ugly hack.
            if 'MYPYC_RUN_GDB' in os.environ:
                if platform.system() == 'Darwin':
                    subprocess.check_call(['lldb', '--', sys.executable, driver_path], env=env)
                    assert False, ("Test can't pass in lldb mode. (And remember to pass -s to "
                                   "pytest)")
                elif platform.system() == 'Linux':
                    subprocess.check_call(['gdb', '--args', sys.executable, driver_path], env=env)
                    assert False, ("Test can't pass in gdb mode. (And remember to pass -s to "
                                   "pytest)")
                else:
                    assert False, 'Unsupported OS'

            proc = subprocess.Popen([sys.executable, driver_path], stdout=subprocess.PIPE,
                                    stderr=subprocess.STDOUT, env=env)
            output = proc.communicate()[0].decode('utf8')
            outlines = output.splitlines()

            if testcase.config.getoption('--mypyc-showc'):
                show_c(cfiles)
            if proc.returncode != 0:
                print()
                print('*** Exit status: %d' % proc.returncode)

            # Verify output.
            if bench:
                print('Test output:')
                print(output)
            else:
                assert_test_output(testcase, outlines, 'Invalid output')

            assert proc.returncode == 0
Exemplo n.º 20
0
    def run_case(self, testcase: DataDrivenTestCase) -> None:
        bench = testcase.config.getoption('--bench', False) and 'Benchmark' in testcase.name

        # setup.py wants to be run from the root directory of the package, which we accommodate
        # by chdiring into tmp/
        with use_custom_builtins(os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase), (
                chdir_manager('tmp')):
            text = '\n'.join(testcase.input)

            options = Options()
            options.use_builtins_fixtures = True
            options.show_traceback = True
            options.strict_optional = True
            options.python_version = (3, 6)
            options.export_types = True

            workdir = 'build'
            os.mkdir(workdir)

            source_path = 'native.py'
            with open(source_path, 'w') as f:
                f.write(text)
            with open('interpreted.py', 'w') as f:
                f.write(text)

            source = build.BuildSource(source_path, 'native', text)
            sources = [source]
            module_names = ['native']
            module_paths = [os.path.abspath('native.py')]

            # Hard code another module name to compile in the same compilation unit.
            to_delete = []
            for fn, text in testcase.files:
                fn = os.path.relpath(fn, test_temp_dir)

                if os.path.basename(fn).startswith('other'):
                    name = os.path.basename(fn).split('.')[0]
                    module_names.append(name)
                    sources.append(build.BuildSource(fn, name, text))
                    to_delete.append(fn)
                    module_paths.append(os.path.abspath(fn))

            for source in sources:
                options.per_module_options.setdefault(source.module, {})['mypyc'] = True

            try:
                result = emitmodule.parse_and_typecheck(
                    sources=sources,
                    options=options,
                    alt_lib_path='.')
                ctext = emitmodule.compile_modules_to_c(
                    result,
                    module_names=module_names,
                    use_shared_lib=len(module_names) > 1)
            except CompileError as e:
                for line in e.messages:
                    print(line)
                assert False, 'Compile error'

            cpath = os.path.abspath(os.path.join(workdir, '__native.c'))
            with open(cpath, 'w') as f:
                f.write(ctext)

            setup_file = os.path.abspath(os.path.join(workdir, 'setup.py'))
            with open(setup_file, 'w') as f:
                f.write(setup_format.format(module_paths))

            run_setup(setup_file, ['build_ext', '--inplace'])
            # Oh argh run_setup doesn't propagate failure. For now we'll just assert
            # that the file is there.
            if not glob.glob('native.*.so'):
                show_c(ctext)
                assert False, "Compilation failed"

            for p in to_delete:
                os.remove(p)

            driver_path = 'driver.py'
            env = os.environ.copy()
            env['MYPYC_RUN_BENCH'] = '1' if bench else '0'

            # XXX: This is an ugly hack.
            if 'MYPYC_RUN_GDB' in os.environ:
                if platform.system() == 'Darwin':
                    subprocess.check_call(['lldb', '--', 'python', driver_path], env=env)
                    assert False, ("Test can't pass in lldb mode. (And remember to pass -s to "
                                   "pytest)")
                elif platform.system() == 'Linux':
                    subprocess.check_call(['gdb', '--args', 'python', driver_path], env=env)
                    assert False, ("Test can't pass in gdb mode. (And remember to pass -s to "
                                   "pytest)")
                else:
                    assert False, 'Unsupported OS'

            proc = subprocess.Popen(['python', driver_path], stdout=subprocess.PIPE,
                                    stderr=subprocess.STDOUT, env=env)
            output, _ = proc.communicate()
            output = output.decode('utf8')
            outlines = output.splitlines()

            show_c(ctext)
            if proc.returncode != 0:
                print()
                print('*** Exit status: %d' % proc.returncode)

            # Verify output.
            if bench:
                print('Test output:')
                print(output)
            else:
                assert_test_output(testcase, outlines, 'Invalid output')

            assert proc.returncode == 0