boost-python

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

半城伤御伤魂 提交于 2019-11-29 02:41:23
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 nothing I can do, because I have to embed my Python file and control it by using C++ code (I have to

create boost-python nested namespace

岁酱吖の 提交于 2019-11-28 22:02:24
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 ? What you want is a boost::python::scope . Python has no concept of 'namespaces', but you can use a class very much like a

Boost::Python- possible to automatically convert from dict --> std::map?

陌路散爱 提交于 2019-11-28 21:24:28
问题 I've got a C++ class, with a member function that can take a small-to-large number of parameters. Lets name those parameters, a-f. All parameters have default values. As a part of the python project I am working on, I want to expose this class to python. Currently, the member function looks something like this: class myClass { public: // Constructors - set a-f to default values. void SetParameters(std::map<std::string, double> &); private: double a, b, c, d, e, f; } void myClass:

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

◇◆丶佛笑我妖孽 提交于 2019-11-28 19:40:26
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 ? 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 shared libraries. In this case you probably want to ship boost shared libraries along with your application

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

别等时光非礼了梦想. 提交于 2019-11-28 19:16:51
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 and warning about GLI but no examples. And here How to call a python function from a foreign language

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

落花浮王杯 提交于 2019-11-28 17:31:45
问题 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> >()) ;

boost::python Export Custom Exception

大城市里の小女人 提交于 2019-11-28 17:04:36
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 seems to translate all C++ exceptions (that are subclasses of std::exception ) into

Initializing Cython objects with existing C Objects

ぐ巨炮叔叔 提交于 2019-11-28 11:18:48
C++ Model Say I have the following C++ data structures I wish to expose to Python. #include <memory> #include <vector> struct mystruct { int a, b, c, d, e, f, g, h, i, j, k, l, m; }; typedef std::vector<std::shared_ptr<mystruct>> mystruct_list; Boost Python I can wrap these fairly effectively using boost::python with the following code, easily allowing me to use the existing mystruct (copying the shared_ptr) rather than recreating an existing object. #include "mystruct.h" #include <boost/python.hpp> using namespace boost::python; BOOST_PYTHON_MODULE(example) { class_<mystruct, std::shared_ptr

Make Boost Python not delete the C++ object in destructor

牧云@^-^@ 提交于 2019-11-28 10:40:09
问题 I'm creating bindings for a subset of wxWidgets using Boost Python. Window objects in wxWidgets should not be deleted manually since they handle their own deletion: for example, when a top level window is closed by the user clicking the close button it automatically deletes itself. If a window is deleted strange things will happen with event handlers etc. (Details: http://docs.wxwidgets.org/2.8/wx_windowdeletionoverview.html) This however leads to a problem with window objects created in

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

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 09:56:28
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() python_func(message, arguments, options) When I exercise this code, in pdb I get (which is not what