def setUp(self): super(TestTypeInjection, self).setUp() compilation_options = Main.CompilationOptions(Main.default_options) ctx = compilation_options.create_context() transform = InterpretCompilerDirectives(ctx, ctx.compiler_directives) transform.module_scope = Symtab.ModuleScope('__main__', None, ctx) self.declarations_finder = DeclarationsFinder() self.pipeline = [NormalizeTree(None), transform, self.declarations_finder]
def setUp(self): super(TestInterpretCompilerDirectives, self).setUp() compilation_options = Main.CompilationOptions(Main.default_options) ctx = compilation_options.create_context() self.pipeline = [ InterpretCompilerDirectives(ctx, ctx.compiler_directives), ] self.debug_exception_on_error = DebugFlags.debug_exception_on_error
def setUp(self): super(TestInterpretCompilerDirectives, self).setUp() compilation_options = Main.CompilationOptions(Main.default_options) ctx = compilation_options.create_context() transform = InterpretCompilerDirectives(ctx, ctx.compiler_directives) transform.module_scope = Symtab.ModuleScope('__main__', None, ctx) self.pipeline = [transform] self.debug_exception_on_error = DebugFlags.debug_exception_on_error
def PyrexCompile ( Errors, Main, source, pxddirectories = None ): """Convert a Pyrex file to C.""" options = Main.default_options # . Change for Pyrex version 0.9.6: options is now a dictionary not a CompilationOptions object so convert to a CompilationOptions object. if isinstance ( options, dict ): options = Main.CompilationOptions ( options ) if pxddirectories is not None: options.include_path.extend ( pxddirectories ) context = Main.Context ( options.include_path ) try: result = context.compile ( source, options ) QFAILURE = ( result.num_errors > 0 ) except Errors.PyrexError, e: print >>sys.stderr, e QFAILURE = True
def PyrexCompile ( Errors, Main, source, pxdDirectories = None ): """Convert a Pyrex file to C.""" options = Main.default_options # . Change for Pyrex version 0.9.6: options is now a dictionary not a CompilationOptions object so convert to a CompilationOptions object. if isinstance ( options, dict ): options = Main.CompilationOptions ( options ) if pxdDirectories is not None: options.include_path.extend ( pxdDirectories ) context = Main.Context ( options.include_path, {} ) try: result = Main.compile ( source, options ) failed = ( result.num_errors > 0 ) except Errors.PyrexError as e: print ( e ) failed = True if failed: raise ValueError ( "There was a Pyrex compiler error." )
def compile_file(pyfile): in_file_name = pyfile source = open(in_file_name).read() out_file_name = in_file_name.replace('.py', '.out') temp_py_file = tempfile.NamedTemporaryFile(suffix='.py', delete=False) temp_py_file.write(source.encode()) temp_py_file.flush() Main.Options.embed = 'main' res = Main.compile(temp_py_file.name, Main.CompilationOptions(), '') gcc_cmd = 'gcc -fPIC -O2 %s -I/usr/include/python3.6 -L/usr/lib/python3.6 -lpython3.6m -o %s' % (res.c_file, out_file_name) print(gcc_cmd) assert 0 == subprocess.check_call(gcc_cmd.split(' '))
def generate_c_from_cython(extension, build_dir): if not sys.platform == 'darwin': print('No %s will be built for this platform.' % extension.name) return from distutils.dep_util import newer_group name = extension.name.split('.')[-1] source = extension.depends[0] target = os.path.join(build_dir, name + '.c') if newer_group(extension.depends, target): from Cython.Compiler import Main options = Main.CompilationOptions(defaults=Main.default_options, output_file=target) cython_result = Main.compile(source, options=options) if cython_result.num_errors != 0: raise RuntimeError("%d errors in Cython compile" % cython_result.num_errors) return target
def main(): """ This will do ugly things to compile a python file. """ try: source = open(argv[1]).read() outfile = argv[1].replace(".py", ".out") except IndexError: print("usage: ./compile.py <python file>") exit(EX_USAGE) temp_py_file = NamedTemporaryFile(suffix=".py", delete=False) temp_py_file.write(source.encode()) temp_py_file.flush() Main.Options.embed = "main" res = Main.compile_single(temp_py_file.name, Main.CompilationOptions(), "") gcc_cmd = "gcc %s %s %s %s %s -o %s" % \ (CFLAGS, res.c_file, LFLAGS, IFLAGS, LIBRARIES, outfile) print(gcc_cmd) check_call(gcc_cmd.split(" "))
from distutils.core import setup, Extension have_cython = True try: import Cython.Compiler.Main as cython_compiler except ImportError: have_cython = False cchardet_dir = 'src/cchardet/' uchardet_dir = 'src/ext/uchardet/src' if have_cython: pyx_sources = glob.glob(cchardet_dir + '*.pyx') sys.stderr.write('cythonize: %r\n' % (pyx_sources, )) cython_compiler.compile( pyx_sources, options=cython_compiler.CompilationOptions(cplus=True)) cchardet_sources = glob.glob(cchardet_dir + '*.cpp') sources = cchardet_sources uchardet_sources = [ os.path.join(uchardet_dir, 'CharDistribution.cpp'), os.path.join(uchardet_dir, 'JpCntx.cpp'), os.path.join(uchardet_dir, 'LangModels/LangArabicModel.cpp'), os.path.join(uchardet_dir, 'LangModels/LangBulgarianModel.cpp'), os.path.join(uchardet_dir, 'LangModels/LangCroatianModel.cpp'), os.path.join(uchardet_dir, 'LangModels/LangCzechModel.cpp'), os.path.join(uchardet_dir, 'LangModels/LangEsperantoModel.cpp'), os.path.join(uchardet_dir, 'LangModels/LangEstonianModel.cpp'), os.path.join(uchardet_dir, 'LangModels/LangFinnishModel.cpp'), os.path.join(uchardet_dir, 'LangModels/LangFrenchModel.cpp'),
from distutils.core import setup, Extension have_cython = True try: import Cython.Compiler.Main as cython_compiler except ImportError: have_cython = False cchardet_dir = 'src/cchardet/' uchardet_dir = 'src/ext/uchardet/src' if have_cython: pyx_sources = glob.glob(cchardet_dir + '*.pyx') sys.stderr.write('cythonize: %r\n' % (pyx_sources,)) cython_compiler.compile( pyx_sources, options=cython_compiler.CompilationOptions(cplus=True, compiler_directives={"language_level": 3})) cchardet_sources = glob.glob(cchardet_dir + '*.cpp') sources = cchardet_sources uchardet_sources = [ os.path.join(uchardet_dir, 'CharDistribution.cpp'), os.path.join(uchardet_dir, 'JpCntx.cpp'), os.path.join(uchardet_dir, 'LangModels/LangArabicModel.cpp'), os.path.join(uchardet_dir, 'LangModels/LangBulgarianModel.cpp'), os.path.join(uchardet_dir, 'LangModels/LangCroatianModel.cpp'), os.path.join(uchardet_dir, 'LangModels/LangCzechModel.cpp'), os.path.join(uchardet_dir, 'LangModels/LangEsperantoModel.cpp'), os.path.join(uchardet_dir, 'LangModels/LangEstonianModel.cpp'), os.path.join(uchardet_dir, 'LangModels/LangFinnishModel.cpp'), os.path.join(uchardet_dir, 'LangModels/LangFrenchModel.cpp'),
def cython_extension(srcfile): options = Main.CompilationOptions(include_path=[ os.path.join(os.path.abspath(os.path.dirname(__file__)), 'include') ]) Main.compile(srcfile, options=options)
import subprocess import sys import tempfile from Cython.Compiler import Main, CmdLine, Options in_file_name = sys.argv[1] source = open(in_file_name).read() out_file_name = in_file_name.replace('.py', '.out') temp_py_file = tempfile.NamedTemporaryFile(suffix='.py', delete=False) temp_py_file.write(source.encode()) temp_py_file.flush() Main.Options.embed = 'main' res = Main.compile_single(temp_py_file.name, Main.CompilationOptions(), '') gcc_cmd = 'gcc -fPIC -O2 %s -I/usr/include/python2.7 -L/usr/lib/python2.7 -lpython2.7 -o %s' % (res.c_file, out_file_name) print(gcc_cmd) assert 0 == subprocess.check_call(gcc_cmd.split(' '))
import Cython.Compiler.Main as main import os, glob, sys nargs = len(sys.argv) # If getenv does not work, set the pCore directory manually, for example: # pdynamo_pcore = "/home/mikolaj/local/opt/pDynamo-1.8.0/pCore-1.8.0" if nargs < 2: pdynamo_pcore = os.getenv("PDYNAMO_PCORE") else: pdynamo_pcore = sys.argv[1] current_directory = os.getcwd() pxd_directories = [ current_directory, os.path.join(pdynamo_pcore, "extensions/pyrex") ] # Decide between taking all files in the current directory or the files from the command line if nargs > 2: sources = [os.path.abspath(filename) for filename in sys.argv[2:]] else: sources = glob.glob(os.path.join(current_directory, "*.pyx")) # Now compile for source in sources: options = main.CompilationOptions(main.default_options) options.include_path.extend(pxd_directories) main.compile(source, options)