cppyy 是自动、运行时、Python-C++ 绑定生成器,用于从 Python 调用 C++,和从 C++ 调用 Python。运行时生成使详细特殊化能够高性能,惰性加载可缩减大型工程的内存使用,Python 端交叉继承和回调为工作于 C++ 框架、运行时模板实例化、对象自动向下铸造、异常映射、交互探索 C++ 库。cppyy 兑现这些无需任何语言扩展、中间语言、或手动编写样板代码。关于设计/性能,参阅此 PyHPC paper ,尽管 CPython/cppyy 性能已有巨大改进。
cppyy 基于 Cling ,C++ 解释器,以匹配 Python 的动态、交互、运行时行为。考虑此会话以动态、交互方式展示 C++ 和 Python 特征的混合 (整个文档编制有更多范例和在 tutorial ):
>>> import cppyy >>> cppyy.cppdef(""" ... class MyClass { ... public: ... MyClass(int i) : m_data(i) {} ... virtual ~MyClass() {} ... virtual int add_int(int i) { return m_data + i; } ... int m_data; ... };""") True >>> from cppyy.gbl import MyClass >>> m = MyClass(42) >>> cppyy.cppdef(""" ... void say_hello(MyClass* m) { ... std::cout << "Hello, the number is: " << m->m_data << std::endl; ... }""") True >>> MyClass.say_hello = cppyy.gbl.say_hello >>> m.say_hello() Hello, the number is: 42 >>> m.m_data = 13 >>> m.say_hello() Hello, the number is: 13 >>> class PyMyClass(MyClass): ... def add_int(self, i): # python side override (CPython only) ... return self.m_data + 2*i ... >>> cppyy.cppdef("int callback(MyClass* m, int i) { return m->add_int(i); }") True >>> cppyy.gbl.callback(m, 2) # calls C++ add_int 15 >>> cppyy.gbl.callback(PyMyClass(1), 2) # calls Python-side override 5 >>>
有了现代 C++ 编译器的支持,Cppyy 面向未来。考虑以下会话使用
boost::any
,胶囊类型允许以 C++ 异构容器。
Boost
库以其对现代 C++ 的不设限使用和大量使用模板而闻名:
>>> import cppyy >>> cppyy.include('boost/any.hpp') # assumes you have boost installed >>> from cppyy.gbl import std, boost >>> val = boost.any() # the capsule >>> val.__assign__(std.vector[int]()) # assign it a std::vector<int> <cppyy.gbl.boost.any object at 0xf6a8a0> >>> val.type() == cppyy.typeid(std.vector[int]) # verify type True >>> extract = boost.any_cast[int](std.move(val)) # wrong cast Traceback (most recent call last): File "<stdin>", line 1, in <module> cppyy.gbl.boost.bad_any_cast: Could not instantiate any_cast<int>: int boost::any_cast(boost::any&& operand) => wrapexcept<boost::bad_any_cast>: boost::bad_any_cast: failed conversion using boost::any_cast >>> extract = boost.any_cast[std.vector[int]](val) # correct cast >>> type(extract) is std.vector[int] True >>> extract += xrange(100) >>> len(extract) 100 >>> val.__assign__(std.move(extract)) # move forced <cppyy.gbl.boost.any object at 0xf6a8a0> >>> len(extract) # now empty (or invalid) 0 >>> extract = boost.any_cast[std.vector[int]](val) >>> list(extract) [0, 1, 2, 3, 4, 5, 6, ..., 97, 98, 99] >>>
当然,没有理由使用 Boost 从 Python (事实上,此范例调用了 pythonizations ),但它展示了 cppyy 无缝支持很多高级 C++ 特征。
cppyy 可用于 CPython (v2 和 v3) 和 PyPy ,采用后者可达到像 C++ 的性能。它明智地使用预编译头、动态加载及惰性实例化,以支持由数百万行代码和数千个类组成的 C++ 程序。cppyy 依赖最少,允许在分布式、异构、开发环境下使用。
快速入门
Features
重新分发
开发者
背景
请报告 Bug 或请求改进有关 问题追踪程序 .