python兼容c吗,python C API是否与C ++完全兼容?

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?

解决方案

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 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值