def run_test_with_prelude(self, code, prelude, *params, **interface): """Test if a function call return value is unchanged when executed using python eval or compiled with pythran. Args: code (str): python (pythran valid) module to test. params (tuple): arguments to pass to the function to test. prelude (fct): function to call between 'code' and the c++ generated code interface (dict): pythran interface for the module to test. Each key is the name of a function to call, the value is a list of the arguments' type. Returns: nothing. Raises: AssertionError by 'unittest' if return value differ. SyntaxError if code is not python valid. CalledProcessError if pythran generated code cannot be compiled. ...possible others... """ for name in sorted(interface.keys()): # Build the python function call. modname = "test_" + name arglist = ",".join(("'{0}'".format(p) if isinstance(p, str) else str(p)) for p in params) function_call = "{0}({1})".format(name, arglist) # Compile the python module, python-way, 'env' contains the module # and allow to call functions. # This may be done once before the loop, but the context might # need to be reset. compiled_code = compile(code, "", "exec") env = {'__builtin__' : __import__('__builtin__')} prelude and prelude() eval(compiled_code, env) python_ref = eval(function_call, env) # Produce the reference # Compile the code using pythran cxx_code = cxx_generator(modname, code, interface) cxx_compiled = pythran_compile(os.environ.get("CXX", "c++"), cxx_code, cxxflags=TestEnv.PYTHRAN_CXX_FLAGS, check=True) prelude and prelude() pymod = load_dynamic(modname, cxx_compiled) # Produce the pythran result pythran_res = getattr(pymod, name)(*params) # Compare pythran result against python ref and raise if mismatch if python_ref != pythran_res: print "Python result: ", python_ref print "Pythran result: ", pythran_res self.assertAlmostEqual(python_ref, pythran_res)
def __call__(self, check_output=False): module_path = os.path.join(os.path.dirname(__file__), "openmp", self.module_name + ".py") print self.module_name specs = {self.module_name: []} module_code = file(module_path).read() if "unittest.skip" in module_code: return self.skipTest("Marked as skipable") mod = pythran.cxx_generator(self.module_name, module_code, specs) pymod = load_dynamic(self.module_name, pythran.compile(os.environ.get("CXX", "c++"), mod, check=False, cxxflags=['-O0', '-fopenmp'])) res = getattr(pymod, self.module_name)() assert res, 'Test Failed'
def __call__(self, check_output=False): module_path=os.path.join(os.path.dirname(__file__),"rosetta",self.module_name+".py") print self.module_name specs = { "test": [] } module_code = file(module_path).read() if "unittest.skip" in module_code: return self.skipTest("Marked as skipable") mod = pythran.cxx_generator(self.module_name, module_code, specs) pymod=load_dynamic(self.module_name, pythran.compile(os.environ.get("CXX","c++"), mod, check=False)) if check_output: res = getattr(pymod,"test")() compiled_code=compile(file(module_path).read(),"","exec") env={} eval(compiled_code, env) ref=eval("test()",env) if ref != res: print ref, res
def run(self): import glob import timeit from pythran import cxx_generator, spec_parser from pythran import compile as pythran_compile where = "pythran/tests/cases/" candidates = glob.glob(where + '*.py') sys.path.append(where) median = lambda x: sorted(x)[len(x) / 2] for candidate in candidates: with file(candidate) as content: runas = [line for line in content.readlines() if line.startswith(BenchmarkCommand.runas_marker)] if runas: module_name, _ = os.path.splitext( os.path.basename(candidate)) runas_commands = runas[0].replace( BenchmarkCommand.runas_marker, '').split(";") runas_context = ";".join(["import {0}".format( module_name)] + runas_commands[:-1]) runas_command = module_name + '.' + runas_commands[-1] # cleaning sopath = module_name + ".so" if os.path.exists(sopath): os.remove(sopath) ti = timeit.Timer(runas_command, runas_context) # pythran part if self.mode.startswith('pythran'): specs = spec_parser(candidate) code = file(candidate).read() mod = cxx_generator(module_name, code, specs) cxxflags = ["-Ofast", "-DNDEBUG"] if self.mode == "pythran+omp": cxxflags.append("-fopenmp") pythran_compile(os.environ.get("CXX", "c++"), mod, cxxflags=cxxflags) timing = median(ti.repeat(self.nb_iter, number=1)) print module_name, timing
def run(self): import os for extension in self.extensions: source = extension.sources[0] out = extension.name + '.so' if not path.isfile( out) or path.getmtime(source) > path.getmtime(out): print 'building pythran extension', extension.name content = file(source).read() module = pythran.cxx_generator(extension.name, content, pythran.spec_parser(content), []) pythran.compile(os.environ.get('CXX', 'c++'), module, out, cxxflags=['-O2', '-g'] + extension.extra_compile_args + extension.extra_link_args)
def run(self): import os for extension in self.extensions: source = extension.sources[0] out = extension.name + '.so' if not path.isfile(out) or path.getmtime(source) > path.getmtime(out): print 'building pythran extension', extension.name content = file(source).read() module = pythran.cxx_generator( extension.name, content, pythran.spec_parser(content), [] ) pythran.compile( os.environ.get('CXX','c++'), module, out, cxxflags = ['-O2', '-g'] + extension.extra_compile_args + extension.extra_link_args )
def __call__(self): module_path=os.path.join(os.path.dirname(__file__),"cases",self.module_name+".py") specs = pythran.spec_parser(module_path) print self.module_name module= pythran.cxx_generator(self.module_name, file(module_path).read(), specs) pythran.compile(os.environ.get("CXX","c++"), module, check=False)