c++ - 从 C++ 缓冲区到 python::boost::list

标签 c++ python boost python-3.3

我正在尝试将 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++ 对象的类型确定。由于您的 datastd::string 类型,因此此追加操作试图创建一个 Python 字符串。推测:我猜想 python 字符串需要遵循某种布局,并且由于您只是向它提供一些随机数据,它无法将其解释为有效字符串,这就是您得到 UnicodeDecodeError 的原因。我不知道你到底打算用这个列表做什么,以及你想如何将你的缓冲区暴露给 Python,但以下似乎有效(使用 std::vector<char> 作为 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'

关于c++ - 从 C++ 缓冲区到 python::boost::list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13682665/

相关文章:

c++ - 调试涉及使用 : vector, 字符串、STL 的 C++ 代码

python - 使用 Python 2.7 解析 msg/eml 文件

元组的 Python 数组首先分组,然后存储

c++ - 读写超时

C++ Blocking Queue Segfault w/Boost

c++ - 编译 boost C++11 clang mac 找不到 cstddef

C++ 拥有 myMethod(Thing& a) 或 myMethod(Thing a) 之间的确切区别?

c++ - 顶点着色器的定点算法

c++ - 文件 basic_socket.hpp 中的 lib boost asio 1.47.0 出错

python - 在python中的一个句子中使用两个或多个关系运算符