[英]From C++ buffer to python::boost::list
我正在尝试将C ++缓冲区转换为python :: boost :: list,我的C ++类是:
#include "boost/python/list.hpp"
using namespace boost::python;
class Buffer {
public:
unsigned char* m_pBuff;
int m_iWidth;
int m_iHeight;
Buffer( cont int p_iWidth, const int p_iHeight ) {
m_pBuff = new unsigned char[p_iWidth * p_iHeight];
m_iWidth = p_iWidth;
m_iHeight = p_iHeight;
}
~Buffer() { delete[] m_pBuff; }
/* Class Functions */
list getList ( void ) {
list l;
l.append(m_iWidth);
l.append(m_iHeight);
std::string data(m_iWidth * m_iHeight, ' ');
unsigned char* pBuff = m_pBuff;
for ( int i = 0; i < m_iWidth * m_iHeight; ++i, ++pBuff ) {
data[i] = (char*) *pBuff;
}
l.append(data);
return l;
}
};
python boost模块定义为:
using namespace boost::python;
BOOST_PYTHON_MODULE(BufferMethods)
{
class_<Buffer>("Buffer", init<const int, const int>())
.add_property("width", &Buffer::m_iWidth)
.add_property("height", &Buffer::m_iHeight)
/* Other functions */
.def("getList", &Buffer::getList)
;
}
但是当我在python中运行模块时,它返回此错误:
>>> from BufferMethods import *
>>> Buff = Buffer(800, 600)
>>> dataList = Buff.getList()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe0 in position 0: invalid continuation byte
>>>
我做错了什么? 我正在使用python 3.3。
当您尝试将项目附加到boost::python::list
实例中时,附加项目的python类型取决于作为参数给出的C ++对象的类型。 由于您的data
类型为std::string
,因此此附加操作正在尝试创建Python字符串。 推测:我猜想python字符串需要遵循某种布局,并且由于您只是向其提供一些随机数据,因此它无法将其解释为有效的字符串,这就是为什么会收到UnicodeDecodeError的原因。 我不知道您打算对列表执行什么操作,以及如何将缓冲区公开给Python,但以下方法似乎可行(使用std::vector<char>
作为data
类型而不是data
类型) std::string
):
#include <boost/python.hpp>
#include <boost/python/list.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <vector>
using namespace boost::python;
class Buffer {
public:
unsigned char* m_pBuff;
int m_iWidth;
int m_iHeight;
Buffer( const int p_iWidth, const int p_iHeight ) {
m_pBuff = new unsigned char[p_iWidth * p_iHeight];
m_iWidth = p_iWidth;
m_iHeight = p_iHeight;
}
~Buffer() { delete[] m_pBuff; }
/* Class Functions */
list getList ( void ) {
list l;
l.append(m_iWidth);
l.append(m_iHeight);
std::vector<char> data(m_iWidth*m_iHeight);
unsigned char* pBuff = m_pBuff;
for ( int i = 0; i < m_iWidth * m_iHeight; ++i, ++pBuff ) {
data[i] = (char) *pBuff;
}
l.append(data);
return l;
}
};
BOOST_PYTHON_MODULE(BufferMethods)
{
class_<std::vector<char> >("CharVec")
.def(vector_indexing_suite<std::vector<char> >());
class_<Buffer>("Buffer", init<const int, const int>())
.add_property("width", &Buffer::m_iWidth)
.add_property("height", &Buffer::m_iHeight)
/* Other functions */
.def("getList", &Buffer::getList)
;
}
因此在python(3.2)中:
In [1]: from BufferMethods import *
In [2]: Buff = Buffer(800,600)
In [3]: dataList = Buff.getList()
In [4]: dataList[2]
Out[4]: <BufferMethods.CharVec at 0x18172d0>
In [5]: dataList[2][2]
Out[5]: '\x00'
使用Python 2.7解决了这个问题...也许是由于我在这里使用了非官方的python.boost版本导致了错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.