1

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?

Amro
  • 123,847
  • 25
  • 243
  • 454
Louis Thibault
  • 20,240
  • 25
  • 83
  • 152
  • You should able to call the Python C API from C++ but you wouldn't be able to write a Python C module in C++. – Dan D. Apr 28 '12 at 22:33
  • @DanD. I'm a bit confused now, if you look at this page, http://docs.python.org/extending/extending.html, it deals with extending python with C or C++. My original question may be a bit convoluted,so please allow me to rephrase it. If I want to write a python module in C++, do I need to do anything differently from what's on that page (other than using C++ constructs) ? Your response seems to suggest that this is not possible, hence my confusion. – Louis Thibault Apr 28 '12 at 22:37
  • @DanD. Care to detail why? Of course any names exported to Python would have to be in an `extern "C" { }` block due to name mangling (which, of course, cannot export things C can't handle, like overloads), but I'm not aware of any other roadblocks. –  Apr 28 '12 at 22:38

2 Answers2

4

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

Community
  • 1
  • 1
mloskot
  • 37,086
  • 11
  • 109
  • 136
  • First of all, thank you very much for your thorough and detailed answer. I had absolutely no idea of these caveats! Just one quick question: can some of these problems be avoided using `boost.Python`? I've been told that this library is more geared towards C++ development than `Python.h`. – Louis Thibault Apr 28 '12 at 22:55
  • You're welcome. Yes, of course Boost.Python will deal with nearly all such problems for you. My example is based on plain Python C API, but Boost.Python provides you with C++ abstraction on top of Python C API, so you can forget about most of the issues. – mloskot Apr 28 '12 at 22:58
  • 1
    Perfect, I'll start looking at boost, then. Thanks once more -- helpful answers like yours are what keep me coming back to SO! – Louis Thibault Apr 28 '12 at 23:00
1

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.

Kray
  • 186
  • 1
  • 6