【问题标题】:Cython conditional compilation based on external value given via `setuptools`Cython 条件编译基于通过“setuptools”给出的外部值
【发布时间】:2015-02-01 02:37:28
【问题描述】:

我尝试从 Cython pyx 文件有条件地生成 C 代码。我在 Cython 文档中发现,我可以使用 DEF 定义一个值,并使用 IF 根据定义的值有条件地生成代码,但是我如何从 @987654326 通过 Extension 设置来自 setup.py 的值@。

谢谢

【问题讨论】:

标签: python cython setuptools conditional-compilation


【解决方案1】:

感谢您的链接。

setup.py 中有趣的标志是cython_compile_time_env。并从 Cython 导入 Extension

from setuptools import setup
from Cython.Distutils.extension import Extension

ext = Extension(
    name,
    include_dirs=include_dirs,
    cython_compile_time_env=dict(OPENMP=True),
    sources=['test.pyx'])

setup(name=name,
      cmdclass=dict(build_ext=build_ext),
      ext_modules=[ext])

而在test.pyx

...
IF OPENMP:
#Do openmp
ELSE:
#No openmp
...

Cython 条件语句(上面的IF...ELSE)记录在here

【讨论】:

  • 这里是the report of this feature being added to Cython。它没有很好的记录。有时有人被告知使用关键字pyrex_compile_time_env 而不是cython_compile_time_env。另外值得知道的是,如果您的 setuptools 目录可能无法以不明显的方式清理:因此使用您期望的新编译时间变量再次构建将与先前缓存的结果一起使用。即使python setup.py clean --all 也没有避免这种情况。在每次构建之前,我最终只是 touching 我的 .pyx 文件。
  • 函数cythonize的用户相关:github.com/cython/cython/issues/1572
  • compile_time_env的参数compile_time_env的示例用法cythonizegithub.com/pywr/pywr/blob/…
  • 在更复杂的设置中,您可能希望在稍后获得更多类似class custom_build_ext(build_ext): ... def build_extensions(self): self.cython_compile_time_env = {}的信息时修改环境(只是一个字典)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-15
  • 1970-01-01
相关资源
最近更新 更多