Search code examples
c++boostboost-python

Parameter passing from Python script to C++ with boost-python


I am currently embedding Python in C++ using boost-python and boost-numpy. I have the following Python test script:

import numpy as np
import time


def test_qr(m,n):
    print("create numpy array")
    A = np.random.rand(m, n)

    print("Matrix A is {}".format(A))
    print("Lets QR factorize this thing! Mathematics is great !!")
    ts = time.time()
    Q, R = np.linalg.qr(A)
    te = time.time()
    print("It took {} seconds to factorize A".format(te - ts))
    print("The Q matrix is {}".format(Q))
    print("The R matrix is {}".format(R))
    return Q,R


def sum(m,n):
    return m+n

I am able to execute a part of the code in C++ like this:

namespace p = boost::python;
namespace np = boost::python::numpy;
int main() {
Py_Initialize();  //initialize python environment
np::initialize(); //initialize numpy environment
p::object main_module = p::import("__main__");
p::object main_namespace = main_module.attr("__dict__");

// execute code in the main_namespace
p::exec_file("/Users/Michael/CLionProjects/CythonTest/test_file.py",main_namespace); //loads python script
p::exec("m = 100\n"
        "n = 100\n"
        "Q,R = test_qr(m,n)", main_namespace);

np::ndarray Q_matrix = p::extract<np::ndarray>(main_namespace["Q"]); // extract results as numpy array types
np::ndarray R_matrix = p::extract<np::ndarray>(main_namespace["R"]);
std::cout<<"C++ Q Matrix: \n" << p::extract<char const *>(p::str(Q_matrix)) << std::endl; // extract every element as a
std::cout<<"C++ R Matrix: \n" << p::extract<char const *>(p::str(R_matrix)) << std::endl;
std::cout<<"code also works with numpy, ask for a raise" << std::endl;
p::object sum = main_namespace.attr("sum")(10,10);
int result = p::extract<int>(main_namespace.attr("sum")(10,10));
std::cout<<"sum result works " << result << std::endl;
return 0;}

Now I am trying to use the sum function in the Python script but I do not always want to write a string like:

p::exec("m = 100\n"
        "n = 100\n"
        "Q,R = test_qr(m,n)", main_namespace);}

How can this be done without using the exec function?

I have tried things like:

p::object sum = main_namespace.attr("sum")(10,10);
int result = p::extract<int>(main_namespace.attr("sum")(10,10));
std::cout<<"sum result works " << result << std::endl;

As mentioned in the documentation of boost. I also tried using the call_method function, but it didn't work. I get either boost::python::error_already_set exception which mean there is something wrong in Python, but I do not know what. Or an exit code 11.


Solution

  • The issue is rather trivial. Let's look at the tutorial you mention:

    object main_module = import("__main__");
    object main_namespace = main_module.attr("__dict__");
    object ignored = exec("result = 5 ** 2", main_namespace);
    int five_squared = extract<int>(main_namespace["result"]);
    

    Notice how they extract the result object in the last line: main_namespace["result"]

    The main_namespace object is a Python dictionary, and rather than extracting it's attribute, you're just looking for a value stored with the particular key. Hence, indexing with [] is the way to go.

    C++ code:

    #define BOOST_ALL_NO_LIB
    
    #include <boost/python.hpp>
    #include <boost/python/numpy.hpp>
    
    #include <iostream>
    
    namespace bp = boost::python;
    
    int main()
    {
        try {
            Py_Initialize();
    
            bp::object module = bp::import("__main__");
            bp::object globals = module.attr("__dict__");
    
            bp::exec_file("bpcall.py", globals);
    
            bp::object sum_fn = globals["sum"];
            int result = bp::extract<int>(sum_fn(1,2));
            std::cout << "Result (C++) = " << result << "\n";
    
        } catch (bp::error_already_set) {
            PyErr_Print();
        }
    
        Py_Finalize();
    }
    

    Python script:

    def sum(m,n):
        return m+n
    

    Output:

    Result (C++) = 3