コード例 #1
0
ファイル: testutil.py プロジェクト: vemel/mypy
def build_ir_for_single_file(
        input_lines: List[str],
        compiler_options: Optional[CompilerOptions] = None) -> List[FuncIR]:
    program_text = '\n'.join(input_lines)

    # By default generate IR compatible with the earliest supported Python C API.
    # If a test needs more recent API features, this should be overridden.
    compiler_options = compiler_options or CompilerOptions(capi_version=(3, 5))
    options = Options()
    options.show_traceback = True
    options.use_builtins_fixtures = True
    options.strict_optional = True
    options.python_version = (3, 6)
    options.export_types = True
    options.preserve_asts = True
    options.per_module_options['__main__'] = {'mypyc': True}

    source = build.BuildSource('main', '__main__', program_text)
    # 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)
    if result.errors:
        raise CompileError(result.errors)

    errors = Errors()
    modules = build_ir([result.files['__main__']], result.graph, result.types,
                       Mapper({'__main__': None}), compiler_options, errors)
    if errors.num_errors:
        raise CompileError(errors.new_messages())

    module = list(modules.values())[0]
    return module.functions
コード例 #2
0
ファイル: build.py プロジェクト: popzxc/mypy
def generate_c(sources: List[BuildSource],
               options: Options,
               groups: emitmodule.Groups,
               fscache: FileSystemCache,
               compiler_options: CompilerOptions,
               ) -> Tuple[List[List[Tuple[str, str]]], str]:
    """Drive the actual core compilation step.

    The groups argument describes how modules are assigned to C
    extension modules. See the comments on the Groups type in
    mypyc.emitmodule for details.

    Returns the C source code and (for debugging) the pretty printed IR.
    """
    t0 = time.time()

    # Do the actual work now
    serious = False
    result = None
    try:
        result = emitmodule.parse_and_typecheck(
            sources, options, compiler_options, groups, fscache)
        messages = result.errors
    except CompileError as e:
        messages = e.messages
        if not e.use_stdout:
            serious = True

    t1 = time.time()
    if compiler_options.verbose:
        print("Parsed and typechecked in {:.3f}s".format(t1 - t0))

    if not messages and result:
        errors = Errors()
        modules, ctext = emitmodule.compile_modules_to_c(
            result, compiler_options=compiler_options, errors=errors, groups=groups)

        if errors.num_errors:
            messages.extend(errors.new_messages())

    t2 = time.time()
    if compiler_options.verbose:
        print("Compiled to C in {:.3f}s".format(t2 - t1))

    # ... you know, just in case.
    if options.junit_xml:
        py_version = "{}_{}".format(
            options.python_version[0], options.python_version[1]
        )
        write_junit_xml(
            t2 - t0, serious, messages, options.junit_xml, py_version, options.platform
        )

    if messages:
        print("\n".join(messages))
        sys.exit(1)

    return ctext, '\n'.join(format_modules(modules))