path and tuple translators

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

path and tuple translators

Johan Råde-2
Does Boost.Python have translators for boost::filesystem::path and
boost::tuple?
If not, are there any plans to add this functionality?

Thanks,
Johan Råde

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: path and tuple translators

Jim Bosch-2
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
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig

filesystem.hpp (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: path and tuple translators

Johan Råde-2
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
> [hidden email]
> 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

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: path and tuple translators

Johan Råde-2
In reply to this post by Jim Bosch-2

> 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/
>
 > ...
 >
> Jim

Hi Jim,

I have tested your code. I used it to convert from Python sequence to
std::vector and to boost::tuple. Everything works fine. Your code would
be a valuable addition to the Boost.Python library.

A few comments:

1. Many application developers use boost::tuple or std::tuple but have
never heard of Boost.Fusion. For their benefit you might add a header
that handles boost::tuple and std::tuple while hiding the Boost.Fusion
stuff.
Something like the following should suffice

     // boost/python/from_python/tuple.hpp

     #ifndef BOOST_PYTHON_FROM_PYTHON_TUPLE_HPP
     #define BOOST_PYTHON_FROM_PYTHON_TUPLE_HPP

     #include <boost/fusion/include/tuple.hpp>
     #include <boost/fusion/include/boost_tuple.hpp>
     #include <boost/python/from_python/boost_fusion.hpp>

     namespace boost { namespace python {

         template <typename Sequence>
         struct tuple_from_python
       : boost_fusion_from_python<Sequence> {};

     }}

     #endif


2. If you change boost/python/from_python/container.hpp line 46 from

      } catch (error_already_set & err) {

to

      } catch (error_already_set &) {

then you get rid of a compiler warning (on MSVS 2010).


3. I feel the naming of some of the headers and classes is a bit
inconsistent. Why

     boost::python::tuple_from_python

and

     boost::python::container_from_python_sequence

Both convert from Python sequences.


4. I also tested with std::tuple (on MSVS 2010) and that did not work.
I got the error message

1>C:\Libraries\Boost\boost_1_48_0\boost/mpl/for_each.hpp(95): error
C2664: 'boost::mpl::assertion_failed' : cannot convert parameter 1 from
'boost::mpl::failed ************boost::mpl::is_sequence<T>::*
***********' to 'boost::mpl::assert<false>::type'
1>          with
1>          [
1>              T=std::tr1::tuple<std::string,double,bool>
1>          ]
1>          No constructor could take the source type, or constructor
overload resolution was ambiguous

--Johan




_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig