Is the python C API entirely compatible with C++?

僤鯓⒐⒋嵵緔 提交于 2020-01-05 03:52:09

问题


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?

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?

EDIT:

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?


回答1:


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.

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

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.

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




回答2:


Python C API can be called within C++ code. Python C++ extensions are written using the same C API as C extensions use, or using some 3rd party API, such as boost::python.



来源:https://stackoverflow.com/questions/10368141/is-the-python-c-api-entirely-compatible-with-c

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