将类型对象(类,而非实例)从Python传递到C++

2 投票
1 回答
1658 浏览
提问于 2025-04-18 04:54

我想要一个用 boost::python 包装的 C++ 函数,这个函数能够接收类型(而不是实例),还有一个用 boost::python 包装的 C++ 类。我可以声明这个包装的函数接受一个 object,但我不知道怎么提取类型。我试过类似的做法,但类型对象似乎无法用 extract 提取:

#include<boost/python.hpp>
namespace py=boost::python;

struct A {};
struct B: public A {};

int func(py::object klass) {
    py::extract<std::type_info> T(klass);
    if(!T.check()) throw std::runtime_error("Unable to extract std::type_info");
    if(T()==typeid(A)) return 0;
    if(T()==typeid(B)) return 1;
    return -1;
}

BOOST_PYTHON_MODULE(deadbeef)
{
   py::def("func",func);
   py::class_<A>("A");
   py::class_<B,py::bases<A>>("B");
}

编译时使用了

clang++ -lboost_python -fPIC `pkg-config python --cflags` a.cc -std=c++11 -shared -o deadbeef.so

我运行了

PYTHONPATH=. python
>>> import deadbeef
>>> deadbeef.func(deadbeef.A)  ## I want this to return 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: Unable to extract std::type_info

谢谢任何建议。

1 个回答

4

要在Python中传递一个类型对象,需要创建一个C++类型,并注册一个自定义的转换器。因为Python类型对象本质上是Python对象,所以创建一个从 boost::python::object 继承的类型是合适的:

/// @brief boost::python::object that refers to a type.
struct type_object: 
  public boost::python::object
{
  /// @brief If the object is a type, then refer to it.  Otherwise,
  ///        refer to the instance's type.
  explicit
  type_object(boost::python::object object):
    boost::python::object(get_type(object))
  {}

private:

  /// @brief Get a type object from the given borrowed PyObject.
  static boost::python::object get_type(boost::python::object object)
  {
    return PyType_Check(object.ptr())
      ? object 
      : object.attr("__class__");
  }
};

// ... register custom converter for type_object.

不过,示例代码还存在一个额外的问题。你不能直接在Python类型对象和C++类型之间进行比较。而且,Python类型对象和C++类型之间没有直接的关联。要进行比较,必须比较Python类型对象本身。

Boost.Python使用一个内部注册表,将C++类型的身份(以 boost::python::type_info 的形式)与Python类对象关联起来。这个关联是单向的,也就是说,你只能查找Python类对象。接下来,我们扩展 type_object 类,以提供一些辅助函数,用于检查C++类型:

/// @brief boost::python::object that refers to a type.
struct type_object: 
  public boost::python::object
{
  ...

  /// @brief Type identity check.  Returns true if this is the object returned
  ///        returned from type() when passed an instance of an object created
  ///        from a C++ object with type T.
  template <typename T>
  bool is() const
  {
    // Perform an identity check that registartion for type T and type_object
    // are the same Python type object.
    return get_class_object<T>() == static_cast<void*>(ptr());
  }

  /// @brief Type identity check.  Returns true if this is the object is a
  ///        subclass of the type returned returned from type() when passed
  ///        an instance of an object created from a C++ object with type T.
  template <typename T>
  bool is_subclass() const
  {
    return PyType_IsSubtype(reinterpret_cast<PyTypeObject*>(ptr()),
                            get_class_object<T>());
  }

private:

  ...

  /// @brief Get the Python class object for C++ type T.
  template <typename T>
  static PyTypeObject* get_class_object()
  {
    namespace python = boost::python;
    // Locate registration based on the C++ type.
    const python::converter::registration* registration =
          python::converter::registry::query(python::type_id<T>());

    // If registration exists, then return the class object.  Otherwise,
    // return NULL.
    return (registration) ? registration->get_class_object()
                          : NULL;
  }
};

现在,如果 typetype_object 的一个实例,你可以检查:

  • 使用 type.is<Spam>() 来判断 type 是否是与C++的 Spam 类型相关联的Python类型。
  • 使用 type.is_subclass<Spam>() 来判断 type 是否是与C++的 Spam 类型相关联的Python类型的子类。

