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 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))
def generate_c( sources: List[BuildSource], options: Options, groups: emitmodule.Groups, compiler_options: Optional[CompilerOptions] = None ) -> 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. """ compiler_options = compiler_options or CompilerOptions() # Do the actual work now t0 = time.time() try: result = emitmodule.parse_and_typecheck(sources, options) except CompileError as e: for line in e.messages: print(line) fail('Typechecking failure') t1 = time.time() if compiler_options.verbose: print("Parsed and typechecked in {:.3f}s".format(t1 - t0)) all_module_names = [] for group_sources, _ in groups: all_module_names.extend([source.module for source in group_sources]) errors = Errors() ops = [] # type: List[str] ctext = emitmodule.compile_modules_to_c(result, compiler_options=compiler_options, errors=errors, ops=ops, groups=groups) if errors.num_errors: errors.flush_errors() sys.exit(1) t2 = time.time() if compiler_options.verbose: print("Compiled to C in {:.3f}s".format(t2 - t1)) return ctext, '\n'.join(ops)
def generate_c( sources: List[BuildSource], options: Options, shared_lib_name: Optional[str], compiler_options: Optional[CompilerOptions] = None ) -> Tuple[List[Tuple[str, str]], str]: """Drive the actual core compilation step. Returns the C source code and (for debugging) the pretty printed IR. """ module_names = [source.module for source in sources] compiler_options = compiler_options or CompilerOptions() # Do the actual work now t0 = time.time() try: result = emitmodule.parse_and_typecheck(sources, options) except CompileError as e: for line in e.messages: print(line) fail('Typechecking failure') t1 = time.time() if compiler_options.verbose: print("Parsed and typechecked in {:.3f}s".format(t1 - t0)) errors = Errors() ops = [] # type: List[str] ctext = emitmodule.compile_modules_to_c(result, module_names, shared_lib_name, compiler_options=compiler_options, errors=errors, ops=ops) if errors.num_errors: errors.flush_errors() sys.exit(1) t2 = time.time() if compiler_options.verbose: print("Compiled to C in {:.3f}s".format(t2 - t1)) return ctext, '\n'.join(ops)
def run_case_step(self, testcase: DataDrivenTestCase, incremental_step: int) -> None: bench = testcase.config.getoption( '--bench', False) and 'Benchmark' in testcase.name 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 options.incremental = False # 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'} source = build.BuildSource('native.py', 'native', None) sources = [source] module_names = ['native'] module_paths = ['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') and fn.endswith('.py'): name = os.path.basename(fn).split('.')[0] module_names.append(name) sources.append(build.BuildSource(fn, name, None)) to_delete.append(fn) module_paths.append(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 separate = (self.get_separate('\n'.join( testcase.input), incremental_step) if self.separate else False) groups = construct_groups(sources, separate, len(module_names) > 1) try: result = emitmodule.parse_and_typecheck(sources=sources, options=options, alt_lib_path='.') errors = Errors() compiler_options = CompilerOptions(multi_file=self.multi_file, separate=self.separate) ir, cfiles = emitmodule.compile_modules_to_c( result, compiler_options=compiler_options, errors=errors, groups=groups, ) 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' # Check that serialization works on this IR check_serialization_roundtrip(ir) setup_file = os.path.abspath(os.path.join(WORKDIR, 'setup.py')) # We pass the C file information to the build script via setup.py unfortunately with open(setup_file, 'w', encoding='utf-8') as f: f.write( setup_format.format(module_paths, separate, cfiles, self.multi_file)) 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)) 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: if incremental_step == 1: msg = 'Invalid output' expected = testcase.output else: msg = 'Invalid output (step {})'.format(incremental_step) expected = testcase.output2.get(incremental_step, []) assert_test_output(testcase, outlines, msg, expected) if incremental_step > 1 and options.incremental: suffix = '' if incremental_step == 2 else str(incremental_step - 1) expected_rechecked = testcase.expected_rechecked_modules.get( incremental_step - 1) if expected_rechecked is not None: assert_module_equivalence('rechecked' + suffix, expected_rechecked, result.manager.rechecked_modules) expected_stale = testcase.expected_stale_modules.get( incremental_step - 1) if expected_stale is not None: assert_module_equivalence('stale' + suffix, expected_stale, result.manager.stale_modules) assert proc.returncode == 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
def run_case_step(self, testcase: DataDrivenTestCase, incremental_step: int) -> None: bench = testcase.config.getoption('--bench', False) and 'Benchmark' in testcase.name options = Options() options.use_builtins_fixtures = True options.show_traceback = True options.strict_optional = True options.python_version = sys.version_info[:2] options.export_types = True options.preserve_asts = True options.incremental = self.separate # 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'} source = build.BuildSource('native.py', 'native', None) sources = [source] module_names = ['native'] module_paths = ['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') and fn.endswith('.py'): name = fn.split('.')[0].replace(os.sep, '.') module_names.append(name) sources.append(build.BuildSource(fn, name, None)) to_delete.append(fn) module_paths.append(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 separate = (self.get_separate('\n'.join(testcase.input), incremental_step) if self.separate else False) groups = construct_groups(sources, separate, len(module_names) > 1) try: compiler_options = CompilerOptions(multi_file=self.multi_file, separate=self.separate) result = emitmodule.parse_and_typecheck( sources=sources, options=options, compiler_options=compiler_options, groups=groups, alt_lib_path='.') errors = Errors() ir, cfiles = emitmodule.compile_modules_to_c( result, compiler_options=compiler_options, errors=errors, groups=groups, ) if errors.num_errors: errors.flush_errors() assert False, "Compile error" except CompileError as e: for line in e.messages: print(fix_native_line_number(line, testcase.file, testcase.line)) assert False, 'Compile error' # Check that serialization works on this IR. (Only on the first # step because the the returned ir only includes updated code.) if incremental_step == 1: check_serialization_roundtrip(ir) opt_level = int(os.environ.get('MYPYC_OPT_LEVEL', 0)) debug_level = int(os.environ.get('MYPYC_DEBUG_LEVEL', 0)) setup_file = os.path.abspath(os.path.join(WORKDIR, 'setup.py')) # We pass the C file information to the build script via setup.py unfortunately with open(setup_file, 'w', encoding='utf-8') as f: f.write(setup_format.format(module_paths, separate, cfiles, self.multi_file, opt_level, debug_level)) 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(f'native.*.{suffix}') or glob.glob(f'native.{suffix}') driver_path = 'driver.py' if not os.path.isfile(driver_path): # No driver.py provided by test case. Use the default one # (mypyc/test-data/driver/driver.py) that calls each # function named test_*. default_driver = os.path.join( os.path.dirname(__file__), '..', 'test-data', 'driver', 'driver.py') shutil.copy(default_driver, driver_path) 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: if incremental_step == 1: msg = 'Invalid output' expected = testcase.output else: msg = f'Invalid output (step {incremental_step})' expected = testcase.output2.get(incremental_step, []) if not expected: # Tweak some line numbers, but only if the expected output is empty, # as tweaked output might not match expected output. outlines = [fix_native_line_number(line, testcase.file, testcase.line) for line in outlines] assert_test_output(testcase, outlines, msg, expected) if incremental_step > 1 and options.incremental: suffix = '' if incremental_step == 2 else str(incremental_step - 1) expected_rechecked = testcase.expected_rechecked_modules.get(incremental_step - 1) if expected_rechecked is not None: assert_module_equivalence( 'rechecked' + suffix, expected_rechecked, result.manager.rechecked_modules) expected_stale = testcase.expected_stale_modules.get(incremental_step - 1) if expected_stale is not None: assert_module_equivalence( 'stale' + suffix, expected_stale, result.manager.stale_modules) assert proc.returncode == 0