Running python code in a python object in boost::python
NickName:Jean-Luc Nacif Coelho Ask DateTime:2017-07-14T04:45:56

Running python code in a python object in boost::python

I'm writing a library for python in C++ using boost::python and I've hit a snag.

I'd like to take an object (in this case, an np.array) and run some python code on it (in this case, the code that gets the data pointer from an array). The problem is that the boost tutorial doesn't specifically say how to get a python object that is in the C++ code and pass it to the python code you want to run.

Here is a sketch of what I want to do:

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

void* get_data_ptr(boost::python::object& array) {
    boost::python::object main_module = import("__main__");
    boost::python::object main_namespace = main_module.attr("__dict__");
    boost::python::object ignored = exec(
                  "import ctypes\n"
                  "temp = ctypes.c_void_p.from_buffer(array)\n"
                  "py_ptr = ctypes.addressof(temp)\n",
                  main_namespace); // array would have to go to python somehow

    uintptr_t ptr = extract<uintptr_t>(main_namespace["py_ptr"]);
    // Some operation on the np array
}

How would I do that?

PS: It is important that my method does not have collateral effects, I.E. does not add anything such as a variable to the global namespace that would alter the python code that is running on top of it.

Copyright Notice:Content Author:「Jean-Luc Nacif Coelho」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/45090595/running-python-code-in-a-python-object-in-boostpython

More about “Running python code in a python object in boost::python” related questions

Running python code in a python object in boost::python

I'm writing a library for python in C++ using boost::python and I've hit a snag. I'd like to take an object (in this case, an np.array) and run some python code on it (in this case, the code that ...

Show Detail

Boost::Python Forward Declaration of boost::python::object throwing python TypeError

I'm trying to refactor parts of my project, particularly the Python/C++ interface. The standard boost::python python initialization was working before: boost::python::object main_module = boost::

Show Detail

Python* to boost::python::object

I am trying to build a Python module in C++ that transforms a 2D vector into a Numpy 2D array. What is incorrect here - presumably there is some transformation needed to a boost python object from

Show Detail

BOOST::PYTHON Setting the namespace of a boost::python::object with cppyy

We are trying to set the namespace of a boost::python::object, with the following code In PyROOTGlobals.h we declare: #ifndef PyROOTGlobals_h #define PyROOTGlobals_h #include &lt;boost/python.hpp&...

Show Detail

Boost.Python - Passing boost::python::object as argument to python function?

So I'm working on a little project in which I'm using Python as an embedded scripting engine. So far I've not had much trouble with it using boost.python, but there's something I'd like to do with ...

Show Detail

python::boost::object allocation

I was wondering if it's correct to dynamically allocate an object of type boost::python::object. boost::python::object * obj = new boost::python::object(); Will there be any problems? What about ...

Show Detail

Evaluate None object in boost python

I am passing a list of (mostly) floats to a module in boost python, some elements are None objects. In the C++ code I am extracting the floats like so: for(i=0;i&lt;boost::python::len(list) - wind...

Show Detail

Linker errors running bjam on Boost Python Tutorial

I have been working on solving this problem for fourty hours now, using every boost python resource I could find, and every permutation of installation and building I can think of, but could not find

Show Detail

Find the type of boost python object

I have been embedding python into c++ and I would like to know if there is a way to find the type of the boost::python::object which is a result after executing a function of a python module. I hav...

Show Detail

boost python threading segmentation fault

Consider the following straightforward python extension. When start()-ed, Foo will just add the next sequential integer to a py::list, once a second: #include &lt;boost/python.hpp&gt; #include &lt;

Show Detail