简体   繁体   English

使用pybind11的UnicodeDecodeError

[英]UnicodeDecodeError with pybind11

I am trying to wrap a class which returns a string. 我试图包装一个返回字符串的类。

class SS {
  public:
    SS(const std::string& s) : data_(s.data()), size_(s.size()) {}

    // Return a pointer to the beginning of the referenced data
    const char* data() const { return data_; }

    const char* data_;
    size_t size_;
};

class PySS: public SS {
  public:
    PySS(const std::string &str): SS(str) {
      std::cout << "cons " << str << std::endl; #key1
      std::cout << "data " << SS::data() << std::endl; # key1

    }

    std::string data() {
      std::cout << "call data " << SS::data() << std::endl; # p��
      return std::string(SS::data());
    }
};

void init_slice(py::module & m) {
  py::class_<PySS>(m, "SS")
    .def(py::init<const std::string&>())
    .def("data", &PySS::data);
}

When calling from python, 从python调用时,

s = SS('key1')
print (s.data())

it throws unicode error 它会引发unicode错误

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xae in position 1: invalid start byte

I print the string in the constructor, and it shows the identical result. 我在构造函数中打印字符串,它显示相同的结果。 But in the other function it shows some uninterpreted string. 但在另一个函数中,它显示了一些未解释的字符串。

any idea? 任何想法?

[Edit] [编辑]

Here is the minimal example to reproduce the similar issue. 以下是重现类似问题的最小示例。

class SS {
  public:
    SS(const std::string& s) : data_(s.data()) {}

    // Return a pointer to the beginning of the referenced data
    const char* data() const { return data_; }
    const std::string ToString() const {
      std::cout << std::string(data_) << std::endl;
      return std::string(data_);
    }

    const char* data_;
};

void init_slice(py::module & m) {
  py::class_<SS>(m, "Slice")
  .def(py::init<const std::string&>())
  .def("data", &SS::ToString);
}

Problem & solution 问题方案

There are several problems with your example, the most important one is that your pointers are invalid, because they point to something that goes out-of-scope (your s argument of class SS ). 你的例子有几个问题,最重要的是你的指针无效,因为它们指的是超出范围的东西(你s class SS s参数)。

The solution is to copy s to a member variable of class SS as follows: 解决方案是将s复制到class SS的成员变量,如下所示:

#include <string>
#include <iostream>
#include <pybind11/pybind11.h>

namespace py = pybind11;

class SS {
  public:
    SS(const std::string& s) : m_data(s) {}

    const char* data() const { return m_data.data(); }

    std::string m_data;
};

class PySS: public SS {
  public:
    PySS(const std::string& s): SS(s) {}

    std::string get() { return std::string(SS::data()); }
};

PYBIND11_MODULE(example, m)
{
  py::class_<PySS>(m, "SS")
    .def(py::init<const std::string&>())
    .def("get", &PySS::get);
}

Two more remarks: 还有两个评论:

  • In your example the macro PYBIND11_MODULE was missing, which takes care of some general things to be able to import your module (see this example ). 在您的示例中,缺少宏PYBIND11_MODULE ,它会处理一些能够导入模块的常规事项(请参阅此示例 )。
  • I would never declare the same function with two different meanings: your SS::data() returns a pointer, while PySS::data() returns a copy (a std::string ). 我永远不会用两个不同的含义声明相同的函数:你的SS::data()返回一个指针,而PySS::data()返回一个副本(一个std::string )。 I thus renamed the latter to PySS::get() to make the distinction clear. 因此我将后者重命名为PySS::get()以区分清楚。

Work-around for third-party classes 解决第三方课程

Given that you class SS it outside of you control, I think that you can only work around the problem by wrapping it. 鉴于你在你控制之外对class SS ,我认为你只能通过包装解决问题。 For example: 例如:

#include <string>
#include <iostream>
#include <pybind11/pybind11.h>

namespace py = pybind11;

class SS {
  public:
    SS() = default;
    SS(const std::string& s) : data_(s.data()), size_(s.size()) {}
    const char* data() const { return data_; }

  private:
    const char* data_;
    size_t size_;
};

class PySS {
  public:
    PySS(const std::string& s) { m_data = s; m_SS = SS(m_data); }
    std::string get() { return std::string(m_SS.data()); }

  private:
    std::string m_data;
    SS m_SS;
};

PYBIND11_MODULE(example, m)
{
  py::class_<PySS>(m, "SS")
    .def(py::init<const std::string&>())
    .def("get", &PySS::get);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM