boost-python

Using boost-python with C++ in Linux

My development shop has put together a fairly useful Python-based test suite, and we'd like to test some Linux-based C++ code with it. We've gotten the test project they ship with Boost to compile (type 'bjam' in the directory and it works), but we're having issues with our actual project. Building the boost libraries and bjam from sour...

Python embedded in CPP: how to get data back to CPP

While working on a C++ project, I was looking for a third party library for something that is not my core business. I found a really good library, doing exactly what's needed, but it is written in Python. I decided to experiment with embedding Python code in C++, using the Boost.Python library. The C++ code looks something like this: #...

[boost.python] How to create python object instance of class derived from abstract c++ class?

Hello, This is my code: // c++ (main.cpp) #include <vector> #include <iostream> #include <boost/python.hpp> #include <boost/python/enum.hpp> #include <boost/python/def.hpp> #include <boost/python/module.hpp> using namespace std; using namespace boost; using namespace boost::python; class Base { public: Base(void) {} virtual void ...

How do I import modules in boost::python embedded python code?

Hello, I'm using boost::python to embed some python code into an app. I was able to get print statements or other expressions to be evaluated properly, but when I try to import modules, it is not importing and application is exiting. Further the globals() function call in the embedded code gives a runtime error too. #include <boost/pyth...

How to pass pointer to an array in Python for a wrapped C++ function

I am new to C++/Python mixed language programming and do not have much idea about Python/C API. I just started using Boost.Python to wrap a C++ library for Python. I am stuck at wrapping a function that takes pointer to an array as an argument. Following (2nd ctor) is its prototype in C++. class AAF{ AAF(AAF_TYPE t); AAF(double v0, ...

Boost.Python and Python exceptions.

Hello! How can I make boost.python code python exceptions aware? For example, int test_for(){ for(;;){ } return 0; } doesn't interrupt on Ctrl-C, if I export it to python. I think other exceptions won't work this way to. This is a toy example. My real problem is that I have a C function that may take hours to compute. And I...

How to add a custom implicit conversion to a C++ type in Boost::Python?

In my C++ code I have a class Foo with many methods taking a Bar type variable as an argument: class Foo { public: void do_this(Bar b); void do_that(Bar b); /* ... */ }; Bar has a number of constructors that create a new object from many common types such as int, std::string, float, etc: class Bar { public: Bar(int i); Bar(...

How to overload operators with Boost.Python

I am trying to overload operators of a C++ class using Boost.Python. According to this, I am doing it the right way... but I have a bunch of compiler errors. Here is a simple example I made trying to pinpoint the problem: #include "boost/python.hpp" using namespace boost::python; class number { public: number(int i) : m_Number(i...

Returning a c++ array (pointer) from boost python

I'm currently writing python bindings for a c++ library I'm working on. The library reads some binary file format and reading speed is very important. While optimizing the library for speed, I noticed that std::vector (used in the instances I'm reading) was eating up a lot of processing time, so I replaced those with simple arrays alloca...

How to get Python exception text

I want to embed python in my C++ application. I'm using Boost library - great tool. But i have one problem. If python function throws an exception, i want to catch it and print error in my application or get some detailed information like line number in python script that caused error. How can i do it? I can't find any functions to ge...

Sharing widgets between PyQT and Boost.Python

Hello, I was wondering if it was possible to share widgets between PyQt and Boost.Python. I will be embedding a Python interpreter into an application of mine that uses Qt. I would like users of my application to be able to embed their own UI widgets into UI widgets programmed in C++ and exposed via Boost.Python. Is this possible and ...

Boost.Python - How to return by reference?

I'm using Boost.Python to create Python modules from C++ classes. And I ran into a problem with references. Condider the following case where I have a class Foo with overloaded get methods that can either return by value or reference. Specifying that the return by value should be used was easy once I typedefed a signature. But I think ...

Using Boost Python with Weak Ptrs?

Trying to set up a dependency in C++ with a parent-child relationship. The parent contains the child and the child has a weak pointer to the parent. I would also like to be able to derive from the parent in Python. However, when I do this, I get a weak pointer error connecting up this parent-child relationship. C++ code: #include <boo...

Pointers to members in swig (or Boost::Python)

Hello, I made some bindings from my C++ app for python. The problem is that I use pointers to members (It's for computing shortest path and giving the property to minimize as parameter). This is the C++ signature: std::vector<Path> martins(int start, int dest, MultimodalGraph & g, float Edge::*) This is what I did (from what I unde...

Problem installing Shoutpy + Boost.python on opensolaris

Hi all, Im trying to install shoutpy on opensolaris 2009.6. It relies on boost.python. i've installed the boost_devel libraries from blastwave and linked /opt/csw/include/boost to /usr/include/boost . But when I try to easy_install shoutpy I get the following output munderwo@opensolaris-test1:/usr/include$ pfexec easy_install shoutpy S...

Boost.Python: __init__ accepting None argument.

I have a C++ value type wrapped with Boost.Python which has a concept of a NULL value. The relevant parts of the wrapper code appear as follows: class_<TCurrency> currency( "TCurrency" ) .def( init<long>() ) .def( init<const std::string&>() ) <...>; Currently, trying to create a NULL instance in Python by passing None to ...

Boost.Python: Defining a constructor outside a class

Given a class: class TCurrency { TCurrency(); TCurrency(long); TCurrency(const std::string); ... }; Wrapped with Boost.Python: class_<TCurrency>( "TCurrency" ) .def( init<long> ) .def( init<const std::string&> ) ... ; Is it possible to create a factory method that appears as a constructor in Python: ...

Operator= in Boost::Python

If I have something like the following class class Foo { private: int _bar; public: Foo& operator=( const Foo& other ) { _bar = other._bar; return *this; } } Is there an easy way to export that functionality to python using boost::python? The documentation does not list and nice and easy .def( self =...

Python, Threads, the GIL, and C++

Is there some way to make boost::python control the Python GIL for every interaction with python? I am writing a project with boost::python. I am trying to write a C++ wrapper for an external library, and control the C++ library with python scripts. I cannot change the external library, only my wrapper program. (I am writing a functi...

exposing std::vector<double> with boost.python

I have written some C++ code that generates a std::vector. I also have a python script that manipulates some data that, for now, I am declaring like this (below). import numpy x = numpy.random.randn(1000) y = numpy.random.randn(1000) I can run the script fine. From my C++ code: using namespace boost::python; try{ ...