我正在尝试将 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。