boostorg / python

Boost.org python module
http://boostorg.github.io/python
Boost Software License 1.0
465 stars 201 forks source link

About `No Python class registered for C++ class`,throw errors. #423

Closed shelllet closed 10 months ago

shelllet commented 10 months ago
class Strs {
std::vector<Str> m_cc;

public:
   ...
   Str [](int offset) { return m_cc[offset];}
   ...
};

class Str{};

struct StrToPython
{
  static PyObject* convert(const Str& b)
  {
      ...
  }
};

BOOST_PYTHON_MODULE(pytype_function_ext)
{
  class_<Strs >("Strs") ;

  to_python_converter< Str , StrToPython>();
}

test code

py::object list(Strs());

py::object s1 = list[0];// (1), throw "No Python class registered for C++ class Str"

py::object s2(Str()) // ok!it's call StrToPython::convert.

becuase to_python_converter does't set m_class_object value. so (1) throw errors.

how to resolve this problem?

shelllet commented 10 months ago

By default indexed elements are returned by proxy. disabled by supplying true in the NoProxy template parameter.