Boost.Python 从类型创建句柄

Boost.Python create handle from type

在向 python 公开 C++ 代码的某些地方,我需要使用 PyObject*。如果我有一个 boost::python::class_ 对象的实例,我可以在其上调用 ptr()。但是,如果我只有类型怎么办?

基本上,给定类型列表 boost::python::bases<A, B, C>,我想将其转换为 boost::python::tuple 个实例,我可以将其传递给 PyErr_NewExceptionWithDoc() 之类的东西。这可能吗?

给定一个 C++ 类型 T,可以创建一个 boost::python::type_id 对象,然后在 Boost.Python 注册表中查询注册信息。如果在注册表中找到一个条目,则可以使用它来获取为类型 T:

创建的 Python class 的句柄
/// @brief Get the class object for a wrapped type that has been exposed
///        through Boost.Python.
template <typename T>
boost::python::object get_instance_class()
{
  // Query into the registry for type T.
  namespace python = boost::python;
  python::type_info type = python::type_id<T>();
  const python::converter::registration* registration =
    python::converter::registry::query(type);

  // If the class is not registered, return None.
  if (!registration) return python::object();

  python::handle<PyTypeObject> handle(python::borrowed(
    registration->get_class_object()));
  return python::object(handle);
}

这是一个完整的示例 demonstrating 在 Boost.Python 注册表中定位 Python class 对象:

#include <boost/python.hpp>
#include <iostream>

/// @brief Get the class object for a wrapped type that has been exposed
///        through Boost.Python.
template <typename T>
boost::python::object get_instance_class()
{
  // Query into the registry for type T.
  namespace python = boost::python;
  python::type_info type = python::type_id<T>();
  const python::converter::registration* registration =
    python::converter::registry::query(type);

  // If the class is not registered, return None.
  if (!registration) return python::object();

  python::handle<PyTypeObject> handle(python::borrowed(
    registration->get_class_object()));
  return python::object(handle);
}

struct spam {};

int main()
{
  Py_Initialize();

  namespace python = boost::python;
  try
  {
    // Create the __main__ module.
    python::object main_module = python::import("__main__");
    python::object main_namespace = main_module.attr("__dict__");

    // Create `Spam` class.
    // >>> class Spam: pass
    auto spam_class_object = python::class_<spam>("Spam", python::no_init);
    // >>> print Spam
    main_module.attr("__builtins__").attr("print")(get_instance_class<spam>());
    // >>> assert(spam is spam)
    assert(spam_class_object.ptr() == get_instance_class<spam>().ptr());
  }
  catch (python::error_already_set&)
  {
    PyErr_Print();
    return 1;
  }
}

输出:

<class 'Spam'>

有关更多类型相关的功能,例如接受类型对象、isissubclass,请参阅 this 答案。