boost-python

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

纵然是瞬间 提交于 2019-11-28 00:33:54
问题 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

Boost.Python: Wrap functions to release the GIL

偶尔善良 提交于 2019-11-27 21:35:42
I am currently working with Boost.Python and would like some help to solve a tricky problem. Context When a C++ method/function is exposed to Python, it needs to release the GIL (Global Interpreter Lock) to let other threads use the interpreter. This way, when the python code calls a C++ function, the interpreter can be used by other threads. For now, each C++ function looks like this: // module.cpp int myfunction(std::string question) { ReleaseGIL unlockGIL; return 42; } To pass it to boost python, I do: // python_exposure.cpp BOOST_PYTHON_MODULE(PythonModule) { def("myfunction", &myfunction)

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

痴心易碎 提交于 2019-11-27 21:26:26
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: #include <string> #include <iostream> #include <boost/python.hpp> using namespace boost::python; int main(int, char **) { Py_Initialize(); try { object module((handle<>(borrowed(PyImport_AddModule("__main__"))))); object name_space = module.attr("__dict__");

How to compile static library with -fPIC from boost.python

↘锁芯ラ 提交于 2019-11-27 20:34:11
问题 By default, libboostpython.a is compiled without -fPIC . But I have to make a python extension and it is a dynamic library with -fPIC that links to static libraries. How can I compile a static library ( libboostpython.a ) with -fPIC from boost.python ? 回答1: There are a couple options you could use: Compile boost from source and pass extra compiler options to bjam. E.g. bjam ... cxxflags='-fPIC' . That would compile every boost source file as position independent code. Use boost in the form of

Ubuntu - Linking boost.python - Fatal error: pyconfig cannot be found

只愿长相守 提交于 2019-11-27 20:16:14
问题 Having some issues, now I have read the following: hello world python extension in c++ using boost? I have tried installing boost onto my desktop, and, done as the posts suggested in terms of linking. I have the following code: #include <boost/python.hpp> #include <Python.h> using namespace boost::python; Now I have tried linking with the following: g++ testing.cpp -I /usr/include/python2.7/pyconfig.h -L /usr/include/python2.7/Python.h -lpython2.7 And I have tried the following as well: g++

Boost.Python custom converter

懵懂的女人 提交于 2019-11-27 19:03:40
问题 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:

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

好久不见. 提交于 2019-11-27 18:25:38
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 how to pass a c++ class instance to a python class. The following is my code, changed from the boost python demo. in main.cpp #include <python2.6/Python.h> #include <boost/python.hpp> #include <iostream> using namespace boost::python; using namespace std; class World { private: string name; public: void set(string name) { this->name = name; } void

Writing Python bindings for C++ code that use OpenCV

∥☆過路亽.° 提交于 2019-11-27 17:21:16
I'm trying to write a python wrapper for some C++ code that make use of OpenCV but I'm having difficulties returning the result, which is a OpenCV C++ Mat object, to the python interpreter. I've looked at OpenCV's source and found the file cv2.cpp which has conversions functions to perform conversions to and fro between PyObject* and OpenCV's Mat. I made use of those conversions functions but got a segmentation fault when I tried to use them. I basically need some suggestions/sample code/online references on how to interface python and C++ code that make use of OpenCV, specifically with the

Embedding Python in C++ and calling methods from the C++ code

风格不统一 提交于 2019-11-27 16:59:16
问题 I try to embed a Python script into my C++ program. After reading some things about embedding and extending I understand how to open my own python script and how to pass some integers to it. But now I'm at a point a do not understand how to resolve my problem. I have to do both, calling Python functions from C++ and calling C++ functions from my embedded Python script. But I do not know where I have to start. I know I have to compile a .so file to expose my C++ functions to Python but this is

Boost.Python call by reference : TypeError: No to_python (by-value) converter found for C++ type:

让人想犯罪 __ 提交于 2019-11-27 14:38:04
I'm trying to expose my C++ Classes to Python using Boost.Python. Here is a simplyfied version of what i'm trying to do: I have a class A deriving from boost::noncopyable and a second class B with a method that takes a reference to A as an argument. class A : boost::noncopyable { /*...*/ }; class B { public: virtual void do_something(A& a) { /*...*/ } }; I'm exposing the classes as follows: /* Wrapper for B, so B can be extended in python */ struct BWrap : public B, wrapper<B> { void do_something(A &a) { if (override do_something = this->get_override("do_something")) { do_something(a); return;