boost-python

Setting metaclass of wrapped class with Boost.Python

回眸只為那壹抹淺笑 提交于 2019-12-07 04:44:18
问题 I have an Event class defined in C++ that I expose to Python using Boost. My scripts are expected to derive from this class, and I'd like to do some initialization whenever a new child class is defined. How can I set the metaclass of the exposed Event class such that whenever a Python script derives from this class, the metaclass could do the required initialization? I would like to avoid having to explicitly use a metaclass in the scripts... class KeyboardEvent(Event): # This is what I want

boost::python: expose a C++ class to a python script embedded in a C++ app

喜夏-厌秋 提交于 2019-12-07 04:39:56
问题 I am successfully able to load a python script file and call a function within using boost::python in a C++ app. In the boost python EmbeddingPython wiki there is a tip on how to load a python module. namespace bp = boost::python; bp::object import(const std::string& module, const std::string& path, bp::object& globals) { bp::dict locals; locals["module_name"] = module; locals["path"] = path; bp::exec("import imp\n" "new_module = imp.load_module(module_name, open(path), path, ('py', 'U', imp

How to send signal using callback called from external script?

送分小仙女□ 提交于 2019-12-07 04:15:24
Introduction I'm trying to update QT GUI element basing on the state of calculations in embedded python script. I'm able to extract required values from python, but can't set a reference to c++ object to make it work. The details Let's assume python code is called (in calc.cpp) this way: void class_name::transfer(varA, varB, varC) { Py_Initialize(); emit inprogress(70); //HERE IT WORKS object module = import("__main__"); object name_space = module.attr("__dict__"); exec_file("MyModule.py", name_space, name_space); object MyFunc = name_space["MyFunc"]; object result = MyFunc(varA, varB, varC,

boost::python and set::erase -> weird behaviour

限于喜欢 提交于 2019-12-07 03:10:15
问题 I'm trying to store objects in a std::set. Those objects are boost::shared_ptr<>, coming from the python environment. adding values to the set won't cause any troubles. But when I try to erase a value, even though I'm passing the very same reference, it won't work. Here is an example : #include <set> #include <iostream> #include <boost/shared_ptr.hpp> #include <boost/python.hpp> using namespace std; using namespace boost; using namespace boost::python; struct Bar { Bar() {} }; struct Foo {

Pure virtual function call

半世苍凉 提交于 2019-12-07 02:54:46
问题 I'm using boost.python to make python-modules written in c++. I have some base class with pure virtual functions which I have exported like this: class Base { virtual int getPosition() = 0; }; boost::python::class_<Base>("Base") .def("GetPosition", boost::python::pure_virtual(&Base::getPosition)); in Python I have code: class Test(Base): def GetPosition(self): return 404 Test obj obj.GetPosition() RuntimeError: Pure virtual function called What's wrong? 回答1: This error happens when a

How can I specify the value of a named argument in boost.python?

情到浓时终转凉″ 提交于 2019-12-07 02:36:58
问题 i want to embed a function written in python into c++ code. My python code is:test.py def func(x=None, y=None, z=None): print x,y,z My c++ code is: module = import("test"); namespace = module.attr("__dict__"); //then i want to know how to pass value 'y' only. module.attr("func")("y=1") // is that right? 回答1: I'm not sure Boost.Python implements the ** dereference operator as claimed, but you can still use the Python C-API to execute the method you are intested on, as described here. Here is a

Using c++11 lambda as accessor function in boost::python's add_property (get_signature fails with lambda)

三世轮回 提交于 2019-12-07 01:30:55
问题 I am trying to use c++11 lambdas as accessor functions in boost::python 's add_property , something along the following (the lambda is not strictly needed in this example, but will be needed for more complicated things happening inside the lambda, such as validation): #include<boost/python.hpp> struct A{ A(): a(2){}; int a; }; BOOST_PYTHON_MODULE(boost_python_lambda) { boost::python::class_<A>("A") // .def_readonly("a",&A::a) // the classical way: works fine .add_property("a",[](const A& a)

Class-scoped enum

廉价感情. 提交于 2019-12-06 17:44:03
问题 I have a c++ class with an enum inside, and I wanted to mimick that with boost::python , so that I can write MyClass.value in python. boost::python::class_ does not have an enum_ method, and I was looking for workarounds. I first tried with lambdas like MyClass{ enum{value1,value2}; }; class_<MyClass>("MyClass").add_property("value1",&[](){return value1;}).staticmethod("value1"); which gives compiler error (in get_signature for add_property ). I know I could create getter method for each of

How to expose aligned class with boost.python

陌路散爱 提交于 2019-12-06 15:07:21
When trying to expose aligned class like this: class __declspec(align(16)) foo { public: void foo_method() {} }; BOOST_PYTHON_MODULE(foo_module) { class_<foo>("foo") .def("foo_method", &foo::foo_method); } You end up with error (msvs 2010): error C2719: 'unnamed-parameter': formal parameter with __declspec(align('16')) won't be aligned, see reference to class template instantiation 'boost::python::converter::as_to_python_function<T,ToPython>' being compiled The solution I found so far, is to use smart pointer to store object: BOOST_PYTHON_MODULE(foo_module) { class_<foo, boost::shared_ptr<foo>

Custom RTTI for use in script-defined types

笑着哭i 提交于 2019-12-06 14:04:00
问题 I'm developing a game engine in C++ that allows Python scripting. Here's an example of how functions can be defined in Python that are then called by the an event manager whenever a certain event occurs: # Import some classes defined in C++ from cpp.event import PlayerDiesEvent, EventManager def onPlayerDeath(event): pass em = EventManager() em.connect(PlayerDiesEvent, onPlayerDeath) em.post(PlayerDiesEvent(...)) # Will call `onPlayerDeath` The user can also define new types of events: from