Questions tagged [boost-python]

Library for intuitive and tight integration between C++ and Python.

Boost::python is a C++ library offering seamless integration of C++ and Python with reference handling, built-in and user-defined two-way type conversions, Python/C++ exception translation, function overloading, exposing class hierarchies, docstring support and more.

1337 questions
13
votes
2 answers

Is wrapping C++ library with ctypes a bad idea?

I read through the following two threads on wrapping C library and C++ library, I am not sure I get it yet. The C++ library I am working with does use class and template, but not in any over-sophisticated way. What are issues or caveats of wrapping…
Oliver
  • 3,592
  • 8
  • 34
  • 37
13
votes
2 answers

static openCL class not properly released in python module using boost.python

EDIT: Ok, all the edits made the layout of the question a bit confusing so I will try to rewrite the question (not changing the content, but improving its structure). The issue in short I have an openCL program that works fine, if I compile it as an…
NOhs
  • 2,780
  • 3
  • 25
  • 59
13
votes
1 answer

Exposing C++ functions, that return pointer using Boost.Python

I want to expose the following C++ function to Python using Boost.Python: int* test1() { return new int(42); } // Now exposing the function with Boost.Python BOOST_PYTHON_MODULE(libtest1) { using namespace boost::python; def("test1",…
avli
  • 1,121
  • 1
  • 10
  • 17
13
votes
1 answer

How to expose raw byte buffers with Boost::Python?

I've got third party C++ library in which some class methods use raw byte buffers. I'm not quite sure how to deal in Boost::Python with it. C++ library header is something like: class CSomeClass { public: int load( unsigned char *&…
vartec
  • 131,205
  • 36
  • 218
  • 244
13
votes
1 answer

How to define custom float-type numpy dtypes (C-API)

I have a custom float data type that emulates 128bit floats using two 64bit floats (the double-double class dd_real from the QD library). From C++ I want to export an ndarray to python. I already know how to do this for 64bit floats, but for…
H. Brandsmeier
  • 957
  • 5
  • 19
12
votes
1 answer

boost::python - how to invoke a python function in its own thread from C++?

I have a module written in python. this module is sort of an interface to many different functionalities I implemented in Python: EmbeddingInterface.py simply imports this module and creates an instance: import CPPController cppControllerInstance =…
Elad Maimoni
  • 3,703
  • 3
  • 20
  • 37
12
votes
2 answers

How to add a property to a module in boost::python?

You can add a property to a class using a getter and a setter (in a simplistic case): class("X") .add_property("foo", &X::get_foo, &X::set_foo); So then you can use it from python like this: >>> x = mymodule.X() >>> x.foo = 'aaa' >>>…
Alex B
  • 82,554
  • 44
  • 203
  • 280
12
votes
1 answer

Exposing a pointer in Boost.Python

I have this very simple C++ class: class Tree { public: Node *head; }; BOOST_PYTHON_MODULE(myModule) { class_("Tree") .def_readwrite("head",&Tree::head) ; } I want to access the head variable from Python, but the…
Vlad the Impala
  • 15,572
  • 16
  • 81
  • 124
12
votes
4 answers

running c++ code from python

I want to execute a code helloword.cpp which takes in some argument from console parses those arguments and then prints "hello world" in the console. Now, I want to parse these arguments from a python scripts parsearguments.py So for example: def…
frazman
  • 32,081
  • 75
  • 184
  • 269
11
votes
3 answers

How to reinitialise an embedded Python interpreter?

I'm working on embedding Python in our test suite application. The purpose is to use Python to run several tests scripts to collect data and make a report of tests. Multiple test scripts for one test run can create global variables and functions…
nab
  • 191
  • 3
  • 9
11
votes
2 answers

Cmake could not find boost_python

I am trying to build this simple boost python demo from this link on my MacOS High Sierra. Following is the hello_ext.cpp: #include char const* greet() { return "hello, world"; } BOOST_PYTHON_MODULE(hello_ext) { using…
AdeleGoldberg
  • 1,289
  • 3
  • 12
  • 28
11
votes
7 answers

Is it possible to modify PYTHONPATH at runtime?

I have a C++ application dynamically linked to the Python interpreter. I want to be able to import python modules from a particular directory. I want to modify the PYTHONPATH for my process so that sys.path will include the paths that I added to…
Skrymsli
  • 5,173
  • 7
  • 34
  • 36
11
votes
1 answer

Boost-python How to pass a c++ class instance to a python class

I am new to boost python. I have to first init a cpp class instance in cpp code, and then pass this cpp instance to python code, use a python class instance to invoke it(the cpp instance). I have tried the Python/C API way, but failed, so I wonder…
Wenbo
  • 123
  • 1
  • 6
11
votes
1 answer

Boost.Python return python object which references to existing c++ objects

Suppose I want to get the reference to some global / internal c++ object, one method is to declare function with boost::python::return_value_policy(). Both GetGlobalObjectA and GetGlobalObjectB return the reference to the…
lostyzd
  • 4,515
  • 3
  • 19
  • 33
11
votes
1 answer

Boost.Python add bindings to existing PyObject (for exception handling)

In order to expose a C++ exception to Python in a way that actually works, you have to write something like: std::string scope = py::extract(py::scope().attr("__name__")); std::string full_name = scope + "." + name; PyObject* exc_type =…
Barry
  • 286,269
  • 29
  • 621
  • 977