[C++-sig] [boost.python] conversion between string and unsigned char *

Roman Yakovenko roman.yakovenko at gmail.com
Thu Aug 3 13:20:12 CEST 2006


On 8/2/06, Jan Langer <jan at langernetz.de> wrote:
> Hello,
> I have a library I want to expose to python by using boost.python and
> pyplusplus. This library works completely with unsigned char * for
> strings. My current approach is
>
> // library
>
> typedef unsigned char * StringT;
> StringT F_ApiGetString (F_ObjHandleT docId, F_ObjHandleT objId, IntT
> propNum);
>
> // boost.python code
>
> struct StringT_converter
> {
>   static PyObject *convert (StringT s)
>   {
>     return bp::incref (bp::str (reinterpret_cast <char *> (s)).ptr ());
>   }
> };
>
> BOOST_PYTHON_MODULE(pyfdk){
>     bp::to_python_converter <StringT, StringT_converter> ();

I think this line does not do what you expect. You are trying to
register custom
converter for C++ fundamental type. I could be wrong, but it seems to
me that boost.python
uses different approach ( type conversion ) when it comes to
fundamental C++ types.


If I right, than pyplusplus can help you.
1. For every function, that uses StringT generate small wrapper,
replacing StringT with
   std::string or custom string class(
http://boost.org/libs/python/doc/v2/faq.html#custom_string )

2. Export those wrappers. Consider to run pyplusplus on generated
files, instead of
   adding code to the module builder.

> I hope someone can help me.

Does it help?

> Thanks,
> Jan


-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list