boost-python

Calling Python functions from C++

♀尐吖头ヾ 提交于 2019-11-28 09:04:36
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? Matthew Scouten If it might have any name: Pass it to a function that takes a boost::python::object . bp::object pycb; //global variable. could also

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

懵懂的女人 提交于 2019-11-28 08:42:46
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 wrappers like this: float foo_wrapper(int x, float y) { Py_BEGIN_ALLOW_THREADS int result = foo(x, y);

How to expose a C++ class to Python without building a module

北战南征 提交于 2019-11-28 08:12:24
问题 I want to know if there is any way to expose a C++ class to Python but without building an intermediate shared library. Here is my desirable scenario. For example I have following C++ class: class toto { public: toto(int iValue1_, int iValue2_): iValue1(iValue1_), iValue2(iValue2_) {} int Addition(void) const {if (!this) return 0; return iValue1 + iValue2;} private: int iValue1; int iValue2; }; I would like to convert somehow this class (or its intance) to a PyObject* in order to send it as

Boost.Python and Boost.Signals2: Segmentation faults

◇◆丶佛笑我妖孽 提交于 2019-11-28 06:08:32
问题 I have a problem with integrating boost.signals2 in my existing C++ lib which I have exposed with boost.python. I have a class that is exposed to python with a std::shared_ptr . This class should be able to raise some signals on certain events. Therefore I have exposed a connect_slot function which takes a boost::python::object as argument. If I raise a signal directly after connecting a slot, everything works fine, but if the class raises the signals lateron, I receive segmentation faults. I

Boost.Python: How to expose std::unique_ptr

假装没事ソ 提交于 2019-11-28 05:23:36
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 tried to expose the std::unique_ptr but just cant get it to work.. Does someone know how to properly

Boost and Python 3.x

杀马特。学长 韩版系。学妹 提交于 2019-11-28 04:43:53
How does boost.python deal with Python 3? Is it Python 2 only? 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. libboostpython needs to be built with python3 in order to do this. This doesn't work with boost 1.58 (which comes with Ubuntu 16.04), so make sure you download the latest boost

Python method to boost function

与世无争的帅哥 提交于 2019-11-28 04:21:40
问题 I have a method exported to Python using boost python that takes a boost::function as an argument. From what I have read boost::python should support boost::function without much fuss, but when I try to call the function with a python method it gives me this error Boost.Python.ArgumentError: Python argument types in Class.createTimer(Class, int, method, bool) did not match C++ signature: createTimer(class Class {lvalue}, unsigned long interval, class boost::function<bool _cdecl(void)>

How to get Python exception text

蓝咒 提交于 2019-11-28 04:19:25
I want to embed python in my C++ application. I'm using Boost library - great tool. But i have one problem. If python function throws an exception, i want to catch it and print error in my application or get some detailed information like line number in python script that caused error. How can i do it? I can't find any functions to get detailed exception information in Python API or Boost. try { module=import("MyModule"); //this line will throw excetion if MyModule contains an error } catch ( error_already_set const & ) { //Here i can said that i have error, but i cant determine what caused an

Runtime error R6034 in embedded Python application

风格不统一 提交于 2019-11-28 03:20:24
I am working on an application which uses Boost.Python to embed the Python interpreter. This is used to run user-generated "scripts" which interact with the main program. Unfortunately, one user is reporting runtime error R6034 when he tries to run a script. The main program starts up fine, but I think the problem may be occurring when python27.dll is loaded. I am using Visual Studio 2005, Python 2.7, and Boost.Python 1.46.1. The problem occurs only on one user's machine. I've dealt with manifest issues before, and managed to resolve them, but in this case I'm at a bit of a loss. Has anyone

Boost python wrapping a virtual method

南楼画角 提交于 2019-11-28 02:17:21
问题 I'm using boost python to create a binding to a c++ library. A number of classes in this library have virtual methods which accept iterator/const_iterator types as arguments. I don't particularly want to expose these types but would prefer to create some wrappers around these virtual methods that accept the appropriate container instead. My question is, is it safe to do this wrapping in the 'default implementation' function ? e.g. class Test { public: Test(); virtual ~Test(); virtual void