问题
So, I am trying to create a to_python converter that will allow me to return a boost::optional from an exposed function and have it treated as T if the optional is set and None if not. Based on a post I found on C++Sig, I wrote the following code.
template<typename T>
struct optional_ : private boost::noncopyable {
struct conversion {
static PyObject* convert(boost::optional<T> const& value) {
if (value) {
return boost::python::to_python_value<T>()(*value);
}
Py_INCREF(Py_None);
return Py_None;
}
};
explicit optional_() {
boost::python::to_python_converter<boost::optional<T>, conversion>();
}
};
As far as I can tell, it works for converting the optionals, but python throws the following exception "TypeError: No to_python (by-value) converter found for C++ type: std::string". I know that C++ is able to convert strings to python since most of my exposed functions return strings. Why doesn't boost::python::to_python_value recognize it, and how can I utilize whatever converter it has?
Fixed by changing to the following (based on this article):
template<typename T>
struct optional_ : private boost::noncopyable {
struct conversion {
static PyObject* convert(boost::optional<T> const& value) {
using namespace boost::python;
return incref((value ? object(*value) : object()).ptr());
}
};
explicit optional_() {
boost::python::to_python_converter<boost::optional<T>, conversion>();
}
};
Now to just do the other version so that it is cleaner and works better.
回答1:
Ok here is the entire to and from optional converter based on the original C++ sig post but rewritten to use the high level boost.python API (sorry about the weird spacing).
template<typename T>
struct optional_ : private boost::noncopyable
{
struct conversion :
public boost::python::converter::expected_from_python_type<T>
{
static PyObject* convert(boost::optional<T> const& value) {
using namespace boost::python;
return incref((value ? object(*value) : object()).ptr());
}
};
static void* convertible(PyObject *obj) {
using namespace boost::python;
return obj == Py_None || extract<T>(obj).check() ? obj : NULL;
}
static void constructor(PyObject *obj,
boost::python::converter::rvalue_from_python_stage1_data *data)
{
using namespace boost::python;
void *const storage =
reinterpret_cast<
converter::rvalue_from_python_storage<boost::optional<T> >*
>(data)->storage.bytes;
if(obj == Py_None) {
new (storage) boost::optional<T>();
} else {
new (storage) boost::optional<T>(extract<T>(obj));
}
data->convertible = storage;
}
explicit optional_() {
using namespace boost::python;
if(!extract<boost::optional<T> >(object()).check()) {
to_python_converter<boost::optional<T>, conversion, true>();
converter::registry::push_back(
&convertible,
&constructor,
type_id<boost::optional<T> >(),
&conversion::get_pytype
);
}
}
};
回答2:
There are a few typos in the above code - here is the corrected version:
#include <boost/noncopyable.hpp>
#include <boost/optional.hpp>
#include <boost/python.hpp>
template<typename T>
struct python_optional : private boost::noncopyable {
struct conversion : public boost::python::converter::expected_from_python_type<T>
{
static PyObject* convert(boost::optional<T> const& value)
{
using namespace boost::python;
return incref((value ? object(*value) : object()).ptr());
}
};
static void* convertible(PyObject *obj) {
using namespace boost::python;
return obj == Py_None || extract<T>(obj).check() ? obj : NULL;
}
static void constructor(
PyObject *obj,
boost::python::converter::rvalue_from_python_stage1_data *data
) {
using namespace boost::python;
void *const storage =
reinterpret_cast<
converter::rvalue_from_python_storage<boost::optional<T> >*
>(data)->storage.bytes;
if(obj == Py_None) {
new (storage) boost::optional<T>();
} else {
new (storage) boost::optional<T>(extract<T>(obj));
}
data->convertible = storage;
}
explicit python_optional() {
using namespace boost::python;
if(!extract<boost::optional<T> >(object()).check()) {
to_python_converter<boost::optional<T>, conversion, true>();
converter::registry::push_back(
&convertible,
&constructor,
type_id<boost::optional<T> >(),
&conversion::get_pytype
);
}
}
};
来源:https://stackoverflow.com/questions/6274822/boost-python-no-to-python-converter-found-for-stdstring