下面是一个完整的示例,基于原始代码,展示了如何将类型对象传递给函数,检查类型身份和子类:

#include <boost/python.hpp>

/// @brief boost::python::object that refers to a type.
struct type_object: 
  public boost::python::object
{
  /// @brief If the object is a type, then refer to it.  Otherwise,
  ///        refer to the instance's type.
  explicit
  type_object(boost::python::object object):
    boost::python::object(get_type(object))
  {}

  /// @brief Type identity check.  Returns true if this is the object returned
  ///        returned from type() when passed an instance of an object created
  ///        from a C++ object with type T.
  template <typename T>
  bool is() const
  {
    // Perform an identity check that registartion for type T and type_object
    // are the same Python type object.
    return get_class_object<T>() == static_cast<void*>(ptr());
  }

  /// @brief Type identity check.  Returns true if this is the object is a
  ///        subclass of the type returned returned from type() when passed
  ///        an instance of an object created from a C++ object with type T.
  template <typename T>
  bool is_subclass() const
  {
    return PyType_IsSubtype(reinterpret_cast<PyTypeObject*>(ptr()),
                            get_class_object<T>());
  }

private:

  /// @brief Get a type object from the given borrowed PyObject.
  static boost::python::object get_type(boost::python::object object)
  {
    return PyType_Check(object.ptr())
      ? object 
      : object.attr("__class__");
  }

  /// @brief Get the Python class object for C++ type T.
  template <typename T>
  static PyTypeObject* get_class_object()
  {
    namespace python = boost::python;
    // Locate registration based on the C++ type.
    const python::converter::registration* registration =
          python::converter::registry::query(python::type_id<T>());

    // If registration exists, then return the class object.  Otherwise,
    // return NULL.
    return (registration) ? registration->get_class_object()
                          : NULL;
  }
};

/// @brief Enable automatic conversions to type_object.
struct enable_type_object
{
  enable_type_object()
  {
    boost::python::converter::registry::push_back(
      &convertible,
      &construct,
      boost::python::type_id<type_object>());
  }

  static void* convertible(PyObject* object)
  {
    return (PyType_Check(object) || Py_TYPE(object)) ? object : NULL;
  }

  static void construct(
    PyObject* object,
    boost::python::converter::rvalue_from_python_stage1_data* data)
  {
    // Obtain a handle to the memory block that the converter has allocated
    // for the C++ type.
    namespace python = boost::python;
    typedef python::converter::rvalue_from_python_storage<type_object>
                                                                 storage_type;
    void* storage = reinterpret_cast<storage_type*>(data)->storage.bytes;

    // Construct the type object within the storage.  Object is a borrowed 
    // reference, so create a handle indicting it is borrowed for proper
    // reference counting.
    python::handle<> handle(python::borrowed(object));
    new (storage) type_object(python::object(handle));

    // Set convertible to indicate success. 
    data->convertible = storage;
  }
};

// Mockup types.
struct A {};
struct B: public A {};
struct C {};

/// Mockup function that receives an object's type.
int func(type_object type)
{
  if (type.is<A>()) return 0;
  if (type.is<B>()) return 1;
  return -1;
}

/// Mockup function that returns true if the provided object type is a
/// subclass of A.
bool isSubclassA(type_object type)
{
  return type.is_subclass<A>();
}

BOOST_PYTHON_MODULE(example)
{
  namespace python = boost::python;

  // Enable receiving type_object as arguments.
  enable_type_object();

  python::class_<A>("A");
  python::class_<B, python::bases<A> >("B");
  python::class_<C>("C");

  python::def("func", &func);
  python::def("isSubclassA", &isSubclassA);
}

交互使用:

>>> import example
>>> assert(example.func(type("test")) == -1)
>>> assert(example.func(example.A) == 0)
>>> assert(example.func(example.B) == 1)
>>> assert(example.isSubclassA(example.A))
>>> assert(example.isSubclassA(example.B))
>>> assert(not example.isSubclassA(example.C))
>>> assert(example.func("test") == -1)
>>> assert(example.func(example.A()) == 0)
>>> assert(example.func(example.B()) == 1)
>>> assert(example.isSubclassA(example.A()))
>>> assert(example.isSubclassA(example.B()))
>>> assert(not example.isSubclassA(example.C()))

撰写回答