我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用distutils.extension.Extension()。
def test_get_outputs(self): pkg_dir, dist = self.create_dist() cmd = install_lib(dist) # setting up a dist environment cmd.compile = cmd.optimize = 1 cmd.install_dir = pkg_dir f = os.path.join(pkg_dir, 'foo.py') self.write_file(f, '# python file') cmd.distribution.py_modules = [pkg_dir] cmd.distribution.ext_modules = [Extension('foo', ['xxx'])] cmd.distribution.packages = [pkg_dir] cmd.distribution.script_name = 'setup.py' # get_output should return 4 elements self.assertGreaterEqual(len(cmd.get_outputs()), 2)
def test_get_inputs(self): pkg_dir, dist = self.create_dist() cmd = install_lib(dist) # setting up a dist environment cmd.compile = cmd.optimize = 1 cmd.install_dir = pkg_dir f = os.path.join(pkg_dir, 'foo.py') self.write_file(f, '# python file') cmd.distribution.py_modules = [pkg_dir] cmd.distribution.ext_modules = [Extension('foo', ['xxx'])] cmd.distribution.packages = [pkg_dir] cmd.distribution.script_name = 'setup.py' # get_input should return 2 elements self.assertEqual(len(cmd.get_inputs()), 2)
def test_get_outputs(self): project_dir, dist = self.create_dist() os.chdir(project_dir) os.mkdir('spam') cmd = install_lib(dist) # setting up a dist environment cmd.compile = cmd.optimize = 1 cmd.install_dir = self.mkdtemp() f = os.path.join(project_dir, 'spam', '__init__.py') self.write_file(f, '# python package') cmd.distribution.ext_modules = [Extension('foo', ['xxx'])] cmd.distribution.packages = ['spam'] cmd.distribution.script_name = 'setup.py' # get_outputs should return 4 elements: spam/__init__.py, .pyc and # .pyo, foo.import-tag-abiflags.so / foo.pyd outputs = cmd.get_outputs() self.assertEqual(len(outputs), 4, outputs)
def test_get_inputs(self): project_dir, dist = self.create_dist() os.chdir(project_dir) os.mkdir('spam') cmd = install_lib(dist) # setting up a dist environment cmd.compile = cmd.optimize = 1 cmd.install_dir = self.mkdtemp() f = os.path.join(project_dir, 'spam', '__init__.py') self.write_file(f, '# python package') cmd.distribution.ext_modules = [Extension('foo', ['xxx'])] cmd.distribution.packages = ['spam'] cmd.distribution.script_name = 'setup.py' # get_inputs should return 2 elements: spam/__init__.py and # foo.import-tag-abiflags.so / foo.pyd inputs = cmd.get_inputs() self.assertEqual(len(inputs), 2, inputs)
def test_optional_extension(self): # this extension will fail, but let's ignore this failure # with the optional argument. modules = [Extension('foo', ['xxx'], optional=False)] dist = Distribution({'name': 'xx', 'ext_modules': modules}) cmd = build_ext(dist) cmd.ensure_finalized() self.assertRaises((UnknownFileError, CompileError), cmd.run) # should raise an error modules = [Extension('foo', ['xxx'], optional=True)] dist = Distribution({'name': 'xx', 'ext_modules': modules}) cmd = build_ext(dist) cmd.ensure_finalized() cmd.run() # should pass
def test_get_outputs(self): pkg_dir, dist = self.create_dist() cmd = install_lib(dist) # setting up a dist environment cmd.compile = cmd.optimize = 1 cmd.install_dir = pkg_dir f = os.path.join(pkg_dir, 'foo.py') self.write_file(f, '# python file') cmd.distribution.py_modules = [pkg_dir] cmd.distribution.ext_modules = [Extension('foo', ['xxx'])] cmd.distribution.packages = [pkg_dir] cmd.distribution.script_name = 'setup.py' # get_output should return 4 elements self.assertTrue(len(cmd.get_outputs()) >= 2)
def extract_module_types(packages): py_modules = [] ext_modules = [] for package_name, package in packages.items(): if package_name in ('pytouhou.ui', 'pytouhou.ui.sdl'): package_args = sdl_args elif package_name == 'pytouhou.ui.opengl': package_args = opengl_args else: package_args = {} for module_name, extensions in package.items(): fully_qualified_name = '%s.%s' % (package_name, module_name) if '.pyx' in extensions or '.pxd' in extensions or compile_everything: if fully_qualified_name == 'pytouhou.lib.sdl': compile_args = sdl_args else: compile_args = package_args ext = 'pyx' if '.pyx' in extensions else 'py' source = '%s.%s' % (fully_qualified_name.replace('.', os.path.sep), ext) ext_modules.append(Extension(fully_qualified_name, [source], **compile_args)) else: py_modules.append(fully_qualified_name) return py_modules, ext_modules
def setup_odbc(include_dirs, lib_dirs): src_path = os.path.join(os.path.dirname(__file__), 'dbadapter/pyodbc/src') src = [os.path.abspath(os.path.join(src_path, f)) for f in os.listdir(src_path) if f.endswith('.cpp') ] if sys.platform == 'win32': libraries = ['odbc32', 'advapi32'] elif sys.platform == 'darwin': if os.environ.get('UNIXODBC_PATH', ''): include_dirs.append(os.path.join(os.environ.get('UNIXODBC_PATH'))) include_dirs.append(os.path.join(os.environ.get('UNIXODBC_PATH'), 'include')) lib_dirs.append(os.path.join(os.environ.get('UNIXODBC_PATH'), 'DriverManager', '.libs')) libraries = ['odbc'] else: libraries = ['odbc'] else: libraries = ['odbc'] return Extension('dbadapter.pyodbc', src, include_dirs=include_dirs, libraries=libraries, library_dirs=lib_dirs)
def customize_compiler_for_nvcc(self): """inject deep into distutils to customize how the dispatch to gcc/nvcc works. If you subclass UnixCCompiler, it's not trivial to get your subclass injected in, and still have the right customizations (i.e. distutils.sysconfig.customize_compiler) run on it. So instead of going the OO route, I have this. Note, it's kindof like a wierd functional subclassing going on.""" # tell the compiler it can processes .cu self.src_extensions.append('.cu') # save references to the default compiler_so and _comple methods default_compiler_so = self.compiler_so super = self._compile # now redefine the _compile method. This gets executed for each # object but distutils doesn't have the ability to change compilers # based on source extension: we add it. def _compile(obj, src, ext, cc_args, extra_postargs, pp_opts): print(extra_postargs) if os.path.splitext(src)[1] == '.cu': # use the cuda for .cu files self.set_executable('compiler_so', CUDA['nvcc']) # use only a subset of the extra_postargs, which are 1-1 translated # from the extra_compile_args in the Extension class postargs = extra_postargs['nvcc'] else: postargs = extra_postargs['gcc'] super(obj, src, ext, cc_args, postargs, pp_opts) # reset the default compiler_so, which we might have changed for cuda self.compiler_so = default_compiler_so # inject our redefined _compile method into the class self._compile = _compile # run the customize_compiler
def customize_compiler_for_nvcc(self): """inject deep into distutils to customize how the dispatch to gcc/nvcc works. If you subclass UnixCCompiler, it's not trivial to get your subclass injected in, and still have the right customizations (i.e. distutils.sysconfig.customize_compiler) run on it. So instead of going the OO route, I have this. Note, it's kindof like a wierd functional subclassing going on.""" # tell the compiler it can processes .cu self.src_extensions.append('.cu') # save references to the default compiler_so and _comple methods default_compiler_so = self.compiler_so super = self._compile # now redefine the _compile method. This gets executed for each # object but distutils doesn't have the ability to change compilers # based on source extension: we add it. def _compile(obj, src, ext, cc_args, extra_postargs, pp_opts): if os.path.splitext(src)[1] == '.cu': # use the cuda for .cu files self.set_executable('compiler_so', CUDA['nvcc']) # use only a subset of the extra_postargs, which are 1-1 translated # from the extra_compile_args in the Extension class postargs = extra_postargs['nvcc'] else: postargs = extra_postargs['gcc'] super(obj, src, ext, cc_args, postargs, pp_opts) # reset the default compiler_so, which we might have changed for cuda self.compiler_so = default_compiler_so # inject our redefined _compile method into the class self._compile = _compile # run the customize_compiler
def customize_compiler_for_nvcc(self): """inject deep into distutils to customize how the dispatch to gcc/nvcc works. If you subclass UnixCCompiler, it's not trivial to get your subclass injected in, and still have the right customizations (i.e. distutils.sysconfig.customize_compiler) run on it. So instead of going the OO route, I have this. Note, it's kindof like a wierd functional subclassing going on.""" # tell the compiler it can processes .cu self.src_extensions.append('.cu') # save references to the default compiler_so and _comple methods default_compiler_so = self.compiler_so super = self._compile # now redefine the _compile method. This gets executed for each # object but distutils doesn't have the ability to change compilers # based on source extension: we add it. def _compile(obj, src, ext, cc_args, extra_postargs, pp_opts): print (extra_postargs) if os.path.splitext(src)[1] == '.cu': # use the cuda for .cu files self.set_executable('compiler_so', CUDA['nvcc']) # use only a subset of the extra_postargs, which are 1-1 translated # from the extra_compile_args in the Extension class postargs = extra_postargs['nvcc'] else: postargs = extra_postargs['gcc'] super(obj, src, ext, cc_args, postargs, pp_opts) # reset the default compiler_so, which we might have changed for cuda self.compiler_so = default_compiler_so # inject our redefined _compile method into the class self._compile = _compile # run the customize_compiler
def customize_compiler_for_nvcc(self): """inject deep into distutils to customize how the dispatch to gcc/nvcc works. If you subclass UnixCCompiler, it's not trivial to get your subclass injected in, and still have the right customizations (i.e. distutils.sysconfig.customize_compiler) run on it. So instead of going the OO route, I have this. Note, it's kindof like a wierd functional subclassing going on.""" # tell the compiler it can processes .cu self.src_extensions.append('.cu') # save references to the default compiler_so and _comple methods default_compiler_so = self.compiler_so super = self._compile # now redefine the _compile method. This gets executed for each # object but distutils doesn't have the ability to change compilers # based on source extension: we add it. def _compile(obj, src, ext, cc_args, extra_postargs, pp_opts): print extra_postargs if os.path.splitext(src)[1] == '.cu': # use the cuda for .cu files self.set_executable('compiler_so', CUDA['nvcc']) # use only a subset of the extra_postargs, which are 1-1 translated # from the extra_compile_args in the Extension class postargs = extra_postargs['nvcc'] else: postargs = extra_postargs['gcc'] super(obj, src, ext, cc_args, postargs, pp_opts) # reset the default compiler_so, which we might have changed for cuda self.compiler_so = default_compiler_so # inject our redefined _compile method into the class self._compile = _compile # run the customize_compiler
def configuration(parent_package='', top_path=None): from numpy.distutils.misc_util import Configuration config = Configuration('randomkit', parent_package, top_path) libs = [] if sys.platform == 'win32': libs.append('Advapi32') extensions = [Extension('modl.utils.randomkit.random_fast', sources=['modl/utils/randomkit/random_fast.pyx', 'modl/utils/randomkit/randomkit.c', 'modl/utils/randomkit/distributions.c', ], language="c++", include_dirs=[numpy.get_include(), 'modl/utils/randomkit'], ), Extension('modl.utils.randomkit.sampler', sources=['modl/utils/randomkit/sampler.pyx'], language="c++", include_dirs=[numpy.get_include()] )] config.ext_modules += extensions config.add_subpackage('tests') return config
def configuration(parent_package='', top_path=None): from numpy.distutils.misc_util import Configuration config = Configuration('math', parent_package, top_path) extensions = [Extension('modl.utils.math.enet', sources=['modl/utils/math/enet.pyx'], include_dirs=[numpy.get_include()], ), ] config.ext_modules += extensions config.add_subpackage('tests') return config