[C++-sig] converters

Jacques A. Vidrine nectar at celabo.org
Mon Jun 9 19:00:25 CEST 2003


Hello, All!

I'm having some difficulty due to the fact that conversions between
the Python string type and std::string have NUL-terminated string
semantics rather than the expected `opaque byte sequence' semantics.

For my immediate needs, I thought I could simply substitute
vector<char> and my own converters instead.  (Indeed, the API I'm
wrapping uses vector<char> rather than std::string.)  I found that
this was quite easy to do for returning values to Python:

   struct voc_to_str {
        static PyObject *convert(const vector<char> v) {
                return PyString_FromStringAndSize(&v[0], v.size());
        }
   };
   
   to_python_converter<vector<char>, voc_to_str>();

but I don't quite see how one arranges for the opposite transformation
(vector<char> -> python::str).


Naturally, I also wonder whether it is possible to override the
`default' builtin converters for std::string so that they behave more
Python- and C++- like.

Any pointers would be much appreciated!

Cheers,
-- 
Jacques Vidrine   . NTT/Verio SME      . FreeBSD UNIX       . Heimdal
nectar at celabo.org . jvidrine at verio.net . nectar at freebsd.org . nectar at kth.se




More information about the Cplusplus-sig mailing list