def run_setup(with_optional_extensions):
if with_optional_extensions:
ext_modules=[Extension(
"acebitstream", ["c/acebitstream_mod.c", "c/acebitstream.c"],
define_macros=[(sys.byteorder.upper()+'_ENDIAN_SWAP', 1)]
)]
else:
ext_modules=[]
setup(
name='acefile',
version=acefile.__version__,
description=title,
long_description=desc,
url=acefile.__url__,
author=acefile.__author__,
author_email=acefile.__email__,
license=acefile.__license__,
platforms=['all'],
classifiers=[
# https://pypi.python.org/pypi?%3Aaction=list_classifiers
'Development Status :: 4 - Beta',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Topic :: System :: Archiving :: Compression',
],
keywords=['ace', 'unace', 'compression', 'decompression', 'archive'],
py_modules=['acefile'],
ext_modules=ext_modules,
entry_points = {
'console_scripts': [
'acefile-unace=acefile:unace',
],
},
test_suite = 'acefile.testsuite',
)
python类Extension()的实例源码
def test_abi3_filename(self):
"""
Filename needs to be loadable by several versions
of Python 3 if 'is_abi3' is truthy on Extension()
"""
print(get_abi3_suffix())
extension = Extension('spam.eggs', ['eggs.c'], py_limited_api=True)
dist = Distribution(dict(ext_modules=[extension]))
cmd = build_ext(dist)
cmd.finalize_options()
assert 'spam.eggs' in cmd.ext_map
res = cmd.get_ext_filename('spam.eggs')
if six.PY2 or not get_abi3_suffix():
assert res.endswith(get_config_var('SO'))
elif sys.platform == 'win32':
assert res.endswith('eggs.pyd')
else:
assert 'abi3' in res
def test_abi3_filename(self):
"""
Filename needs to be loadable by several versions
of Python 3 if 'is_abi3' is truthy on Extension()
"""
print(get_abi3_suffix())
extension = Extension('spam.eggs', ['eggs.c'], py_limited_api=True)
dist = Distribution(dict(ext_modules=[extension]))
cmd = build_ext(dist)
cmd.finalize_options()
assert 'spam.eggs' in cmd.ext_map
res = cmd.get_ext_filename('spam.eggs')
if six.PY2 or not get_abi3_suffix():
assert res.endswith(get_config_var('SO'))
elif sys.platform == 'win32':
assert res.endswith('eggs.pyd')
else:
assert 'abi3' in res
def ext(extName):
extPath = extName.replace(".", os.path.sep)+".pyx"
return Extension( extName,
[extPath],
extra_compile_args=my_extra_compile_args_nonmath
)
def ext_math(extName):
extPath = extName.replace(".", os.path.sep)+".pyx"
return Extension( extName,
[extPath],
extra_compile_args=my_extra_compile_args_math,
extra_link_args=my_extra_link_args,
libraries=["m"] # "m" links libm, the math library on unix-likes; see http://docs.cython.org/src/tutorial/external.html
)
# http://stackoverflow.com/questions/13628979/setuptools-how-to-make-package-contain-extra-data-folder-and-all-folders-inside
def run(self):
try:
build_ext.run(self)
except Exception:
e = sys.exc_info()[1]
sys.stdout.write('%s\n' % str(e))
warnings.warn(self.warning_message % ("Extension modules",
"There was an issue with "
"your platform configuration"
" - see above."))
def _ext_modules(self, poet):
"""
Builds the extension modules.
Transforms the extensions section:
[extensions]
"my.module" = "my/module.c"
to a proper extension:
Extension('my.module', 'my/module.c')
:param poet: The Poet instance for which to build.
:type poet: poet.poet.Poet
:rtype: dict
"""
extensions = []
for module, source in poet.extensions.items():
if not isinstance(source, list):
source = [source]
extensions.append(Extension(module, source))
return {
'ext_modules': extensions
}
def get_extension_modules(config):
"""Handle extension modules"""
EXTENSION_FIELDS = ("sources",
"include_dirs",
"define_macros",
"undef_macros",
"library_dirs",
"libraries",
"runtime_library_dirs",
"extra_objects",
"extra_compile_args",
"extra_link_args",
"export_symbols",
"swig_opts",
"depends")
ext_modules = []
for section in config:
if ':' in section:
labels = section.split(':', 1)
else:
# Backwards compatibility for old syntax; don't use this though
labels = section.split('=', 1)
labels = [l.strip() for l in labels]
if (len(labels) == 2) and (labels[0] == 'extension'):
ext_args = {}
for field in EXTENSION_FIELDS:
value = has_get_option(config, section, field)
# All extension module options besides name can have multiple
# values
if not value:
continue
value = split_multiline(value)
if field == 'define_macros':
macros = []
for macro in value:
macro = macro.split('=', 1)
if len(macro) == 1:
macro = (macro[0].strip(), None)
else:
macro = (macro[0].strip(), macro[1].strip())
macros.append(macro)
value = macros
ext_args[field] = value
if ext_args:
if 'name' not in ext_args:
ext_args['name'] = labels[1]
ext_modules.append(Extension(ext_args.pop('name'),
**ext_args))
return ext_modules
def get_extension_modules(config):
"""Handle extension modules"""
EXTENSION_FIELDS = ("sources",
"include_dirs",
"define_macros",
"undef_macros",
"library_dirs",
"libraries",
"runtime_library_dirs",
"extra_objects",
"extra_compile_args",
"extra_link_args",
"export_symbols",
"swig_opts",
"depends")
ext_modules = []
for section in config:
if ':' in section:
labels = section.split(':', 1)
else:
# Backwards compatibility for old syntax; don't use this though
labels = section.split('=', 1)
labels = [l.strip() for l in labels]
if (len(labels) == 2) and (labels[0] == 'extension'):
ext_args = {}
for field in EXTENSION_FIELDS:
value = has_get_option(config, section, field)
# All extension module options besides name can have multiple
# values
if not value:
continue
value = split_multiline(value)
if field == 'define_macros':
macros = []
for macro in value:
macro = macro.split('=', 1)
if len(macro) == 1:
macro = (macro[0].strip(), None)
else:
macro = (macro[0].strip(), macro[1].strip())
macros.append(macro)
value = macros
ext_args[field] = value
if ext_args:
if 'name' not in ext_args:
ext_args['name'] = labels[1]
ext_modules.append(extension.Extension(ext_args.pop('name'),
**ext_args))
return ext_modules
def get_extension_modules(config):
"""Handle extension modules"""
EXTENSION_FIELDS = ("sources",
"include_dirs",
"define_macros",
"undef_macros",
"library_dirs",
"libraries",
"runtime_library_dirs",
"extra_objects",
"extra_compile_args",
"extra_link_args",
"export_symbols",
"swig_opts",
"depends")
ext_modules = []
for section in config:
if ':' in section:
labels = section.split(':', 1)
else:
# Backwards compatibility for old syntax; don't use this though
labels = section.split('=', 1)
labels = [l.strip() for l in labels]
if (len(labels) == 2) and (labels[0] == 'extension'):
ext_args = {}
for field in EXTENSION_FIELDS:
value = has_get_option(config, section, field)
# All extension module options besides name can have multiple
# values
if not value:
continue
value = split_multiline(value)
if field == 'define_macros':
macros = []
for macro in value:
macro = macro.split('=', 1)
if len(macro) == 1:
macro = (macro[0].strip(), None)
else:
macro = (macro[0].strip(), macro[1].strip())
macros.append(macro)
value = macros
ext_args[field] = value
if ext_args:
if 'name' not in ext_args:
ext_args['name'] = labels[1]
ext_modules.append(Extension(ext_args.pop('name'),
**ext_args))
return ext_modules
def get_extension_modules(config):
"""Handle extension modules"""
EXTENSION_FIELDS = ("sources",
"include_dirs",
"define_macros",
"undef_macros",
"library_dirs",
"libraries",
"runtime_library_dirs",
"extra_objects",
"extra_compile_args",
"extra_link_args",
"export_symbols",
"swig_opts",
"depends")
ext_modules = []
for section in config:
if ':' in section:
labels = section.split(':', 1)
else:
# Backwards compatibility for old syntax; don't use this though
labels = section.split('=', 1)
labels = [l.strip() for l in labels]
if (len(labels) == 2) and (labels[0] == 'extension'):
ext_args = {}
for field in EXTENSION_FIELDS:
value = has_get_option(config, section, field)
# All extension module options besides name can have multiple
# values
if not value:
continue
value = split_multiline(value)
if field == 'define_macros':
macros = []
for macro in value:
macro = macro.split('=', 1)
if len(macro) == 1:
macro = (macro[0].strip(), None)
else:
macro = (macro[0].strip(), macro[1].strip())
macros.append(macro)
value = macros
ext_args[field] = value
if ext_args:
if 'name' not in ext_args:
ext_args['name'] = labels[1]
ext_modules.append(Extension(ext_args.pop('name'),
**ext_args))
return ext_modules
def declare_cython_extension(extName, use_math=False, use_openmp=False, include_dirs=None):
"""Declare a Cython extension module for setuptools.
Parameters:
extName : str
Absolute module name, e.g. use `mylibrary.mypackage.mymodule`
for the Cython source file `mylibrary/mypackage/mymodule.pyx`.
use_math : bool
If True, set math flags and link with ``libm``.
use_openmp : bool
If True, compile and link with OpenMP.
Return value:
Extension object
that can be passed to ``setuptools.setup``.
"""
extPath = extName.replace(".", os.path.sep)+".pyx"
if use_math:
compile_args = list(my_extra_compile_args_math) # copy
link_args = list(my_extra_link_args_math)
libraries = ["m"] # link libm; this is a list of library names without the "lib" prefix
else:
compile_args = list(my_extra_compile_args_nonmath)
link_args = list(my_extra_link_args_nonmath)
libraries = None # value if no libraries, see setuptools.extension._Extension
# OpenMP
if use_openmp:
compile_args.insert( 0, openmp_compile_args )
link_args.insert( 0, openmp_link_args )
# See
# http://docs.cython.org/src/tutorial/external.html
#
# on linking libraries to your Cython extensions.
#
return Extension( extName,
[extPath],
extra_compile_args=compile_args,
extra_link_args=link_args,
include_dirs=include_dirs,
libraries=libraries
)
# Gather user-defined data files
#
# http://stackoverflow.com/questions/13628979/setuptools-how-to-make-package-contain-extra-data-folder-and-all-folders-inside
#
def declare_cython_extension(extName, use_math=False, use_openmp=False):
"""Declare a Cython extension module for setuptools.
Parameters:
extName : str
Absolute module name, e.g. use `mylibrary.mypackage.subpackage`
for the Cython source file `mylibrary/mypackage/subpackage.pyx`.
use_math : bool
If True, set math flags and link with ``libm``.
use_openmp : bool
If True, compile and link with OpenMP.
Return value:
Extension object
that can be passed to ``setuptools.setup``.
"""
extPath = extName.replace(".", os.path.sep)+".pyx"
if use_math:
compile_args = list(my_extra_compile_args_math) # copy
link_args = list(my_extra_link_args_math)
libraries = ["m"] # link libm; this is a list of library names without the "lib" prefix
else:
compile_args = list(my_extra_compile_args_nonmath)
link_args = list(my_extra_link_args_nonmath)
libraries = None # value if no libraries, see setuptools.extension._Extension
# OpenMP
if use_openmp:
compile_args.insert( 0, openmp_compile_args )
link_args.insert( 0, openmp_link_args )
# See
# http://docs.cython.org/src/tutorial/external.html
#
# on linking libraries to your Cython extensions.
#
return Extension( extName,
[extPath],
extra_compile_args=compile_args,
extra_link_args=link_args,
libraries=libraries
)
#########################################################
# Set up modules
#########################################################
# declare Cython extension modules here
#