给定使用Boost.Python公开的C++类,如何公开两个构造函数:
我在C++代码中遵循常量
enum
{
BOUNDARY_NONE = -1,
};
Run Code Online (Sandbox Code Playgroud)
我想用Boost.Python将它暴露给Python,这样它就可以在Python中使用了BOUNDARY_NONE.我不想在任何附加范围内定义它.
我发现如何使用Boost.Python v1执行此操作
#include <boost/python/reference.hpp>
my_module_builder.add(make_ref(BOUNDARY_NONE), "BOUNDARY_NONE");
Run Code Online (Sandbox Code Playgroud)
但我使用较新版本的Python,我不知道如何使用它来访问my_module_builder(我改用BOOST_PYTHON_MODULE).
这里出现了类似的问题,但它们正在为类范围添加常量,并且我没有任何类范围可供使用.
boost::python 导出自定义异常的公认答案显示了如何从 C++ 导出自定义异常类,而Boost.Python 自定义异常类显示了如何导出从 Python 的异常继承的异常类。我怎样才能做到这两点?即公开一个异常类,该类具有用于检索信息的自定义方法,并且该类也可以从 Python 的异常派生。
我有一个C++类,我用Boost Python包装.
其中一个类方法有两个cv::Mat如下:
MyClass::do_something(cv::Mat input, cv::Mat output)
Run Code Online (Sandbox Code Playgroud)
我用python提供的功能包括上面的方法,构造函数和一些打印方法.
初始化和打印方法(用于调试)在C++和Python包装器中都能很好地工作:
obj = MyClass(arg1, arg2, arg3)
obj.print_things()
Run Code Online (Sandbox Code Playgroud)
这些调用成功完成.
我遇到了do_something()调用问题(在Python绑定中,它在C++中成功完成):
from libmyclass import *
import cv
rgb = cv.CreateMat(256,256,cv.CV_8UC3)
result = cv.CreateMat(256,256,cv.CV_8UC3)
#...fill "rgb"
obj.do_something(rgb,result)
Run Code Online (Sandbox Code Playgroud)
我执行上面的python代码时得到的错误是:
Boost.Python.ArgumentError: Python argument types in
MyClass.do_something(MyClass, cv2.cv.cvmat, cv2.cv.cvmat)
did not match C++ signature:
do_something(MyClass {lvalue}, cv::Mat, cv::Mat)
Run Code Online (Sandbox Code Playgroud)
这是cv2.cv.Mat和cv :: Mat之间的差异吗?我有OpenCV 2.3.1和2.4,都有Boost Python绑定.
如果它是相关的,这是我的Boost包装器的样子:
#include <boost/python.hpp>
#include "MyClass.h"
#include <cv.h>
using namespace boost::python;
BOOST_PYTHON_MODULE(libmyclass) {
class_<MyClass>("MyClass", init<std::string, std::string, std::string>())
.def("print_things", &MyClass::print_things)
.def("do_something", &MyClass::do_something)
; …Run Code Online (Sandbox Code Playgroud) 我真的很想知道是否有可能将python列表的引用传递给boost :: python c ++ dll.我想要实现的是我在python中有一个列表,可以随时用c ++读取.假设你在C++中有一个变量来保存对列表的引用.
有没有办法做到这一点?到目前为止,我只在python中找到了ctypes,我可以在其中引用原始c类型,在这种情况下,它没有帮助.
我很高兴任何建议或解决方法(一个简单的例子会很棒)
问候克里斯
考虑以下简单的python扩展.何时start()-ed,Foo只需将下一个连续整数添加到a py::list,每秒一次:
#include <boost/python.hpp>
#include <thread>
#include <atomic>
namespace py = boost::python;
struct Foo {
Foo() : running(false) { }
~Foo() { stop(); }
void start() {
running = true;
thread = std::thread([this]{
while(running) {
std::cout << py::len(messages) << std::end;
messages.append(py::len(messages));
std::this_thread::sleep_for(std::chrono::seconds(1));
}
});
}
void stop() {
if (running) {
running = false;
thread.join();
}
}
std::thread thread;
py::list messages;
std::atomic<bool> running;
};
BOOST_PYTHON_MODULE(Foo)
{
PyEval_InitThreads();
py::class_<Foo, boost::noncopyable>("Foo",
py::init<>())
.def("start", &Foo::start)
.def("stop", &Foo::stop)
; …Run Code Online (Sandbox Code Playgroud) 我有一个类,Foo其中有一个我想要公开的成员x,但通过 getter 函数而不是属性。我刚刚发现make_getter,所以我想我应该尝试一下:
#include <boost/python.hpp>
namespace py = boost::python;
struct Base {
int x;
};
struct Foo : Base {
Foo(int i): Base{i} { }
};
BOOST_PYTHON_MODULE(Foo)
{
py::class_<Foo>("Foo", py::init<int>())
.def_readonly("x", &Foo::x)
.def("getX", py::make_getter(&Foo::x))
;
}
Run Code Online (Sandbox Code Playgroud)
然而,这失败了:
>>> import Foo
>>> f = Foo.Foo(42)
>>> f.x
42
>>> f.getX()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
Foo.getX(Foo)
did not match C++ signature:
getX(Base {lvalue})
>>> …Run Code Online (Sandbox Code Playgroud) 我有一个 C++ 类想要暴露给 Python。(假设这个类已经写好了并且不能轻易修改)。在这个类中,有一个成员是一个指针,我也想公开该成员。这是代码的最小版本。
struct C {
C(const char* _a) { a = new std::string(_a); }
~C() { delete a; }
std::string *a;
};
BOOST_PYTHON_MODULE(text_detection)
{
class_<C>("C", init<const char*>())
.def_readonly("a", &C::a);
}
Run Code Online (Sandbox Code Playgroud)
它编译正常,只是当我尝试访问该字段时出现 Python 运行时错误:
>>> c = C("hello")
>>> c.a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: No to_python (by-value) converter found for C++ type: std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*
Run Code Online (Sandbox Code Playgroud)
这是可以理解的。但问题是,是否有可能a通过 Boost Python 公开成员指针?如何?
我下载了最新版本的Boost,我试图在Ubuntu 10.04上启动并运行Boost.python教程:http://www.boost.org/doc/libs/1_43_0/libs/python/doc/tutorial/ DOC/HTML /蟒蛇/ hello.html的
我导航到正确的目录,运行"bjam"并使用默认设置进行编译.我还没有创建一个bjam配置文件.编译似乎有效,但现在我不知道如何在我的python脚本中包含这些文件.当我尝试运行python hello world脚本时,它给了我这个错误:
Traceback (most recent call last):
File "./hello.py", line 6, in <module>
import hello_ext
ImportError: libboost_python.so.1.43.0: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)
有谁知道发生了什么?
我自己编译了Boost并用它将以下函数导出到DLL:
#include <boost/python.hpp>
using namespace boost::python;
std::string greet()
{
return "hello, dude !!";
}
BOOST_PYTHON_MODULE(hello)
{
def("greet", greet);
}
Run Code Online (Sandbox Code Playgroud)
在我将hello.dll文件重命名为hello.pyd后,这在Python中加载正常.
现在我正在尝试这个:
#include <boost/python.hpp>
using namespace boost::python;
struct World
{
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};
BOOST_PYTHON_MODULE(hello)
{
class_<World>("World")
.def("greet", &World::greet)
.def("set", &World::set);
}
Run Code Online (Sandbox Code Playgroud)
它出错了:
Error 29 error LNK2019: unresolved external symbol "__declspec(dllimport) void * __cdecl boost::python::objects::find_static_type(void *,struct boost::python::type_info,struct boost::python::type_info)" (__imp_?find_static_type@objects@python@boost@@YAPAXPAXUtype_info@23@1@Z) referenced in function "private: virtual void * __thiscall boost::python::objects::value_holder<struct …Run Code Online (Sandbox Code Playgroud)