如何配置pyximport始终创建一个cpp文件? [英] How to configure pyximport to always make a cpp file?

查看:161
本文介绍了如何配置pyximport始终创建一个cpp文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想大部分时间在cython中使用c ++功能,我发现使用pyximport非常方便,但是为每个pyx模块制作pyxbld配置文件(如您如何告诉pyximport使用cython --cplus选项?)很烦人。我可以配置pyximport始终为所有pyx模块生成c ++输出吗?

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?

推荐答案

这是一个hack。

以下代码对 pyximport 中的 get_distutils_extension 函数进行了猴子修补,以便<它创建的code>扩展对象的语言属性均设置为 c ++

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

输入上面的代码在pyximportcpp.py中。然后,不要使用 import pyximport; pyximport.install(),使用 import pyximportcpp; pyximportcpp.install()

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

这篇关于如何配置pyximport始终创建一个cpp文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