boost-python

boost::python: Python list to std::vector

末鹿安然 提交于 2019-11-29 21:26:00
Finally I'm able to use std::vector in python using the [] operator. The trick is to simple provide a container in the boost C++ wrapper which handles the internal vector stuff: #include <boost/python.hpp> #include <vector> class world { std::vector<double> myvec; void add(double n) { this->myvec.push_back(n); } std::vector<double> show() { return this->myvec; } }; BOOST_PYTHON_MODULE(hello) { class_<std::vector<double> >("double_vector") .def(vector_indexing_suite<std::vector<double> >()) ; class_<World>("World") .def("show", &World::show) .def("add", &World::add) ; } The other challenge is:

How to define a Python metaclass with Boost.Python?

做~自己de王妃 提交于 2019-11-29 17:15:57
问题 The Python C API has the PyObject *PyType_Type object, which is equivalent to type in the interpreter. If I want to define a metaclass in C++, how can I set type as one of its bases in Boost.Python? Also, what other things should I take into consideration when defining a Python metaclass in C++? It'd be ideal if there was a Boost.Python solution to this. If not, a solution that uses the Python C API (or a combination of Boost and the C API) is good as well. Since my other classes are exposed

boost_python import error: module does not define init function

谁都会走 提交于 2019-11-29 16:44:07
问题 First off: I looked at the related questions, but they are not very helpful unfortunately. I'm trying to wrap an enum and a class from an external library. #include <Python.h> #include <boost/python.hpp> using namespace boost::python; #include <libvpsc/rectangle.h> using vpsc::Rectangle; using vpsc::Dim; BOOST_PYTHON_MODULE(adaptagrams) { enum_<Dim>("dim") .value("x", vpsc::XDIM) .value("y", vpsc::YDIM) .value("unset", vpsc::UNSET) ; class_<Rectangle>("Rectangle", init<double, double, double,

Boost Python Runtime error when passing object of derived type from python to C++ function expecting a shared_ptr to base type

混江龙づ霸主 提交于 2019-11-29 16:36:31
I have a function that takes a std::shared_ptr, and I want to pass an object of Derived type to this function from python. Here's my class definitions: struct AbstractBase { virtual void foo() = 0; }; struct Derived : public AbstractBase { virtual void foo(){ std::cout<<"Derived's foo!"<<std::endl; } }; struct Unrelated { void bar(std::shared_ptr<AbstractBase> base_shared_ptr) { base_shared_ptr->foo(); } }; #endif /* CLASSES_H */ A simple pure C++ example does what I want: int main() { std::shared_ptr<Derived> d(new Derived); Unrelated u; u.bar(d); } output: Derived's foo! Here is my Boost

Boost.Python and Boost.Signals2: Segmentation faults

拥有回忆 提交于 2019-11-29 12:45:11
I have a problem with integrating boost.signals2 in my existing C++ lib which I have exposed with boost.python. I have a class that is exposed to python with a std::shared_ptr . This class should be able to raise some signals on certain events. Therefore I have exposed a connect_slot function which takes a boost::python::object as argument. If I raise a signal directly after connecting a slot, everything works fine, but if the class raises the signals lateron, I receive segmentation faults. I think this may have something to do with threading in the c++ lib (It is also using boos::asio etc.)

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

不羁岁月 提交于 2019-11-29 12:04:50
问题 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 double-doubles I somehow need to specify my own custom dtype. How to do that? Note: numpy has its own 128bit float (np.float128) unfortunately this maps to long double in C/C++ which is merely an 80bit-float stored in 128bit (on all of my platforms). In

Boost.Python __init__() should return None, not 'NoneType'

大城市里の小女人 提交于 2019-11-29 10:50:19
I have a whole bunch of working C++ code that I want to write Python bindings for. I'm trying to use Boost.Python since it seems to be the easiest way to get this working, but it isn't cooperating. Here's part of the code for the extension module I'm trying to build: BOOST_PYTHON_MODULE(libpcap_ext) { using namespace boost::python; class_<PacketEngine>("PacketEngine") .def("getAvailableDevices", &PacketEngine_getAvailableDevices); } Bjam seems to be a pain and refuses to recognize my Pythonpath or allow me to link with libpcap, so I'm using CMake. Here's my CMakeLists file, which can import

Boost.Python custom exception class

笑着哭i 提交于 2019-11-29 05:57:36
I'm implementing a Python extension module using Boost.Python. The module should define its own custom exception classes that inherit Exception . How do I do that? The following function creates a new Python exception class and adds it to the current scope. If it is called in a module initialization function, then it is added to the module. The first argument is the name of the new exception class. The second argument is the type object for the base class of the new exception class; it defaults to the type object for Exception . The return value is the type object for the new exception class.

Boost.Python custom converter

◇◆丶佛笑我妖孽 提交于 2019-11-29 05:09:00
I have a class taking a vector as parameter (a binary file content). I would like to convert python 'str' type into vector of unsigned char but only for one of my class method. BOOST_PYTHON_MODULE(hello) { class_<Hello>("Hello"). // This method takes a string as parameter and print it .def("printChar", &Hello::printChar) // This method takes a vector<unsigned char> parameter .def("storeFile", &Hello::storeFile) } Using custom converter seems to be what I need but if I modify my boost::python::converter::registry it will be modify for all my calls to printChar and all python methods passing

boost_python import error: module does not define init function

て烟熏妆下的殇ゞ 提交于 2019-11-29 03:08:10
First off: I looked at the related questions, but they are not very helpful unfortunately. I'm trying to wrap an enum and a class from an external library. #include <Python.h> #include <boost/python.hpp> using namespace boost::python; #include <libvpsc/rectangle.h> using vpsc::Rectangle; using vpsc::Dim; BOOST_PYTHON_MODULE(adaptagrams) { enum_<Dim>("dim") .value("x", vpsc::XDIM) .value("y", vpsc::YDIM) .value("unset", vpsc::UNSET) ; class_<Rectangle>("Rectangle", init<double, double, double, double, optional<bool> >()) .add_property("centerX", &Rectangle::getCentreX) .add_property("centerY",