boost-python

create boost-python nested namespace

元气小坏坏 提交于 2019-11-27 14:11:31
问题 Using boost python I need create nested namespace. Assume I have following cpp class structure: namespace a { class A{...} namespace b { class B{...} } } Obvious solution not work: BOOST_PYTHON_MODULE( a ) { boost::python::class_<a::A>("A") ... ; BOOST_PYTHON_MODULE(b){ boost::python::class_<a::b::B>("B") ... ; } } It causes compile-time error: linkage specification must be at global scope Is there any way to declare class B that would be accessed from Python as a.b.B ? 回答1: What you want is

pass callback from python to c++ using boost::python

馋奶兔 提交于 2019-11-27 12:28:03
问题 I want to pass callback from my python code to c++ I want my code look something like this: In C++ : typedef void (*MyCallback_t) (CallbackInfo); class MyClass {... void setcallback(MyCallback_t cb); ... } And to use it in python : import mylib def myCallback(mylib_CallbackInfo): ... t = mylib.MyClass() t.setcallback(myCallback) I saw some topics near my problem but couldn't solve it For example here : Realtime processing and callbacks with Python and C++ there is advice to use boost::python

Feeding a Python list into a function taking in a vector with Boost Python

无人久伴 提交于 2019-11-27 11:36:10
I've got a function with the signature: function(std::vector<double> vector); And I've exposed it, but it doesn't take in Python lists. I've looked through the other SO answers, and most involve changing the function to take in boost::python::lists, but I don't want to change the function. I imagine I can use the vector_indexing_suite to write a simple wrapper around this function, but I have many functions of this form and would rather not write a wrapper for every single one. Is there a way to automatically make a Python list->std::vector mapping occur? There are a few solutions to

boost::python Export Custom Exception

試著忘記壹切 提交于 2019-11-27 10:22:10
问题 I am currently writing a C++ extension for Python using Boost.Python. A function in this extension may generate an exception containing information about the error (beyond just a human-readable string describing what happened). I was hoping I could export this exception to Python so I could catch it and do something with the extra information. For example: import my_cpp_module try: my_cpp_module.my_cpp_function() except my_cpp_module.MyCPPException, e: print e.my_extra_data Unfortunately

Boost.Python: How to expose std::unique_ptr

耗尽温柔 提交于 2019-11-27 05:34:38
问题 I am fairly new to boost.python and trying to expose the return value of a function to python. The function signature looks like this: std::unique_ptr<Message> someFunc(const std::string &str) const; When calling the function in python, I get the following error: TypeError: No to_python (by-value) converter found for C++ type: std::unique_ptr<Message, std::default_delete<Message> > My function call in python looks like this: a = mymodule.MyClass() a.someFunc("some string here") # error here I

boost.python not supporting parallelism?

喜欢而已 提交于 2019-11-27 04:33:52
I am trying to wrap a piece of C++ code into python lib using boost.python, however, I found out that multiple instances cannot run at the same time: code (C++): class Foo{ public: Foo(){} void run(){ int seconds = 2; clock_t endwait; endwait = clock () + seconds * CLOCKS_PER_SEC ; while (clock() < endwait) {} } }; BOOST_PYTHON_MODULE(run_test) { using namespace boost::python; class_<Foo>("test", init<>()) .def("run", &Foo::run) ; } which is compile using CMake (CMake): add_library(run_test SHARED run_test.cpp) target_link_libraries(run_test boost_python python2.7) and tested with the

How do you pass kwargs to a boost-python wrapped function?

你说的曾经没有我的故事 提交于 2019-11-27 03:17:38
问题 I have a python function with this signature: def post_message(self, message, *args, **kwargs): I would like to call the function from c++ and pass to it some kwargs. Calling the function is not the problem. Knowing how to pass the kwargs is. Here is a non-working paraphrased sample: std::string message("aMessage"); boost::python::list arguments; arguments.append("1"); boost::python::dict options; options["source"] = "cpp"; boost::python::object python_func = get_python_func_of_wrapped_object

Calling Python functions from C++

假如想象 提交于 2019-11-27 02:37:12
问题 I am trying to achieve call Python functions from C++. I thought it could be achieved through function pointers, but it does not seem to be possible. I have been using boost.python to accomplish this. Say there is a function defined in Python: def callback(arg1, arg2): #do something return something Now I need to pass this function to C++, so that it can be called from there. How do I write the code on C++ side using boost.python to achieve this? 回答1: If it might have any name: Pass it to a

How to write a wrapper over functions and member functions that executes some code before and after the wrapped function?

青春壹個敷衍的年華 提交于 2019-11-27 02:23:07
问题 I'm trying to write some wrapper class or function that allows me to execute some code before and after the wrapped function. float foo(int x, float y) { return x * y; } BOOST_PYTHON_MODULE(test) { boost::python::def("foo", <somehow wrap "&foo">); } Ideally, the wrapper should be generic, working for functions and member functions alike, with any signature. More info: I'm looking for a simple way to release/re-acquire the GIL around my expensive C++ calls without having to manually write thin

Boost and Python 3.x

限于喜欢 提交于 2019-11-27 00:37:10
问题 How does boost.python deal with Python 3? Is it Python 2 only? 回答1: Newer versions of Boost should work fine with Python V3.x. This support has been added quite some time ago, I believe after a successful Google Summer of Code project back in 2009. The way to use Python V3 with Boost is to properly configure the build system by adding for instance: using python : 3.1 : /your_python31_root ; to your user-config.jam file. 回答2: libboostpython needs to be built with python3 in order to do this.