python C API是否与C ++完全兼容? [英] Is the python C API entirely compatible with C++?

查看:116
本文介绍了python C API是否与C ++完全兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解C和C ++之间的关系,后者本质上是前者的扩展,并保留了一定程度的向后兼容性.可以安全地假设可以使用C ++代码调用python C API吗?

As I understand the relationship between C and C++, the latter is essentially an extension of the former and retains a certain degree of backwards compatibility. Is it safe to assume that the python C API can be called with C++ code?

更重要的是,我注意到官方python文档在同一页面上将C和C ++扩展捆绑在一起.我在任何地方都找不到C ++ API.这使我相信,两种语言都可以安全使用相同的API.

More to the point, I notice that the official python documentation bundles C and C++ extensions together on the same page. Nowhere am I able to find a C++ API. This leads me to believe that the same API is safe to use in both languages.

有人可以确认还是拒绝?

Can someone confirm or deny this?

我想我使我的问题复杂得多了.问题是这样的:为了用C ++编写python模块,我必须做什么?我是否只是按照此处所列的相同指示,用C代码替换C ++?是否有单独的API?

I think I made my question much more complicated than it needs to be. The question is this: what must I do in order to write a python module in C++? Do I just follow the same directions as listed here, substituting C code for C++? Is there a separate API?

推荐答案

我可以确认相同的Python C API可安全用于两种语言(C和C ++). 但是,除非您要提出更具体的问题,否则很难为您提供更详细的答案.您需要注意许多警告和问题.例如,您的Python扩展被定义为C类型的struct而不是C ++,因此不要期望隐式定义其构造函数/析构函数并称为.

I can confirm that the same Python C API is safe to be used in both languages, C and C++. However, it is difficult to provide you with more detailed answer, unless you will ask more specific question. There are numerous caveats and issues you should be aware of. For example, your Python extensions are defined as C types struct, not as C++, so don't expect to have their constructor/destructor implicitly defined and called.

例如,从Python手册中的定义新类型中获取示例代码,它可以用C ++方式编写,甚至可以混入C ++类型:

For example, taking the sample code from Defining New Types in the Python manual, it can be written in C++ way and you can even blend-in C++ types:

// noddy.cpp
namespace {

struct noddy_NoddyObject
{
    PyObject_HEAD
    // Type-specific fields go here.
    std::shared_ptr<int> value; // WARNING
};

PyObject* Noddy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    try {
        Noddy *self = (Noddy *)type->tp_alloc(type, 0);
        if (self) {
            self->value = std::make_shared(7);
            // or more complex operations that may throw
            // or extract complex initialisation as Noddy_init function
            return self;
        }
    }
    catch (...) {
        // do something, log, etc.
    }
    return 0;
}

PyTypeObject noddy_NoddyType =
{
    PyObject_HEAD_INIT(NULL)
    // ...
}

} // unnamed namespace

但是,不会调用std::shared_ptr的构造函数或析构函数. 因此,请记住为noddy_NoddyType定义dealloc函数,您将在其中用nullptr重置value.您可能会问,为什么还要为定义为shared_ptr的值而烦恼.如果您在C ++中使用Python扩展(带异常)来避免类型转换和强制转换,在实现的定义内部进行更多的无缝集成,基于异常的错误处理可能会更容易,等等,这将非常有用. 尽管noddy_NoddyType的对象由纯C语言实现的机器管理,但由于有了dealloc功能,value仍会按照众所周知的RAII规则发布.

But, neither constructor nor destructor of the std::shared_ptr will be called. So, remember to define dealloc function for your noddy_NoddyType where you will reset the value with nullptr. Why even bother with having value defined as shared_ptr, you may ask. It is useful if you use your Python extension in C++, with exceptions, to avoid type conversions and casts, to have more seamless integration inside definitions of your implementation, error handling based on exception may be easier then, etc. And in spite of the fact that your objects of the noddy_NoddyType are managed by machinery implemented in pure C, thanks to dealloc function the value will be released according to well-known RAII rules.

在这里您可以找到有趣的示例,该示例将Python C API与C ++语言几乎无缝集成:如何在c ++代码中捕获Python标准输出

Here you can find interesting example of nearly seamless integration of Python C API with the C++ language: How To catch Python stdout in c++ code

这篇关于python C API是否与C ++完全兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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