Boost logo

Boost Users :

From: Nat Goodspeed (ngoodspeed_at_[hidden])
Date: 2005-09-13 19:28:01


For some purposes we have a special string class derived from
std::wstring. Simplified declaration:

class cbstring: public std::wstring
{
public:
    cbstring(const wchar_t* that): std::wstring(that) {}
    cbstring(const std::wstring& that): std::wstring(that) {}
    ...
};

I want to return such objects from C++ as Python Unicode strings, e.g.:

cbstring produceWString()
{
    return L"This is a test Unicode string";
}

If produceWString() returns std::wstring, the conversion happens by
magic. But with the cbstring return type, I get:

TypeError: No to_python (by-value) converter found for C++ type: class
cbstring

The corresponding conversion problem (passing u"Foo" from Python to a
function accepting const cbstring&) can be handled with the following
call in the extension module:

    implicitly_convertible<std::wstring, cbstring>();

However,

    implicitly_convertible<cbstring, std::wstring>();

does NOT allow the produceWString() function to succeed: I still get the
aforementioned TypeError.

So I declared a type converter:

struct cbstring_to_python
{
    static PyObject* convert(const cbstring& cbs)
    {
        return PyUnicode_FromUnicode(cbs.c_str(), cbs.length());
    }
};

and registered it with:

    to_python_converter<cbstring, cbstring_to_python>();

The result is a stack overflow. I'm not clear on exactly why it happens,
but something inside the cbstring_to_python converter is apparently
recursively invoking cbstring_to_python::convert().

How *should* I go about returning our specialized string type as a
Python Unicode string?

OS: Microsoft Windows XP Pro, SP1
Compiler: Microsoft VC++ 7.1 with STLport 4.6
Boost version: 1.31


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net