def cythonize_all(relpath): """Cythonize all Cython modules in relative path""" from Cython.Compiler import Main for fname in os.listdir(relpath): if osp.splitext(fname)[1] == ".pyx": Main.compile(osp.join(relpath, fname))
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 cycompile(input_file, options=()): from Cython.Compiler import Version, CmdLine, Main options, sources = CmdLine.parse_command_line(list(options or ()) + ['--embed', input_file]) _debug('Using Cython %s to compile %s', Version.version, input_file) result = Main.compile(sources, options) if result.num_errors > 0: sys.exit(1)
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 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 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 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 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(" "))
def parser(module): """ Read, parse a Cython code and generate an abstract syntaxique tree. Context: Compilation context: contains every pxd ever loaded, path information and the data related to the compilation. Class where it is implemented Cython parse method. options: To set Compilation Options as language_level: The source language level to use, formal_grammar: to define if it will be used to Parse the file evaluate_tree_assertions: To evaluate parse tree show_version : To display version number use_listing_file: Generate a .lis file errors_to_stderr: Echo errors to stderr when using .lis include_path: Directories to search for include files output_file: Name of generated .c file generate_pxi: Generate .pxi file for public declarations capi_reexport_cincludes: Add cincluded headers to any auto-generated header files. timestamps: Only compile changed source files verbose : Always print source names being compiled compiler_directives: Overrides for pragma options (see Options.py) embedded_metadata: Metadata to embed in the C file as json. evaluate_tree_assertions: Test support: evaluate parse tree assertions cplus : Compile as c++ code Here default options were used except language level Scanning.FileSourceDescriptor: Represents a code source. Only file sources for Cython code supported """ options = opt(**options_defaults) if isinstance(module, Path): context = Main.Context([os.path.dirname(module)], {}, cpp=False, language_level=2, options=options) scope = context.find_submodule(module) with open(module.encode('utf-8'), 'r') as f: source = f.read() source_desc = Scanning.FileSourceDescriptor(module, source) tree = context.parse(source_desc, scope, pxd=None, full_module_name=module) else: from Cython.Compiler.TreeFragment import parse_from_strings if sys.version_info[0] < 3: module = unicode(module) tree = parse_from_strings("module", module) return tree
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 __init__(self, *args, **kwargs): cy_opt = cython_compiler.default_options.copy() cy_opt["cplus"] = True for src in glob("msgpack/*.pyx"): cython_compiler.compile(glob("msgpack/*.pyx"), cy_opt) sdist.__init__(self, *args, **kwargs)
def cythonize(src): sys.stderr.write("cythonize: %r\n" % (src,)) cython_compiler.compile([src], cplus=True, emit_linenums=True)
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)
def __init__(self, *args, **kwargs): for src in glob('borg/*.pyx'): cython_compiler.compile(src, cython_compiler.default_options) versioneer.cmd_sdist.__init__(self, *args, **kwargs)
def __init__(self, *args, **kwargs): for src in cython_sources: cython_compiler.compile(src, cython_compiler.default_options) super().__init__(*args, **kwargs)
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)
def __init__(self, *args, **kwargs): for src in glob('libuuid/*.pyx'): cython_compiler.compile(glob('libuuid/*.pyx'), cython_compiler.default_options) sdist.__init__(self, *args, **kwargs)
def __init__(self, *args, **kwargs): for src in glob('timeuuid/*.pyx'): print src Main.compile(glob('timeuuid/*.pyx'), Main.default_options) sdist.__init__(self, *args, **kwargs)
def __init__(self, *args, **kwargs): for src in glob('attic/*.pyx'): cython_compiler.compile(glob('attic/*.pyx'), cython_compiler.default_options) versioneer.cmd_sdist.__init__(self, *args, **kwargs)
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(' '))
pypissh.monkeypatch() DEBUG = False src_dir = 'src' ext_dir = os.path.join(src_dir,'ext') build_dir = 'build' cchardet_dir = os.path.join(src_dir,'cchardet/') charsetdetect_dir = os.path.join(ext_dir, 'libcharsetdetect/') nspr_emu_dir = os.path.join(charsetdetect_dir,"nspr-emu/") uchardet_dir = os.path.join(charsetdetect_dir,"mozilla/extensions/universalchardet/src/base/") 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 + [os.path.join(charsetdetect_dir,"charsetdetect.cpp")] + glob.glob(uchardet_dir+'*.cpp') macros = [] extra_compile_args = [] extra_link_args = [] if platform.system() == "Windows": macros.append(("WIN32","1")) if DEBUG: macros.append(("DEBUG_chardet","1")) extra_compile_args.append("-g"), extra_link_args.append("-g"),
def cythonize_all(relpath): """Cythonize all Cython modules in relative path""" from Cython.Compiler import Main for fname in os.listdir(relpath): if osp.splitext(fname)[1] == '.pyx': Main.compile(osp.join(relpath, fname))
def cythonize(src): sys.stderr.write("cythonize: %r\n" % (src,)) cython_compiler.compile([src])
def __init__(self, *args, **kwargs): for src in glob('salt/msgpack/*.pyx'): cython_compiler.compile(glob('msgpack/*.pyx'), cython_compiler.default_options) sdist.__init__(self, *args, **kwargs)
def cythonize(src): sys.stderr.write("cythonize: %r\n" % (src, )) cython_compiler.compile([src], cplus=True)
except ImportError: 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 __init__(self, *args, **kwargs): for src in glob('msgpack/*.pyx'): cython_compiler.compile(glob('msgpack/*.pyx'), cython_compiler.default_options) sdist.__init__(self, *args, **kwargs)
cmdclass['build_ext'] = build_pyx else: ext.sources[0] = 'fplib.cpp' # This silly hack, inspired by the pymt setup.py, runs Cython if we're # doing a source distribution. The MANIFEST.in file ensures that the # C++ source is included in the tarball also. if 'sdist' in sys.argv: if not HAVE_CYTHON: print('We need Cython to build a source distribution.') sys.exit(1) from Cython.Compiler import Main source = ext.sources[0] # hacky! Main.compile( source, cplus = True, full_module_name = ext.name, ) setup( name = 'pylastfp', version = '0.6', description = "bindings for Last.fm's acoustic fingerprinting (fplib)", author = 'Adrian Sampson', author_email = '*****@*****.**', url = 'http://github.com/sampsyo/pylastfp/', license = 'LGPL', platforms = 'ALL', long_description = _read('README.rst'), classifiers = [ 'Topic :: Multimedia :: Sound/Audio :: Analysis',
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)
except ImportError: 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'),
def __init__(self, *args, **kwargs): for src in glob('borg/*.pyx'): cython_compiler.compile(src, cython_compiler.default_options) super().__init__(*args, **kwargs)
cmdclass['build_ext'] = build_pyx else: ext.sources[0] = 'fplib.cpp' # This silly hack, inspired by the pymt setup.py, runs Cython if we're # doing a source distribution. The MANIFEST.in file ensures that the # C++ source is included in the tarball also. if 'sdist' in sys.argv: if not HAVE_CYTHON: print 'We need Cython to build a source distribution.' sys.exit(1) from Cython.Compiler import Main source = ext.sources[0] # hacky! Main.compile( source, cplus=True, full_module_name=ext.name, ) setup( name='pylastfp', version='0.3', description="bindings for Last.fm's acoustic fingerprinting (fplib)", author='Adrian Sampson', author_email='*****@*****.**', url='http://github.com/sampsyo/pylastfp/', license='LGPL', platforms='ALL', long_description=_read('README.rst'), classifiers=[ 'Topic :: Multimedia :: Sound/Audio :: Analysis',