[C++-sig] path and tuple translators

Johan Råde johan.rade at gmail.com
Wed Jan 18 17:57:40 CET 2012


On 1/18/2012 4:59 PM, Jim Bosch wrote:
> On 01/18/2012 05:59 AM, Johan Råde wrote:
>> Does Boost.Python have translators for boost::filesystem::path and
>> boost::tuple?
>> If not, are there any plans to add this functionality?
>>
>
> There are none in Boost.Python proper currently.
>
> You can find converters for Boost.Fusion in the python_extensions
> package in the Boost sandbox; along with Fusion's adapters for
> boost::tuple (see the Fusion docs), that should give you what you need:
>
> https://svn.boost.org/svn/boost/sandbox/python_extensions/
>
> You can probably just pull out the header files you want
> (to_python/boost_fusion.hpp and from_python/boost_fusion.hpp) rather
> than use the whole extensions package. The std_pair.hpp converters in
> python_extensions may also be helpful as examples, as they use the
> Fusion converters with Fusion's adapters for std::pair.
>
> I also have converters for filesystem::path (just maps to a Python str),
> attached - they should probably go in python_extensions, too, but I've
> gotten lazy about keeping that current.
>
> Jim
>
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig

I will test the tuple converters.

Concerning path, I need support for unicode paths.
Currently I manually convert from PyObject* to filesystem::path as follows:

     BOOST_STATIC_ASSERT(sizeof(wchar_t) == sizeof(Py_UNICODE));
     if(PyString_Check(obj))
         return path(PyString_AsString(obj));
     else if(PyUnicode_Check(obj))
         return path(PyUnicode_AsUnicode(obj));
     else
         ... throw some exception ....

This code is not fully portable, but suffices for my current 
application. A more robust implementation might use PyUnicode_AsWideChar 
instead of PyUnicode_AsUnicode.

--Johan



More information about the Cplusplus-sig mailing list