C++ 来自C++;缓冲区到python::boost::list

C++ 来自C++;缓冲区到python::boost::list,c++,python,boost,python-3.3,C++,Python,Boost,Python 3.3,我试图把C++缓冲区转换成Python::Booo::列表,我的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 ) {

我试图把C++缓冲区转换成Python::Booo::列表,我的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;
    }
};
#包括“boost/python/list.hpp”
使用名称空间boost::python;
类缓冲区{
公众:
无符号字符*m_pBuff;
国际米乌伊维兹;
国际货币基金组织;
缓冲区(cont int p_iWidth,const int p_iWidth){
m_pBuff=新的无符号字符[p_iWidth*p_iHeight];
m_iWidth=p_iWidth;
m_iHeight=p_iHeight;
}
~Buffer(){delete[]m_pBuff;}
/*类函数*/
列表getList(void){
清单l;
l、 附加(m_iWidth);
l、 附加(m_iHeight);
std::字符串数据(m_iWidth*m_iHeight');
无符号字符*pBuff=m_pBuff;
对于(int i=0;i
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)
    ;
}
使用名称空间boost::python;
BOOST_PYTHON_模块(BufferMethods)
{
类(“缓冲区”,init())
.add_属性(“宽度”和缓冲区::m_iWidth)
.add_属性(“高度”和缓冲区::m_iHeight)
/*其他职能*/
.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
>>>
>>从BufferMethods导入*
>>>Buff=缓冲区(800600)
>>>dataList=Buff.getList()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
UnicodeDecodeError:“utf-8”编解码器无法解码位置0中的字节0xe0:无效的连续字节
>>>

我做错了什么??我使用Python 3.3。

< p>当您试图将项目追加到<代码> Boo::Python::列表< /Calp>实例时,追加项的Python类型是由作为参数的C++对象的类型确定的。由于
数据
的类型为
std::string
,因此此追加操作正在尝试创建Python字符串。推测:我猜python字符串需要遵循某种布局,因为您只是简单地向它提供一些随机数据,它无法将其解释为有效字符串,这就是为什么您会得到一个字符串。我不知道您打算对该列表做什么,以及如何将缓冲区公开给Python,但下面的方法似乎有效(使用
std::vector
作为
data
的类型,而不是
std::string
):

#包括
#包括
#包括
#包括
使用名称空间boost::python;
类缓冲区{
公众:
无符号字符*m_pBuff;
国际米乌伊维兹;
国际货币基金组织;
缓冲区(常数int p_iWidth,常数int p_iHeight){
m_pBuff=新的无符号字符[p_iWidth*p_iHeight];
m_iWidth=p_iWidth;
m_iHeight=p_iHeight;
}
~Buffer(){delete[]m_pBuff;}
/*类函数*/
列表getList(void){
清单l;
l、 附加(m_iWidth);
l、 附加(m_iHeight);
std::矢量数据(m_iWidth*m_iHight);
无符号字符*pBuff=m_pBuff;
对于(int i=0;i
因此,在python(3.2)中:

[1]中的
:从BufferMethods导入*
[2]中:Buff=缓冲区(800600)
在[3]中:dataList=Buff.getList()
在[4]中:数据列表[2]
出[4]:
在[5]:数据列表[2][2]
输出[5]:“\x00”

< /代码> 当您试图将项目追加到<代码> Boo::Python::列表< /Calp>实例时,追加项的Python类型是由作为参数的C++对象的类型确定的。由于
数据
的类型为
std::string
,因此此追加操作正在尝试创建Python字符串。推测:我猜python字符串需要遵循某种布局,因为您只是简单地向它提供一些随机数据,它无法将其解释为有效字符串,这就是为什么您会得到一个字符串。我不知道您打算对该列表做什么,以及如何将缓冲区公开给Python,但下面的方法似乎有效(使用
std::vector
作为
data
的类型,而不是
std::string
):

#包括
#包括
#包括
#包括
使用名称空间boost::python;
类缓冲区{
公众:
无符号字符*m_pBuff;
国际米乌伊维兹;
国际货币基金组织;
缓冲区(常数int p_iWidth,常数int p_iHeight){
m_pBuff=新的无符号字符[p_iWidth*p_iHeight];
m_iWidth=p_iWidth;
m_iHeight=p_iHeight;
}
~Buffer(){delete[]m_pBuff;}
/*类函数*/
列表getList(void){
清单l;
l、 附加(m_iWidth);
l、 附加(m_iHeight);
std::矢量数据(m_iWidth*m_iHight);
无符号字符*pBuff=m_pBuff;
对于(int i=0;i
因此,在python(3.2)中:

[1]中的
:从BufferMethods导入*
[2]中:Buff=缓冲区(800600)
在[3]中:dataList=Buff.getList()
在[4]中:数据列表[2]
出[4]:
在[5]:数据列表[2][2]
输出[5]:“\x00”

使用Python 2.7解决了这个问题。。。这个错误可能是因为我使用的是非官方的build python.boost from。

这个问题是使用python 2.7解决的。。。可能是因为我使用的是非官方的buildpython.b
#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)
    ;
}
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'