Boost.Python 是如何工作的?

发布于 2024-09-18 18:38:12 字数 51 浏览 4 评论 0原文

当解释器是 C 并且是使用 C 编译器构建时,Python 如何能够调用 C++ 对象?

How is Python able to call C++ objects when the interpreter is C and has been built w/ a C compiler?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绾颜 2024-09-25 18:38:12

Boost.Python 有特殊的宏,它们用 extern "C" 声明函数,以便 Python 解释器能够调用它们。这有点复杂,但你可以看看 Boost 文档了解更多信息。

Boost.Python has special macros that declare functions with extern "C" so the Python interpreter will be able to call them. It's kind of complicated, but you can look at the Boost documentation for more info.

疑心病 2024-09-25 18:38:12

Python 声明了一个 C-API(请参阅 http://docs.python.org/2/c -api/http://docs.python.org/3/c -api/)。该 API 定义了一个名为 PyObject 的通用对象类型,它只是一个普通的 C 结构体。该结构定义了(几乎)Python 对象可以执行的所有操作,例如,对该对象进行添加或比较或简单地像函数一样调用它时会发生什么。

因为 python 类型也是对象(因此在 C 中由 PyObject 结构表示),所以定义新类型很简单,只需定义一个新的 PyObject 结构即可。当在 Python 中调用方法时,解释器会将调用转发给与此结构关联的 C 函数。

只要给定的(已编译的)扩展提供了正确的入口点,以便 Python 解释器可以内省它并找出可用的内容(我上面指出的文档确实详细解释了这一点),那么它就可以像任何其他对象一样使用这些对象通常在提示符下可用的对象 - 顺便说一句,它是使用完全相同的 C-API 构建的。您导入已编译的扩展就足够了。

我希望 Python 解释器如何从上面的编译扩展中调用内容已经有些清楚了。唯一缺少的差距是 C-API 如何调用 C++ 代码。

Boost.Python 通过在代码中声明 C 入口点来实现此目的,如下所述:优雅地调用 C++来自C。每次调用时,例如boost::python::class_,它都会为您声明给python的类型执行此操作,从而创建一个代表您的类的PyObject,其中您选择的名称。当您在此类上调用 .def 时,您将填充该结构的内部槽,声明新类型的更多方法、运算符和属性。这些内部槽中的每一个都指向一个 C 风格的函数,该函数只不过是等效 C++ 调用的包装器。

Python declares a C-API (see http://docs.python.org/2/c-api/ or http://docs.python.org/3/c-api/). This API defines a generic object type called PyObject which is just a normal C struct. This structure defines (nearly) everything a python object can do, e.g., what happens when do additions or comparisons on this object or simply call it like a function.

Because python types are also objects (and therefore are represented in C by a PyObject structure), defining a new type is a simple matter of defining a new PyObject struct like that one. When methods are called in Python, the interpreter forwards the call to C functions associated with this structure.

As long as a given (compiled) extension provides the correct entry points such that the Python interpreter can introspect it and find out what is available (the documentation I indicated above does explain this in details), then it can use these objects like any other object you normally have available at the prompt - which BTW, are constructed using the very same C-API. It suffices you import the compiled extension.

I hope it is somewhat clear how the Python interpreter calls stuff from compiled extensions from the above. The sole missing gap is how the C-API calls the C++ code.

Boost.Python does this by declaring C entry points in code along the lines as explained here: Elegantly call C++ from C. Every time you call, e.g., boost::python::class_, it does this for the type you declare to python, creating therefore a PyObject that represents your class, with the name you choose. As you call .def on this class you go filling in the internal slots of that structure, declaring more methods, operators and attributes of your new type. Each of these internal slots points to a C-style function that is nothing but a wrapper to the equivalent C++ call.

樱娆 2024-09-25 18:38:12

C++ 可以通过 extern "C" 声明与 C 进行互操作。

C++ can interoperate with C by extern "C" declarations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文