我们从Python开源项目中,提取了以下41个代码示例,用于说明如何使用Cython.Build.cythonize()。
def cython(module, libraries): if "--cythonize" in sys.argv: sys.argv.remove("--cythonize") from Cython.Build import cythonize return cythonize( [ Extension( f"{__module__}.{module}", [f"{__module__}/{module}.pyx"], libraries=libraries ) ] ) else: return [ Extension( f"{__module__}.{module}", [f"{__module__}/{module}.c"], libraries=libraries ) ]
def capnpify(files, pyx='auto', convert_case=True, version_check=True): cwd = py.path.local('.') if isinstance(files, str): files = glob.glob(files) if files == []: raise ValueError("'%s' did not match any files" % files) compiler = DistutilsCompiler(sys.path) outfiles = [compiler.compile(f, convert_case, pyx, version_check) for f in files] outfiles = [outf.relto(cwd) for outf in outfiles] # if compiler.getpyx(pyx): exts = [] for f in outfiles: ext = Extension('*', [str(f)], include_dirs=compiler.include_dirs) exts.append(ext) exts = cythonize(exts) return exts else: return []
def run(self): extensions = self.distribution.ext_modules if self._has_cython(): from Cython.Build import cythonize new_extensions = cythonize(extensions) for i, ext in enumerate(new_extensions): for source in extensions[i].sources: if source.endswith('.pyx'): # include cython file in the MANIFEST ext.depends.append(source) extensions[i] = ext return for ext in extensions: for isource, source in enumerate(ext.sources): if source.endswith('.pyx'): suf = 'cpp' if ext.language == 'c++' else 'c' new_source = source[:-3] + suf ext.sources[isource] = new_source if not os.path.exists(new_source): print("Aborting: cythonized file '{}' is missing.". format(new_source)) sys.exit()
def run(self): # Make sure the compiled Cython files in the distribution are # up-to-date try: import pypandoc except ImportError: raise RuntimeError( 'Trying to create a source distribution without pypandoc. ' 'The readme will not render correctly on pypi without ' 'pypandoc so we are exiting.' ) # Make sure the compiled Cython files in the distribution are up-to-date from Cython.Build import cythonize cythonize(cython_extensions) _sdist.run(self)
def configuration(parent_package='', top_path=None): if os.path.exists('MANIFEST'): os.remove('MANIFEST') from numpy.distutils.misc_util import Configuration config = Configuration(None, parent_package, top_path) config.set_options(ignore_setup_xxx_py=True, assume_default_configuration=True, delegate_options_to_subpackages=True, quiet=True) config.add_subpackage('modl') config.ext_modules = cythonize(config.ext_modules, nthreads=4) return config
def get_cmdclass(): versioneer_cmds = versioneer.get_cmdclass() sdist_class = versioneer_cmds['sdist'] class sdist(sdist_class): """ensure cython files are compiled to c, when distributing""" def run(self): # only run if .git is present if not os.path.exists('.git'): print("Not on git, can not create source distribution") return try: from Cython.Build import cythonize print("cythonizing sources") cythonize(extensions()) except ImportError: warnings.warn('sdist cythonize failed') return sdist_class.run(self) versioneer_cmds['sdist'] = sdist return versioneer_cmds
def _build_impl(self): dist = Distribution({ "script_name": None, "script_args": ["build_ext"] }) dist.ext_modules = cythonize([self.extension]) dist.include_dirs = [] dist.cmdclass = {'build_ext': custom_build_ext} build = dist.get_command_obj('build') # following the convention of cython's pyxbuild and naming # base directory "_pyxbld" build.build_base = join(self.CYMJ_DIR_PATH, 'generated', '_pyxbld_%s' % self.build_base()) dist.parse_command_line() obj_build_ext = dist.get_command_obj("build_ext") dist.run_commands() built_so_file_path, = obj_build_ext.get_outputs() return built_so_file_path
def setup_package(): metadata = dict(name=spfeas_name, maintainer=maintainer, maintainer_email=maintainer_email, description=description, license=license_file, version=__version__, long_description=long_description, author=author_file, packages=get_packages(), package_data=get_package_data(), ext_modules=cythonize(get_pyx_list()), include_dirs=[np.get_include()], cmdclass=dict(build_ext=build_ext), zip_safe=False, download_url=git_url, install_requires=required_packages, entry_points=get_console_dict()) setup(**metadata)
def maybe_cythonize_extensions(top_path, config): """Tweaks for building extensions between release and development mode.""" is_release = os.path.exists(os.path.join(top_path, 'PKG-INFO')) if is_release: build_from_c_and_cpp_files(config.ext_modules) else: message = ('Please install cython with a version >= {0} in order ' 'to build a pyramid development version.').format( CYTHON_MIN_VERSION) try: import Cython if LooseVersion(Cython.__version__) < CYTHON_MIN_VERSION: message += ' Your version of Cython was {0}.'.format( Cython.__version__) raise ValueError(message) from Cython.Build import cythonize except ImportError as exc: exc.args += (message,) raise config.ext_modules = cythonize(config.ext_modules)
def initialize(self): if not getattr(self, 'initialized', False): self.initialized = True from Cython.Build import cythonize include_dirs = os.environ.get('CYTHON_INCLUDE_DIRS','.').split(':') extension_modules = [ Extension('sharedbuffers.mapped_struct', ['sharedbuffers/mapped_struct.py'], depends = ['sharedbuffers/mapped_struct.pxd']), ] ext_modules = cythonize(extension_modules, include_path = include_dirs) self.extend(ext_modules)
def _setup_extensions(self): # We defer extension setup until this command to leveraage 'setup_requires' pulling in Cython before we # attempt to import anything self.extensions = [] if try_murmur3: self.extensions.append(murmur3_ext) if try_libev: self.extensions.append(libev_ext) if try_cython: try: from Cython.Build import cythonize cython_candidates = ['cluster', 'concurrent', 'connection', 'cqltypes', 'metadata', 'pool', 'protocol', 'query', 'util'] compile_args = [] if is_windows else ['-Wno-unused-function'] self.extensions.extend(cythonize( [Extension('cassandra.%s' % m, ['cassandra/%s.py' % m], extra_compile_args=compile_args) for m in cython_candidates], nthreads=build_concurrency, exclude_failures=True)) self.extensions.extend(cythonize(NoPatchExtension("*", ["cassandra/*.pyx"], extra_compile_args=compile_args), nthreads=build_concurrency)) except Exception: sys.stderr.write("Failed to cythonize one or more modules. These will not be compiled as extensions (optional).\n")
def cythonize(*args, **kwargs): ''' dirty hack, only import cythonize at the time you use it. if you don't write Cython extension, you won't fail even if you don't install Cython. ''' global cythonize from Cython.Build import cythonize return cythonize(*args, **kwargs)
def run(self): clean.run(self) # Remove c files if we are not within a sdist package cwd = os.path.abspath(os.path.dirname(__file__)) remove_c_files = not os.path.exists(os.path.join(cwd, 'PKG-INFO')) if remove_c_files: cython_hash_file = os.path.join(cwd, 'cythonize.dat') if os.path.exists(cython_hash_file): os.unlink(cython_hash_file) print('Will remove generated .c & .so files') if os.path.exists('build'): shutil.rmtree('build') for dirpath, dirnames, filenames in os.walk('skutil'): for filename in filenames: if any(filename.endswith(suffix) for suffix in (".so", ".pyd", ".dll", ".pyc")): print('Removing file: %s' % filename) os.unlink(os.path.join(dirpath, filename)) continue extension = os.path.splitext(filename)[1] if remove_c_files and extension in ['.c', '.cpp']: pyx_file = str.replace(filename, extension, '.pyx') if os.path.exists(os.path.join(dirpath, pyx_file)): os.unlink(os.path.join(dirpath, filename)) for dirname in dirnames: if dirname == '__pycache__' or dirname.endswith('.so.dSYM'): print('Removing directory: %s' % dirname) shutil.rmtree(os.path.join(dirpath, dirname))
def generate_cython(): cwd = os.path.abspath(os.path.dirname(__file__)) print("Generating Cython modules") p = subprocess.call([sys.executable, os.path.join(cwd, 'build_tools', 'cythonize.py'), 'skutil'], cwd=cwd) if p != 0: raise RuntimeError("Running cythonize failed!")
def run(self): from Cython.Build import cythonize compiler_directives = {} if linetrace: compiler_directives['linetrace'] = True self.extensions = cythonize(self.extensions, compiler_directives=compiler_directives) _build_ext.run(self) run_setup(os.path.join(os.getcwd(), "setup.py"), ['build_py'] + extra_args)
def my_cythonize(extensions): try: import Cython if Cython.__version__ < '0.25': print ('WARNING: required cython>0.25, found %s. The .c files will ' 'NOT be regenerated' % Cython.__version__) raise ImportError from Cython.Build import cythonize except ImportError: return cythonize_dummy(extensions) else: return cythonize(extensions, gdb_debug=DEBUG)
def cythonize(extensions): return extensions
def define_extensions(use_cython=False): if sys.platform.startswith("win"): # compile args from # https://msdn.microsoft.com/en-us/library/fwkeyyhe.aspx compile_args = ['/O2', '/openmp'] link_args = [] else: compile_args = ['-Wno-unused-function', '-Wno-maybe-uninitialized', '-O3', '-ffast-math'] link_args = [] if use_openmp: compile_args.append("-fopenmp") link_args.append("-fopenmp") src_ext = '.pyx' if use_cython else '.cpp' modules = [Extension("implicit." + name, [os.path.join("implicit", name + src_ext)], language='c++', extra_compile_args=compile_args, extra_link_args=link_args) for name in ['_als', '_nearest_neighbours']] if CUDA: modules.append(Extension("implicit.cuda._cuda", [os.path.join("implicit", "cuda", "_cuda" + src_ext), os.path.join("implicit", "cuda", "als.cu"), os.path.join("implicit", "cuda", "matrix.cu")], language="c++", extra_compile_args=compile_args, extra_link_args=link_args, library_dirs=[CUDA['lib64']], libraries=['cudart', 'cublas'], runtime_library_dirs=[CUDA['lib64']], include_dirs=[CUDA['include'], '.'])) if use_cython: return cythonize(modules) else: return modules # set_gcc copied from glove-python project # https://github.com/maciejkula/glove-python
def extensions(): from numpy import get_include from Cython.Build import cythonize ext_core = Extension( "pydpc.core", sources=["ext/core.pyx", "ext/_core.c"], include_dirs=[get_include()], extra_compile_args=["-O3", "-std=c99"]) exts = [ext_core] return cythonize(exts)
def compile_(self): """Translate cython code to C code and compile it.""" from Cython import Build argv = copy.deepcopy(sys.argv) sys.argv = [sys.argv[0], 'build_ext', '--build-lib='+self.buildpath] exc_modules = [ distutils.extension.Extension( 'hydpy.cythons.autogen.'+self.cyname, [self.cyfilepath], extra_compile_args=['-O2'])] distutils.core.setup(ext_modules=Build.cythonize(exc_modules), include_dirs=[numpy.get_include()]) sys.argv = argv
def finalize_options(self): try: import cython import numpy except ImportError: raise ImportError( """Could not import cython or numpy. Building yt from source requires cython and numpy to be installed. Please install these packages using the appropriate package manager for your python environment.""") if LooseVersion(cython.__version__) < LooseVersion('0.24'): raise RuntimeError( """Building yt from source requires Cython 0.24 or newer but Cython %s is installed. Please update Cython using the appropriate package manager for your python environment.""" % cython.__version__) if LooseVersion(numpy.__version__) < LooseVersion('1.10.4'): raise RuntimeError( """Building yt from source requires NumPy 1.10.4 or newer but NumPy %s is installed. Please update NumPy using the appropriate package manager for your python environment.""" % numpy.__version__) from Cython.Build import cythonize self.distribution.ext_modules[:] = cythonize( self.distribution.ext_modules) _build_ext.finalize_options(self) # Prevent numpy from thinking it is still in its setup process # see http://stackoverflow.com/a/21621493/1382869 if isinstance(__builtins__, dict): # sometimes this is a dict so we need to check for that # https://docs.python.org/3/library/builtins.html __builtins__["__NUMPY_SETUP__"] = False else: __builtins__.__NUMPY_SETUP__ = False self.include_dirs.append(numpy.get_include())
def run(self): # If we encounter a PKG-INFO file, then this is likely a .tar.gz/.zip # file retrieved from PyPI that already includes the pre-cythonized # extension modules, and then we do not need to run cythonize(). if os.path.exists('PKG-INFO'): no_cythonize(extensions) else: # Otherwise, this is a 'developer copy' of the code, and then the # only sensible thing is to require Cython to be installed. check_cython_version() from Cython.Build import cythonize self.extensions = cythonize(self.extensions) _build_ext.run(self)
def run(self): # Make sure the compiled Cython files in the distribution are up-to-date from Cython.Build import cythonize check_cython_version() cythonize(extensions) versioneer_sdist.run(self)
def package_cython_modules(build_dir, list_of_files, cython_compiler): """ Compiles .pyx/.py files to .pyd/.so files inplace """ os.chdir(build_dir) # Change to build directory and create a new file setup_file = 'cythonize_modules.py' # Write temporary setup file with open(setup_file, 'w') as outfile: outfile.write(""" from setuptools import setup from setuptools import Extension from Cython.Build import cythonize setup(name = 'acore', ext_modules = cythonize('"""+list_of_files[0]+"""'),) setup(name = 'aplot', ext_modules = cythonize('"""+list_of_files[1]+"""'),) setup(name = 'alog', ext_modules = cythonize('"""+list_of_files[2]+"""'),) """) t0 = time.clock() # Start time # Cythonize modules and compile them to .pyd/.so libraries subprocess.call(r'python {setup} build_ext --inplace --compiler={cython_compiler}'.format(setup=setup_file, cython_compiler=cython_compiler)) t1 = time.clock() # End time print('\n>> Modules were successfully compiled by Cython!') print('Compilation time: '+str(round((t1 - t0) / 60.0, 1))+' min')
def cythonize(e, *args, **kwargs): return e
def run(self): # Make sure the compiled Cython files in the distribution are # up-to-date try: import pypandoc except ImportError: raise RuntimeError( 'Trying to create a source distribution without pypandoc. ' 'The readme will not render correctly on pypi without ' 'pypandoc so we are exiting.' ) from Cython.Build import cythonize cythonize(ext_modules) _sdist.run(self)
def finalize_options(self): from Cython.Build import cythonize self.distribution.ext_modules[:] = cythonize( self.distribution.ext_modules) _build_ext.finalize_options(self) # Prevent numpy from thinking it is still in its setup process # see http://stackoverflow.com/a/21621493/1382869 if isinstance(__builtins__, dict): # sometimes this is a dict so we need to check for that # https://docs.python.org/3/library/builtins.html __builtins__["__NUMPY_SETUP__"] = False else: __builtins__.__NUMPY_SETUP__ = False import numpy self.include_dirs.append(numpy.get_include())
def run(self): # Make sure the compiled Cython files in the distribution are up-to-date from Cython.Build import cythonize cythonize(cython_extensions) _sdist.run(self)
def extensions(): try: from Cython.Build import cythonize except ImportError: def cythonize(*args, **kwargs): print("Hint: Wrapping import of cythonize in extensions()") from Cython.Build import cythonize return cythonize(*args, **kwargs) try: import numpy lstIncludes = [numpy.get_include()] except ImportError: lstIncludes = [] extensionArguments = { 'include_dirs': lstIncludes + ['fastmat/core', 'fastmat/inspect', 'util'], 'extra_compile_args': compilerArguments, 'extra_link_args': linkerArguments, 'define_macros': defineMacros } return cythonize( [Extension("*", ["fastmat/*.pyx"], **extensionArguments), Extension("*", ["fastmat/algs/*.pyx"], **extensionArguments), Extension("*", ["fastmat/core/*.pyx"], **extensionArguments)], compiler_directives=cythonDirectives) # determine requirements for install and setup
def cythonize(*args, **kwargs): return []
def compileCythonFiles(): from distutils.core import setup from Cython.Build import cythonize sys.argv = [sys.argv[0], "build_ext", "--inplace"] extensions = cythonize(getPathsToCythonFiles(), compiler_directives={'cdivision': True, 'cdivision_warnings': False}) setup(name = 'umog_addon', ext_modules = extensions, include_dirs=[numpy.get_include()]) print("Compilation Successful.") sys.argv = [sys.argv[0], "clean"] setup(name = 'umog_addon', ext_modules = extensions)
def cython_or_c(ext): if USE_CYTHON: return cythonize(ext) else: for e in ext: for i, j in enumerate(e.sources): e.sources[i] = j.replace('.pyx', '.c') # Enable creating Sphinx documentation ext.cython_directives = { 'embedsignature': True, 'binding': True, 'linetrace': True } return ext
def cythonize(exts): for ext in exts: ext.sources = [s[:-4]+'.c' for s in ext.sources] # replace .pyx with .c
def _setup_extensions(self): # We defer extension setup until this command to leveraage 'setup_requires' pulling in Cython before we # attempt to import anything self.extensions = [] if try_murmur3: self.extensions.append(murmur3_ext) if try_libev: self.extensions.append(libev_ext) if try_cython: try: from Cython.Build import cythonize cython_candidates = ['cluster', 'concurrent', 'connection', 'cqltypes', 'metadata', 'hosts', 'protocol', 'query', 'util'] compile_args = [] if is_windows else ['-Wno-unused-function'] self.extensions.extend(cythonize( [Extension('dse.%s' % m, ['dse/%s.py' % m], extra_compile_args=compile_args) for m in cython_candidates], nthreads=build_concurrency, exclude_failures=True)) self.extensions.extend(cythonize(NoPatchExtension("*", ["dse/*.pyx"], extra_compile_args=compile_args), nthreads=build_concurrency)) except Exception: sys.stderr.write("Failed to cythonize one or more modules. These will not be compiled as extensions (optional).\n")
def extensions(): # List of Cython implementation files (without file extension)... modules = ['sxn', 'xdm'] # Get home directories for Java and Saxon/C... saxonhome = os.environ.get('SAXONC_HOME') javahome = os.environ.get('JAVA_HOME') if not all((saxonhome, javahome)): raise ValueError('SAXONC_HOME and/or JAVA_HOME not set') # Compiler settings... settings = { 'libraries': ['saxonhec'], 'include_dirs': [osp.join(saxonhome, 'Saxon.C.API'), osp.join(javahome, 'include')], 'library_dirs': [saxonhome, '/usr/lib'] } if sys.platform.startswith('linux'): settings['include_dirs'].append(osp.join(javahome, 'include', 'linux')) else: raise NotImplemented(sys.platform, 'not supported yet') # See: http://stackoverflow.com/q/19123623 if os.name != 'nt': settings['runtime_library_dirs'] = settings['library_dirs'] # Additional source files required... addl_src = ['SaxonCGlue.c', 'SaxonCXPath.c', 'XdmValue.cpp', 'XdmItem.cpp', 'XdmNode.cpp', 'XdmAtomicValue.cpp', 'SaxonProcessor.cpp', 'XsltProcessor.cpp', 'XQueryProcessor.cpp', 'XPathProcessor.cpp', 'SchemaValidator.cpp'] for n in range(len(addl_src)): addl_src[n] = osp.join(saxonhome, 'Saxon.C.API', addl_src[n]) if not osp.isfile(addl_src[n]): raise IOError('"%s" file not found' % addl_src[n]) exts = list() for m in modules: pyx_src = localpath('pysaxon', m + '.pyx') exts.append(Extension('pysaxon.' + m, [pyx_src] + addl_src, language='c++', **settings)) return cythonize(exts)
def movedll(self): """Try to find the resulting dll file and to move it into the `cythons` package. Things to be aware of: * The file extension either `pyd` (Window) or `so` (Linux). * The folder containing the dll file is system dependend, but is always a subfolder of the `cythons` package. * Under Linux, the filename might contain system information, e.g. ...cpython-36m-x86_64-linux-gnu.so. """ dirinfos = os.walk(self.buildpath) next(dirinfos) system_dependend_filename = None for dirinfo in dirinfos: for filename in dirinfo[2]: if (filename.startswith(self.cyname) and filename.endswith(dllextension)): system_dependend_filename = filename break if system_dependend_filename: try: shutil.move(os.path.join(dirinfo[0], system_dependend_filename), os.path.join(self.cydirpath, self.cyname+dllextension)) break except BaseException: prefix = ('After trying to cythonize module %s, when ' 'trying to move the final cython module %s ' 'from directory %s to directory %s' % (self.pyname, system_dependend_filename, self.buildpath, self.cydirpath)) suffix = ('A likely error cause is that the cython module ' '%s does already exist in this directory and is ' 'currently blocked by another Python process. ' 'Maybe it helps to close all Python processes ' 'and restart the cyhonization afterwards.' % self.cyname+dllextension) objecttools.augmentexcmessage(prefix, suffix) else: raise IOError('After trying to cythonize module %s, the resulting ' 'file %s could neither be found in directory %s nor ' 'its subdirectories. The distul report should tell ' 'whether the file has been stored somewhere else,' 'is named somehow else, or could not be build at ' 'all.' % self.buildpath)
def cython_ext(extension, **kw): assert len(extension.sources) == 1 base, ext = os.path.splitext(extension.sources[0]) # setuptools sometimes "nicely" turns .pyx into .c for us assert ext in {'.pyx', '.c'} pyx_path = base + '.pyx' c_path = base + '.c' try: from Cython.Build import cythonize except ImportError: have_cythonize = False else: have_cythonize = True have_pyx = os.path.exists(pyx_path) have_c = os.path.exists(c_path) if have_cythonize and have_pyx: return cythonize([extension])[0] msg = "{} extension needs to be compiled, but {} not available".format( extension.name, "sources" if not have_pyx and not have_c else "pyx source" if not have_pyx else "cython") if not have_c: raise ImportError(msg) if have_pyx and have_c: pyx_time = os.path.getmtime(pyx_path) c_time = os.path.getmtime(c_path) if pyx_time > c_time: import datetime new_msg = "{pyx_name} has mtime {pyx_t}, {c_name} has {c_t}" raise ImportError(msg + ':\n' + new_msg.format( pyx_name=os.path.basename(pyx_path), c_name=os.path.basename(c_path), pyx_t=datetime.datetime.fromtimestamp(pyx_time), c_t=datetime.datetime.fromtimestamp(c_time), )) extension.sources[0] = c_path return extension
def pre_build_ext(cmd_obj, version=None): if version == "latest": version = sorted(ceph_version_map.keys())[-1] elif version is None: comp = ccompiler.new_compiler(force=True, verbose=True) for potential_version, method in ceph_version_map.items(): msg = "* checking for librados >= %s (with function %s)" % ( potential_version, method) print(msg) found = comp.has_function(method, libraries=['rados']) if found: version = potential_version print("%s done: FOUND" % msg) else: print("%s done: NOT FOUND" % msg) break if not version: raise Exception("gcc, python-dev, librados2 or " "librados-dev >= 11.2.0 are missing") print("building cradox with %s api compatibility" % version) # Generate the source file from template from jinja2 import Environment env = Environment(trim_blocks=True, lstrip_blocks=True) cradox_out = os.path.join(os.path.dirname(__file__), 'cradox.pyx') cradox_in = "%s.in" % cradox_out with open(cradox_in, 'r') as src: with open(cradox_out, 'w') as dst: template = env.from_string(src.read()) dst.write(template.render(version=version)) test_out = os.path.join(os.path.dirname(__file__), 'test_rados.py') test_in = "%s.in" % test_out with open(test_in, 'r') as src: with open(test_out, 'w') as dst: template = env.from_string(src.read()) dst.write(template.render(version=version)) # Required by old version of setuptools if cmd_obj is not None: from Cython.Build import cythonize cmd_obj.extensions = cythonize(cmd_obj.extensions) for ext in cmd_obj.extensions: ext._needs_stub = False
def finalize_options(self): # finalize_options() may be called multiple times on the # same command object, so make sure not to override previously # set options. if getattr(self, '_initialized', False): return need_cythonize = self.cython_always cfiles = {} for extension in self.distribution.ext_modules: for i, sfile in enumerate(extension.sources): if sfile.endswith('.pyx'): prefix, ext = os.path.splitext(sfile) cfile = prefix + '.c' if os.path.exists(cfile) and not self.cython_always: extension.sources[i] = cfile else: if os.path.exists(cfile): cfiles[cfile] = os.path.getmtime(cfile) else: cfiles[cfile] = 0 need_cythonize = True if need_cythonize: try: import Cython except ImportError: raise RuntimeError( 'please install Cython to compile asyncpg from source') if Cython.__version__ < '0.24': raise RuntimeError( 'asyncpg requires Cython version 0.24 or greater') from Cython.Build import cythonize directives = {} if self.cython_directives: for directive in self.cython_directives.split(','): k, _, v = directive.partition('=') if v.lower() == 'false': v = False if v.lower() == 'true': v = True directives[k] = v self.distribution.ext_modules[:] = cythonize( self.distribution.ext_modules, compiler_directives=directives, annotate=self.cython_annotate) for cfile, timestamp in cfiles.items(): if os.path.getmtime(cfile) != timestamp: # The file was recompiled, patch self._patch_cfile(cfile) super(build_ext, self).finalize_options()
def get_ext_modules(): ''' Generate C extensions 1. By default already Cython generated *.c files are used. 2. If *.c files are not present Cython generates them. 3. If the option '--cython' is specified Cython generates new *.c files. ''' # Check if *.c files are already there file_list = ['qmeq/approach/c_pauli.c', 'qmeq/approach/c_lindblad.c', 'qmeq/approach/c_redfield.c', 'qmeq/approach/c_neumann1.c', 'qmeq/approach/c_neumann2.c', 'qmeq/specfuncc.c'] c_files_exist = all([os.path.isfile(f) for f in file_list]) # Check if --cython option is specified if '--cython' in sys.argv: use_cython = True sys.argv.remove('--cython') else: use_cython = False if c_files_exist and not use_cython: cythonize = None file_ext = '.c' #print('using already Cython generated C files') else: from Cython.Build import cythonize file_ext = '.pyx' #print('using cythonize to generate C files') ext = [# Pauli Extension('qmeq.approach.c_pauli', ['qmeq/approach/c_pauli'+file_ext]), # Lindblad Extension('qmeq.approach.c_lindblad', ['qmeq/approach/c_lindblad'+file_ext]), # Redfield Extension('qmeq.approach.c_redfield', ['qmeq/approach/c_redfield'+file_ext]), # 1vN Extension('qmeq.approach.c_neumann1', ['qmeq/approach/c_neumann1'+file_ext]), # 2vN Extension('qmeq.approach.c_neumann2', ['qmeq/approach/c_neumann2'+file_ext]), # Special functions Extension('qmeq.specfuncc', ['qmeq/specfuncc'+file_ext])] cext = ext if cythonize is None else cythonize(ext) return cext