[C++-sig] bad exception conversion

Wichert Akkerman wichert at wiggy.net
Thu Jun 14 11:19:32 CEST 2012


Hi Jim,

On 06/14/2012 01:02 AM, Jim Bosch wrote:
> I suspect the problem is that your custom exception doesn't derived 
> from Python's built-in Exception base class.  Unfortunately, it's 
> impossible to do that with a Boost.Python wrapped class, but you can 
> get it all done with the Python C API:
>
> namespace {
>     PyObject *UnsuitableErrorType = NULL;
>
>     void translator(const unsuitable_error &e) {
>         std::list<const char*>::const_iterator i;
>         PyObject* unicode;
>     boost::python::list reasons;
>         for (i=e.reasons.begin(); i!=e.reasons.end(); i++) {
>             boost::python::handle<> unicode(PyUnicode_FromString(*i));
>             reasons.append(boost::python::object(unicode));
>         }

For some odd reason that append line fails with this error:

/usr/include/boost/python/converter/arg_to_python.hpp:194:7:   
instantiated from 'void 
boost::python::converter::detail::reject_raw_object_ptr(T*) [with T = 
_object]'
/usr/include/boost/python/converter/arg_to_python.hpp:217:7:   
instantiated from 
'boost::python::converter::detail::pointer_deep_arg_to_python<Ptr>::pointer_deep_arg_to_python(Ptr) 
[with Ptr = _object*]'
/usr/include/boost/python/converter/arg_to_python.hpp:256:13:   
instantiated from 
'boost::python::converter::arg_to_python<T>::arg_to_python(const T&) 
[with T = _object*]'
/usr/include/boost/python/object_core.hpp:393:69:   instantiated from 
'static PyObject* boost::python::api::object_initializer_impl<is_proxy, 
is_object_manager>::get(const T&, mpl_::false_) [with T = _object*, bool 
is_proxy = false, bool is_object_manager = false, PyObject = _object, 
mpl_::false_ = mpl_::bool_<false>]'
/usr/include/boost/python/object_core.hpp:315:7:   instantiated from 
'PyObject* boost::python::api::object_base_initializer(const T&) [with T 
= _object*, PyObject = _object]'
/usr/include/boost/python/object_core.hpp:334:49:   instantiated from 
'boost::python::api::object::object(const T&) [with T = _object*]'
../../python/article.cc:23:48:   instantiated from here
/usr/include/boost/python/converter/arg_to_python.hpp:181:11: error: 
incomplete type 
'boost::python::converter::detail::cannot_convert_raw_PyObject<_object*>' used 
in nested name specifier

which is odd some it is essentially the same code as before, only with 
the reasons list moved out of a class into the local scope.

>         boost::python::handle<> error(
>             PyObject_CallFunctionObjArgs(
>                 UnsuitableErrorType, NULL
>             )
>         );
>         PyObject_SetAttrString(error.get(), "reasons", reasons.get());

Small typo: reasons.ptr() there since reasons is an object, not a handle.

> I haven't tested any of that, but hopefully it's close enough to point 
> you in the right direction.

That's very helpful, thanks.

Wichert.


More information about the Cplusplus-sig mailing list