Учитывая тип C++ T
, можно создать объект boost::python::type_id
, а затем запросить регистрацию в реестре Boost.Python. Если запись найдена в реестре, то можно использовать его, чтобы получить дескриптор класса Python, созданный для типа T
:
/// @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 размещения объекта класса Python в Boost.Python реестра:
#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'>
Для получения дополнительной функциональности типа, например объектов типа принятия, is
и issubclass
, см. this ответ.
В ретроспективе, даже если это было возможно, создать тип python, который наследуется как от типа исключения Python, так и от Boost.Python 'class_', по-видимому, невозможно. Поэтому я думаю, что это больше любопытства, чем практичности. – Barry