boost-python

boost python return same instance with make_constructor

ⅰ亾dé卋堺 提交于 2019-12-25 05:22:18
问题 I'm trying to make so this code returns the same instance in both the init function and the callback function test1.py import test1 c = test1.C() print 'init:', c def func(c): print 'func:', c test1.register_callback(func) test1.cpp #include <iostream> #include <vector> #include <boost/python.hpp> using namespace boost::python; class C; std::vector<boost::shared_ptr<C>> v; class C : public boost::noncopyable { public: C() { std::cout << "C()" << std::endl; } ~C() { std::cout << "~C()" << std:

boost python return same instance with make_constructor

偶尔善良 提交于 2019-12-25 05:22:02
问题 I'm trying to make so this code returns the same instance in both the init function and the callback function test1.py import test1 c = test1.C() print 'init:', c def func(c): print 'func:', c test1.register_callback(func) test1.cpp #include <iostream> #include <vector> #include <boost/python.hpp> using namespace boost::python; class C; std::vector<boost::shared_ptr<C>> v; class C : public boost::noncopyable { public: C() { std::cout << "C()" << std::endl; } ~C() { std::cout << "~C()" << std:

Crash on call from boost::python::exec( anything )

你。 提交于 2019-12-25 01:44:57
问题 I'm trying to implement some Python stuff into my program and I've decided to use Boost::Python, so I compiled it according to the instructions, with bjam, using mingw/gcc, getting dlls and .a files I'm using Code::Blocks for this, so I've put the dlls in the working directory of my project, where the rest of dlls I use are, and decided to run boost::python::exec("b = 5"); Instantly I get a crash. Ideas? #include <boost/python.hpp> float func(int a) { return a*a-0.5; } BOOST_PYTHON_MODULE

The argument of char* converted to python to call a python function in C++ by boost.python

╄→гoц情女王★ 提交于 2019-12-24 13:27:37
问题 I call a python function in c++ by boost.python. And pass a argument of char* to the python function.But there was a error. TypeError: NO to_python (by-value) converter found for c++ type: char. The following is code: C++ #include <boost/python.hpp> #include <boost/module.hpp> #include <boost/def.hpp> using namespace boost::python; void foo(object obj) { char *aa="1234abcd"; obj(aa); } BOOST_PYTHON_MODULE(ctopy) { def("foo",foo); } python import ctopy def test(data) print data t1=ctopy.foo

how to access derived class object from abstract class in boost python

血红的双手。 提交于 2019-12-24 12:40:18
问题 How do I access derived class in python from abstract class(interface class) without exposing derived class, by only exposing abstract class. I do not want to expose derived class for python. Is there any way that I can access derived class through abstract class? The example code is: Base.h class Base { public: virtual void Set(const std::vector<std::string>& AllParameters) = 0; }; struct BaseWrap : Base, wrapper<Base> { void Set(const std::vector<std::string>& AllParameters) { this->get

Parameter passing from Python script to C++ with boost-python

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 10:08:26
问题 I am currently embedding Python in C++ using boost-python and boost-numpy. I have the following Python test script: import numpy as np import time def test_qr(m,n): print("create numpy array") A = np.random.rand(m, n) print("Matrix A is {}".format(A)) print("Lets QR factorize this thing! Mathematics is great !!") ts = time.time() Q, R = np.linalg.qr(A) te = time.time() print("It took {} seconds to factorize A".format(te - ts)) print("The Q matrix is {}".format(Q)) print("The R matrix is {}"

Why does static version of the Boost Python library has a dependency to Python in Windows?

一世执手 提交于 2019-12-24 07:35:42
问题 I'm developing a Python binding for a C++ library using Boost Python. To avoid having issues distributing the Boost libraries, I'm using the static versions. It works fine in Linux and MacOSX, but in Windows, a dependency to Python is still required. My CMakeLists.txt is set as: if((${CMAKE_SYSTEM_NAME} STREQUAL "Linux") OR APPLE) target_link_libraries(my_python_module ${Boost_LIBRARIES}) elseif(WIN32 AND MSVC) add_definitions(/DBOOST_PYTHON_STATIC_LIB) # This is also required to force the

boost python - nullptr while extracting ndarray

夙愿已清 提交于 2019-12-24 07:07:22
问题 I have a C++ code which execute python script with boost_python package. Everything is fine, as longa as I extract int, string, or other not-array variables from python. However I have to extract a numpy::ndarray and convert it to cpp vector . I tried as follow: main.cpp #include <iostream> #include <boost/python.hpp> #include <boost/python/numpy.hpp> using namespace boost::python; int main() double t_end=7 try { Py_Initialize(); object module = import("__main__"); object name_space = module

How to automatically infer the return type from a function type?

谁都会走 提交于 2019-12-23 20:55:00
问题 I'm using boost::python to create a Python wrapper of a C++ library. At some point, boost::python needs a pointer to a member function (or something compatible), like: template <class MyClass, typename ValueType> void (*setter_function)(MyClass&, ValueType) // This doesn't compile, but you got the idea. my_boost_python_call(setter_function f); Since the class I'm wrapping has its setter in the following form: template <class MyClass, typename ValueType> MyClass& (MyClass::*setter_method)

How to call Python from a boost thread?

蓝咒 提交于 2019-12-23 19:55:17
问题 I have a Python app that calls a C++ boost python library and it all works. However, I have a callback C++ to Python scenario where C++ from a boost thread calls python and I get an access violation on the C++ side. If I do exactly the same callback using the python thread it works perfectly. Therefore I suspect that I can not simply callback Python from C++ using a boost thread but need to do something extra for it to work? 回答1: The most likely culprit is that the Global Interpreter Lock