def compile_pythranfile(file_path, output_file=None, module_name=None, cpponly=False, **kwargs): """ Pythran file -> c++ file -> native module. Returns the generated .so (or .cpp if `cpponly` is set to true). """ if not output_file: # derive module name from input file name _, basename = os.path.split(file_path) module_name = module_name or os.path.splitext(basename)[0] else: # derive module name from destination output_file name _, basename = os.path.split(output_file) module_name = module_name or os.path.splitext(basename)[0] # Add compiled module path to search for imported modules sys.path.append(os.path.dirname(file_path)) # Look for an extra spec file spec_file = os.path.splitext(file_path)[0] + '.pythran' if os.path.isfile(spec_file): specs = load_specfile(open(spec_file).read()) kwargs.setdefault('specs', {}).update(specs) output_file = compile_pythrancode(module_name, open(file_path).read(), output_file=output_file, cpponly=cpponly, **kwargs) return output_file
def compile_pythranfile(file_path, output_file=None, module_name=None, cpponly=False, pyonly=False, **kwargs): """ Pythran file -> c++ file -> native module. Returns the generated .so (or .cpp if `cpponly` is set to true). Usage without an existing spec file >>> with open('pythran_test.py', 'w') as fd: ... _ = fd.write('def foo(i): return i ** 2') >>> cpp_path = compile_pythranfile('pythran_test.py', cpponly=True) Usage with an existing spec file: >>> with open('pythran_test.pythran', 'w') as fd: ... _ = fd.write('export foo(int)') >>> so_path = compile_pythranfile('pythran_test.py') Specify the output file: >>> import sysconfig >>> ext = sysconfig.get_config_vars()["SO"] >>> so_path = compile_pythranfile('pythran_test.py', output_file='foo'+ext) """ if not output_file: # derive module name from input file name _, basename = os.path.split(file_path) module_name = module_name or os.path.splitext(basename)[0] else: # derive module name from destination output_file name _, basename = os.path.split(output_file.replace('%{ext}', '')) module_name = module_name or basename.split(".", 1)[0] module_dir = os.path.dirname(file_path) # Look for an extra spec file spec_file = os.path.splitext(file_path)[0] + '.pythran' if os.path.isfile(spec_file): with open(spec_file) as fd: specs = load_specfile(fd.read()) kwargs.setdefault('specs', specs) output_file = compile_pythrancode(module_name, open(file_path).read(), output_file=output_file, cpponly=cpponly, pyonly=pyonly, module_dir=module_dir, **kwargs) return output_file
def compile_pythranfile(file_path, output_file=None, module_name=None, cpponly=False, pyonly=False, **kwargs): """ Pythran file -> c++ file -> native module. Returns the generated .so (or .cpp if `cpponly` is set to true). Usage without an existing spec file >>> with open('pythran_test.py', 'w') as fd: ... _ = fd.write('def foo(i): return i ** 2') >>> cpp_path = compile_pythranfile('pythran_test.py', cpponly=True) Usage with an existing spec file: >>> with open('pythran_test.pythran', 'w') as fd: ... _ = fd.write('export foo(int)') >>> so_path = compile_pythranfile('pythran_test.py') Specify the output file: >>> import sysconfig >>> ext = sysconfig.get_config_vars()["SO"] >>> so_path = compile_pythranfile('pythran_test.py', output_file='foo'+ext) """ if not output_file: # derive module name from input file name _, basename = os.path.split(file_path) module_name = module_name or os.path.splitext(basename)[0] else: # derive module name from destination output_file name _, basename = os.path.split(output_file) module_name = module_name or basename.split(".", 1)[0] module_dir = os.path.dirname(file_path) # Look for an extra spec file spec_file = os.path.splitext(file_path)[0] + '.pythran' if os.path.isfile(spec_file): specs = load_specfile(open(spec_file).read()) kwargs.setdefault('specs', specs) output_file = compile_pythrancode(module_name, open(file_path).read(), output_file=output_file, cpponly=cpponly, pyonly=pyonly, module_dir=module_dir, **kwargs) return output_file
def compile_pythranfile(file_path, output_file=None, module_name=None, cpponly=False, **kwargs): """ Pythran file -> c++ file -> native module. Returns the generated .so (or .cpp if `cpponly` is set to true). Usage without an existing spec file >>> with open('pythran_test.py', 'w') as fd: ... fd.write('def foo(i): return i ** 2') >>> cpp_path = compile_pythranfile('pythran_test.py', cpponly=True) Usage with an existing spec file: >>> with open('pythran_test.pythran', 'w') as fd: ... fd.write('export foo(int)') >>> so_path = compile_pythranfile('pythran_test.py') """ if not output_file: # derive module name from input file name _, basename = os.path.split(file_path) module_name = module_name or os.path.splitext(basename)[0] else: # derive module name from destination output_file name _, basename = os.path.split(output_file) module_name = module_name or os.path.splitext(basename)[0] # Add compiled module path to search for imported modules sys.path.append(os.path.dirname(file_path)) # Look for an extra spec file spec_file = os.path.splitext(file_path)[0] + '.pythran' if os.path.isfile(spec_file): specs = load_specfile(open(spec_file).read()) kwargs.setdefault('specs', specs) output_file = compile_pythrancode(module_name, open(file_path).read(), output_file=output_file, cpponly=cpponly, **kwargs) return output_file