gargynko
gargynko

Reputation: 13

boost::python - C++ calling Python calling C++

I have an application which will be extensible by Python code but the extensing code will also be using code from the application. Example of what I am trying to achieve:

// wrapped_module.cpp

void api_call()
{
 std::cout << "API call" << std::endl;
}

BOOST_PYTHON_MODULE(API)
{
  boost::python::def("api_call", api_call);
}

// extension.py

import API

def myExtension()
 # ... some python work ... #
 API.api_call()

// application_main.cpp

int main()
{
 // initialize interpreter
 // load "extension.py"
 // make "API" module available for "extension.py", so "import API" works
 // load "myExtension" from "extension.py"
 // myExtension()
 // <see "API call" in console output>

 return 0;
}

"extension.py" will never be called as a standalone script, it will always be loaded by c++ application - therefore I do not need to separately build API.dll module for python to import - or do I?

Upvotes: 0

Views: 1064

Answers (1)

n. m. could be an AI
n. m. could be an AI

Reputation: 119847

This is possible but not entirely straightforward.

You need to use an undocumented generated function name, in your case initAPI (it is composed from the word init and your module name, case-sensitive). You need to pass this function as an argument to PyImport_AppendInittab before calling Py_Initialize.

Update In Python 3, use PyInit_API instead if initAPI.

Below is a full working program that embeds Python and extends it with your module, then runs a simple program that uses your module.

#include <boost/python.hpp>
#include <iostream>

void api_call()
{
     std::cout << "API call" << std::endl;
}

BOOST_PYTHON_MODULE(API)
{
      boost::python::def("api_call", api_call);
}

int main (int argc, char* argv[])
{
    // Import your module to embedded Python
    PyImport_AppendInittab("API", &initAPI);

    // Initialise Python
    Py_Initialize();

    // Run Python code
    PyRun_SimpleString("import API\n"
                       "API.api_call()\n");

}

Upvotes: 1

Related Questions