我正在尝试做一些应该非常简单的事情,但是我没有太多的运气来弄清楚现有文档中的内容.
对于python 2项目,我试图将列表gettext-translated字符串作为unicode实例返回给python.gettext()的返回值是一个UTF-8编码的char*,使用PyUnicode_FromString转换为python unicode instrance应该非常简单.我有一种感觉,这是微不足道的,但我似乎无法弄清楚如何.
根据Ignacio Vazquez-Abrams和Thomas KI的评论,确实让这个单线工作; 对于这种情况,您可以绕过所有boost.python基础结构.这是一个例子:
PyObject* PyMyFunc() {
const char* txt = BaseClass::MyFunc();
return PyUnicode_FromString(txt);
}
Run Code Online (Sandbox Code Playgroud)
暴露在通常的def语句中:
class_<MyCclass>("MyClass")
.def("MyFunc", &MyClass::PyMyFunc);
Run Code Online (Sandbox Code Playgroud)
不幸的是,当您想要返回unicode实例列表时,这不起作用.这是我天真的实现:
boost::python::list PyMyFunc() {
std::vector<std::string> raw_strings = BaseClass::MyFunc();
std::vector<std::string>::const_iterator i;
boost::python::list result;
for (i=raw_strings.begin(); i!=raw_strings.end(); i++)
result.append(PyUnicode_FromString(i->c_str()));
return result;
}
Run Code Online (Sandbox Code Playgroud)
但这不编译:boost :: python :: list似乎处理PyObject值.
我正在尝试编译和链接boost-python hello world示例,我有一些链接问题.
操作系统:Ubuntu
g++ -fPIC -w Test2.cpp -I ../../../Libs/Python/Python-2.7.3/Include -I ../../../Libs/Python/Python-2.7.3 -I ../../../Libs/Boost/boost_1_52_0 -Wl,-rpath,../../../Libs/Python/Python-2.7.3/build/lib.linux-x86_64-2.7 -L -L../../../Libs/Python/Python-2.7.3/build/lib.linux-x86_64-2.7 -lssl -lcrypto -lpthread -lm -lutil -lpython2.7 -Wl,-rpath, -L../../../Libs/Boost/boost_1_52_0/lib -L../../../Libs/Boost/boost_1_52_0/stage/lib -lboost_python
Run Code Online (Sandbox Code Playgroud)
我得到以下错误
../../../Libs/Boost/boost_1_52_0/stage/lib/libboost_python.so: undefined reference to `PyUnicodeUCS2_AsWideChar'
../../../Libs/Boost/boost_1_52_0/stage/lib/libboost_python.so: undefined reference to `PyUnicodeUCS2_FromEncodedObject'
collect2: ld returned 1 exit status
make: *** [Test2] Error 1
Run Code Online (Sandbox Code Playgroud)
我有2个Makefile,一个用于python,一个用于boost.Python Makefile:
PYTHON_VERSION = 2.7.3
PYTHON_FOLDER = Python-$(PYTHON_VERSION)
INSTAL_FOLDER = $(PWD)/Python_Build
all: INSTALL_DIRECTORY $(INSTAL_FOLDER)
(cd $(PYTHON_FOLDER); ./configure --prefix=$(INSTAL_FOLDER); sudo make; sudo make install)
clean:
(cd $(PYTHON_FOLDER); sudo make clean;)
(rm …Run Code Online (Sandbox Code Playgroud) 我想使用Boost :: Python公开python中自定义智能指针包装的对象
警告
问题是我似乎无法做到这一点.这是一个示例代码:
LegacyCode :: Ptr - >传统智能指针代码
LegacyCode :: Session - >传统智能指针中包含的遗留对象
namespace boost { namespace python
{
template <class T> T* get_pointer(LegacyCode::Ptr<T> const& p)
{
return p.get();
}
template <typename T>
struct pointee<LegacyCode::Ptr<T> >
{
typedef T type;
};
}}*
BOOST_PYTHON_MODULE(pyro)
{
using namespace boost::python;
class_<LegacyCode::Session,LegacyCode::Ptr<LegacyCode::Session>>("Session")
.def("get_type",&LegacyCode::Session::getType);
}
Run Code Online (Sandbox Code Playgroud) 我们小组正在使用C++开发一个数字框架.我们现在想要将我们框架的基本部分包装在Python中.我们选择的武器是Boost.Python,因为我们已经将Boost用于其他目的.我们只使用smart_ptrs来支持多态性.以下代码段是我们如何应用策略模式的简单示例:
#include <boost/shared_ptr.hpp>
struct AbsStrategy
{
virtual std::string talk( ) = 0;
};
typedef boost::shared_ptr<AbsStrategy> StrategyPtr;
struct Foo : AbsStrategy
{
std::string talk( )
{
return "I am a Foo!";
}
};
struct Bar : AbsStrategy
{
std::string talk( )
{
return "I am a Bar!";
}
};
struct Client
{
Client( StrategyPtr strategy ) :
myStrategy( strategy )
{
}
bool checkStrategy( StrategyPtr strategy )
{
return ( strategy == myStrategy );
}
StrategyPtr myStrategy;
};
Run Code Online (Sandbox Code Playgroud)
如果我像这样用Boost.Python包装整个东西
#include <boost/python.hpp> …Run Code Online (Sandbox Code Playgroud) 我有一个用C++实现的python包,我使用setuptools进行分发.我的C++代码依赖于一些共享对象,特别是boost.python库.我应该如何分发这些共享对象?目前我要求软件包用户单独安装boost C++库,但我宁愿将所有内容捆绑在一个setuptools发行版中,以使他/她更容易.目前LD_LIBRARY_PATH,除了安装我的软件包之外,他们还必须设置boost库及其环境变量.
我正在测试Boost.Python并遇到了一些问题.
我设法完成了"Hello World"示例,没有任何故障(http://www.boost.org/doc/libs/1_54_0/libs/python/doc/tutorial/doc/html/index.html) - 所有编译和我能够在Python中正确使用.so.
但是,只要我将类引入我的测试文件(http://www.boost.org/doc/libs/1_54_0/libs/python/doc/tutorial/doc/html/python/exposing.html),编译器就会编译开始尖叫
Undefined symbols for architecture x86_64:
"boost::python::objects::function_object(boost::python::objects::py_function const&, std::__1::pair<boost::python::detail::keyword const*, boost::python::detail::keyword const*> const&)", referenced from:
boost::python::api::object boost::python::detail::make_function_aux<void (World::*)(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >), boost::python::default_call_policies, boost::mpl::vector3<void, World&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, mpl_::int_<0> >(void (World::*)(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >), boost::python::default_call_policies const&, boost::mpl::vector3<void, World&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > const&, std::__1::pair<boost::python::detail::keyword const*, boost::python::detail::keyword const*> const&, mpl_::int_<0>) in SHLibPy.o
boost::python::api::object boost::python::detail::make_function_aux<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > (World::*)(), boost::python::default_call_policies, boost::mpl::vector2<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, World&>, mpl_::int_<0> >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > (World::*)(), …Run Code Online (Sandbox Code Playgroud) 我想知道是否有任何方法可以将C++类暴露给Python,但是没有构建中间共享库.
这是我理想的情景.例如,我有以下C++类:
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;
};
Run Code Online (Sandbox Code Playgroud)
我想以某种方式将此类(或其intance)转换为PyObject*,以便将其作为paremter(args)发送到例如PyObject_CallObject:
PyObject* PyObject_CallObject(PyObject* wrapperFunction, PyObject* args)
Run Code Online (Sandbox Code Playgroud)
另一方面,在我的python方面,我将有一个wrapperFunction,它将我的C++类(或其实例)上的指针作为参数获取,并调用其方法或使用其属性:
def wrapper_function(cPlusPlusClass):
instance = cPlusPlusClass(4, 5)
result = instance.Addition()
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我并不需要/希望拥有单独的共享库或通过boost python构建模块.我需要的只是找到一种方法将C++代码转换为PyObject并将其发送到python.我无法通过C python库,boost或SWIG找到一种方法.
你有什么主意吗?谢谢你的帮助.
我想用boost :: python将这个C++类用于python代码
/* creature.h */
class Human {
private:
public:
struct emotion {
/* All emotions are percentages */
char joy;
char trust;
char fear;
char surprise;
char sadness;
char disgust;
char anger;
char anticipation;
char love;
};
};
Run Code Online (Sandbox Code Playgroud)
问题是如何在boost-python中公开这个公共属性
namespace py = boost::python;
BOOST_PYTHON_MODULE(example)
{
py::class_<Human>("Human");
// I have not idea how put the public struct here
}
Run Code Online (Sandbox Code Playgroud) 我有一个继承自boost :: statechart库的类.我需要在Python脚本中使用这个类,我想知道我是否需要为所有boost :: statechart库编写包装代码(.def s),因为我的类继承了它?或者boost.python不需要任何包装代码来查看定义(它自动处理其他boost库以在python中调用)?
我有一个使用Qt的用于UI的C++应用程序,我目前正在实现python绑定,以便应用程序更易于为其用户编写脚本.我还希望人们能够在PyQt中编写自己的自定义小部件,并通过boost :: python绑定访问核心功能.
为此,我想允许信号从PyQt小部件连接到Qt小部件,反之亦然,所以我需要以某种方式将PyQt父级化到Qt.我听说它可以做到,但经过一些搜索后找不到任何例子.
有没有人有这种东西的参考?
boost-python ×10
c++ ×8
python ×7
boost ×5
boost-thread ×1
pyqt ×1
python-2.7 ×1
qt ×1
shared-ptr ×1
swig ×1
wrapper ×1
xcode ×1