boost-python

Boost.Python: Ownership of pointer variables

我只是一个虾纸丫 提交于 2019-12-06 03:16:00
I'm exposing a C++ tree class using Boost.Python to python. The node class holds a list of child nodes and provides a method void add_child(Node *node) The Node class takes ownership of the provided Node pointer and deletes it's child nodes when the destuctor gets called. I'm exposing the add_child method as: .def("addChild", &Node::add_child) My actual question is: How do i tell Boost.Python that the Node class takes ownership of the child nodes? Because if i execute the following code in python: parentNode = Node() node = Node() parentNode.addChild(node) the object referenced by the node

Exposing OpenCV-based C++ function with Mat/Numpy conversion to Python

对着背影说爱祢 提交于 2019-12-06 02:26:12
I've ran into a problem trying to expose dynamic C++ library functions, linked to OpenCV and using OpenCV's Mat datatype, to Python 2.7 with the use of Numpy ndarray . I've come up with a solution similar to lightalchemist's solution here and I've also tried using boost::python and boost::numpy (also linked to Python 2.7) described in this SO question. Right now I'm sticking with the former. I've got to a point where I can load the module in iPython, and I see a function I'm trying to port using the inspect module, I can even call it and it even executes. However, the problem occurs

How to import a function from python file by Boost.Python

送分小仙女□ 提交于 2019-12-06 02:23:53
问题 I am totally new to boost.python. I reviewed a lot of recommending of using boost.python to apply with python, however still not easy to understand and find a solution for me. What I want is to import a function or class that directly from a python "SourceFile" Example File: Main.cpp MyPythonClass.py Let's says if there is a "Dog" class in "MyPythonClass.py" with "bark()" function, how do I get callback and send argument in cpp? I have no idea what I should do! Please help me! 回答1: When one

libboost_python3.so.1.56.0: undefined symbol: PyClass_Type

半世苍凉 提交于 2019-12-06 01:45:22
问题 I'm trying to create a helloWorld module for Python3 in C++ using boost::python library. Here is a CmakeList.txt : set(Python_ADDITIONAL_VERSIONS 3.4) find_package( PythonLibs 3.4 REQUIRED ) include_directories( ${PYTHON_INCLUDE_DIRS} ) find_package( Boost 1.56.0 EXACT COMPONENTS python3 REQUIRED ) include_directories( ${Boost_INCLUDE_DIR} ) # Define the wrapper library that wraps our library add_library( hello SHARED main.cpp ) target_link_libraries( hello ${Boost_LIBRARIES} ${PythonLibs

Converting python.io object to std::istream when using boost::python

徘徊边缘 提交于 2019-12-05 22:13:41
While writing my first django application I've faced the following problem with boost::python . From python code, I need to pass io.BytesIO to the C++ class which takes std::istream . I have a legacy C++ library for reading files of certain format. Let's call is somelib . The interface of this library uses std::istream as an input. Something like this: class SomeReader { public: bool read_from_stream(std::istream&); }; And I want to wrap it, so that I can use my lib from python in the following way: reader = somelib.SomeReader() print ">>Pyhton: reading from BytesIO" buf = io.BytesIO("Hello

Need help getting started with Boost.Python

萝らか妹 提交于 2019-12-05 21:28:51
问题 I'm trying to build my first Boost.Python example. #include <iostream> #include <boost/python.hpp> using namespace boost::python; class Hello { public: std::string greet() { std::cout << "Hello World" << std::endl; } }; BOOST_PYTHON_MODULE(hello) { class_<Hello>("Hello") .def("greet", &Hello::greet); } int main() { std::cout << "Boost.Python Test" << std::endl; Hello hello; hello.greet(); return 0; } EDIT: Python development headers were missing, as @cdhowie has pointed out. I have found and

How to organise python / Boost Python projects

依然范特西╮ 提交于 2019-12-05 20:50:18
I have a python project, to which I would like to interface with some C++ libraries using Boost::Python. I would like to know how others go about organising their python/boost::python/C++ code within the same project. By organisation I mean in terms of file/directory structure, build procedures etc. In what follows, pif denotes Python InterFace. First I've got a generic header file, called conv_pif.hpp, which has Boost headers and C++ Std Library headers and such. Then for each boost python module, I have a file (here corresponding to the example module genocpp) of the form string_pif.cpp,

How to override the automatically created docstring data for Boost::Python?

心不动则不痛 提交于 2019-12-05 20:19:53
问题 I am currently working developing a C++-based module for Python. I have found that Boost::Python is working quite well for what I want to accomplish. However, I am now running into some issues with the docstring that is being generated by Boost::Python. Given the following Boost::Python definitions: BOOST_PYTHON_MODULE(gcsmt) { class_<gcsmt::Units>("Units", "Sets the units used as input.", no_init) .def("PrintSupported", &gcsmt::Units::printSupported, "Print out all supported units.") .def(

Boost.python overloaded constructor for numpy array and python list

≡放荡痞女 提交于 2019-12-05 19:40:30
Given a C++ class exposed with Boost.Python, how do I expose two constructors: one that takes a numpy array, and another that takes a python list? I'm not a 100% on what you mean, but I'm assuming that you want to have a constructor taking a Python list and another one taking a numpy array. There are a couple of ways to go about this. The easiest way is by using the make_constructor function and overloading it: using boost; using boost::python; shared_ptr<MyClass> CreateWithList(list lst) { // construct with a list here } shared_ptr<MyClass> CreateWithPyArrayObject(PyArrayObject* obj) { //

Return a structure to Python from C++ using BOOST.python

混江龙づ霸主 提交于 2019-12-05 17:57:32
I have written a C++ method from which I need to return a structure to Python. I'm already able to send an OpenCV mat from Python to C++ using BOOST following the method described in this link . Now I need to go the other way; return from C++ to Python, and then access that structure in Python. Can it be done? Any samples or reference links would be good. I have tried googling before posting this question and I couldn't get any samples or explanation links. You can use another function from modules/python/src2/cv2.cpp: PyObject* pyopencv_from(const cv::Mat& m) { if( !m.data ) Py_RETURN_NONE;