boost-python

What does error_already_set in Boost.python do and how to handle exceptions similarly in Python C API

北城以北 提交于 2019-12-19 19:45:24
问题 I have been working on a project where I want to remove the boost dependencies and replace it with the Python C API. I spent some time understanding the Python C API and I saw this catch (error_already_set const &) I read the boost docs but it explains where it is used. But I want to know why it is needed and how can I achieve the same functionality using the native Python C api. 回答1: Boost throws error_already_set when a Python error has occurred. So if you see code like this: try { bp::exec

error to append integer in c++ boost python list

痞子三分冷 提交于 2019-12-19 10:25:56
问题 I maked this code and it does not work #include <boost/python.hpp> namespace bp = boost::python; int main(int argc, char **argv) { bp::list points; int one = 1; int two = 2; int three = 3; points.append(one); #crach!! points.append(two); points.append(three); return 0;} which is the reason why "append" does not accept integers and directly which would be the correct way? edited the solution is this: #include <boost/python.hpp> namespace bp = boost::python; int main(int argc, char **argv) { Py

boost::python passing reference of python::list

会有一股神秘感。 提交于 2019-12-19 10:06:12
问题 I'd really like to know if there is a possibility to pass a reference of a python list to a boost::python c++ dll. What I want to achieve is that I have a list in python which can be read in c++ at any time. Let's say you'd have a variable in C++ that holds the reference to the list. Is there any way to do this? So far I only found the ctypes in python where I can make references of primitive c types, which in this case, doesn't help. I am happy for any suggestions or workarounds (a simple

Expose a non-const but noncopyable member in Boost Python

≡放荡痞女 提交于 2019-12-19 09:22:46
问题 Here's my problem: I have two classes like these: class Signal { public: void connect(...) { sig.connect(...); } private: boost::signal2::signal sig; }; class MyClass { public: Signal on_event; }; I would like to expose MyClass::on_event so that I can call my_class_instance.on_event.connect(...) from Python. This is how I've wrapped those classes: class_<Signal, boost::noncopyable> ("Signal", noinit) .def("connect", &some_helper_function); class_<MyClass> ("MyClass") .def_readonly("on_event",

Puzzling dependency of Boost.Python 1.54 (debug build) to Python27.lib on Windows

怎甘沉沦 提交于 2019-12-19 09:18:10
问题 I must be doing some kind of obvious mistake but after hours of fighting I'm unable to make further progress: After upgrading to Boost 1.54, CMake 2.8.12 and Python 2.7.5 (all three from slightly earlier minor versions), the Python bindings of my project no longer link in Debug configuration (they link fine in Release). I'm building with VS 2012. Everything was working properly before the update . I built Boost the standard way: bootstrap.bat followed by b2 address-model=64 toolset=msvc-11.0

Exposing a pointer in Boost.Python

↘锁芯ラ 提交于 2019-12-18 15:13:00
问题 I have this very simple C++ class: class Tree { public: Node *head; }; BOOST_PYTHON_MODULE(myModule) { class_<Tree>("Tree") .def_readwrite("head",&Tree::head) ; } I want to access the head variable from Python, but the message I see is: No to_python (by-value) converter found for C++ type: Node* From what I understand, this happens because Python is freaking out because it has no concept of pointers. How can I access the head variable from Python? I understand I should use encapsulation, but

Exposing C++ interface in boost python

二次信任 提交于 2019-12-18 13:16:55
问题 Sample code to illustrate: struct Base { virtual int foo() = 0; }; struct Derived : public Base { virtual int foo() { return 42; } }; Base* get_base() { return new Derived; } BOOST_PYTHON_MODULE(libTestMod) { py::class_<Base>("Base", py::no_init) .def("foo", py::pure_virtual(&Base::foo)); py::def("get_base", get_base, py::return_internal_reference<>()); //ignore mem leak } Base::foo will not be overridden in python Base:foo will be implemented in c++ but that should not be exposed to python

Exposing a C++ class instance to a python embedded interpreter

微笑、不失礼 提交于 2019-12-18 11:07:14
问题 I am looking for a simple way to expose a C++ class instance to a python embedded interpreter. I have a C++ library. This library is wrapped (using swig for the moment) and I am able to use it from the python interpreter I have a C++ main program which instanciates a Foo class from my library and embeds a python interpreter I would like to expose my C++ world instance of Foo to the python world (and seen as a Foo class). Is this possible, if so, how? I think it's almost like in the first

How to import python module from .so file?

↘锁芯ラ 提交于 2019-12-18 10:27:51
问题 [me@hostname python]$ cat hello_world.cc #include <string> #include <Python.h> #include <boost/python.hpp> namespace { std::string greet() { return "Helloworld"; } } using namespace boost::python; BOOST_PYTHON_MODULE(hello_world) { def("greet",greet); } [me@hostnmae python]$ g++ -c -fPIC hello_world.cc -I/path/to/boost/headers -I/path/to/python/headers -o hello_world.o [me@hostname python]$ g++ -shared -Wl,-soname,libhello_world.so -o libhello_world.so hello_world.o [me@hostname python]$

Build problems when adding `__str__` method to Boost Python C++ class

扶醉桌前 提交于 2019-12-18 03:55:33
问题 I have started to play around with boost python a bit and ran into a problem. I tried to expose a C++ class to python which posed no problems. But I can't seem to manage to implement the __str__ functionality for the class without getting build errors I don't understand. I'm using boost 1_42 prebuild by boostpro. I build the library using cmake and the vs2010 compiler. I have a very simple setup. The header-file (tutorial.h) looks like the following: #include <iostream> namespace