def build_ir_for_single_file(input_lines: List[str], compiler_options: Optional[CompilerOptions] = None) -> List[FuncIR]: program_text = '\n'.join(input_lines) compiler_options = compiler_options or CompilerOptions() 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: errors.flush_errors() pytest.fail('Errors while building IR') module = list(modules.values())[0] return module.functions
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
def compile_scc_to_ir( scc: List[MypyFile], result: BuildResult, mapper: Mapper, compiler_options: CompilerOptions, errors: Errors, ) -> ModuleIRs: """Compile an SCC into ModuleIRs. Any modules that this SCC depends on must have either compiled or loaded from a cache into mapper. Arguments: scc: The list of MypyFiles to compile result: The BuildResult from the mypy front-end mapper: The Mapper object mapping mypy ASTs to class and func IRs compiler_options: The compilation options errors: Where to report any errors encountered Returns the IR of the modules. """ if compiler_options.verbose: print("Compiling {}".format(", ".join(x.name for x in scc))) # Generate basic IR, with missing exception and refcount handling. modules = build_ir( scc, result.graph, result.types, mapper, compiler_options, errors ) if errors.num_errors > 0: return modules # Insert uninit checks. for module in modules.values(): for fn in module.functions: insert_uninit_checks(fn) # Insert exception handling. for module in modules.values(): for fn in module.functions: insert_exception_handling(fn) # Insert refcount handling. for module in modules.values(): for fn in module.functions: insert_ref_count_opcodes(fn) return modules