How to configure pyximport to always make a cpp file?

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

I want to use c++ functionality most of the time in cython and I find using pyximport very convenient, but making pyxbld configuration file for each pyx module (as described in How do you tell pyximport to use the cython --cplus option?) is tiresome. Can I configure pyximport to always make a c++ output for all pyx modules?

回答1:

Here's a hack.

The following code monkey-patches the get_distutils_extension function in pyximport so that the Extension objects it creates all have their language attribute set to c++.

import pyximport from pyximport import install  old_get_distutils_extension = pyximport.pyximport.get_distutils_extension  def new_get_distutils_extension(modname, pyxfilename, language_level=None):     extension_mod, setup_args = old_get_distutils_extension(modname, pyxfilename, language_level)     extension_mod.language='c++'     return extension_mod,setup_args  pyximport.pyximport.get_distutils_extension = new_get_distutils_extension

Put the above code in pyximportcpp.py. Then, instead of using import pyximport; pyximport.install(), use import pyximportcpp; pyximportcpp.install().



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!