$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-users] [Boost.Python] Can't work out what an extractor does
From: Kirit Sælensminde (kirit.saelensminde_at_[hidden])
Date: 2008-10-20 23:58:23
I've been puzzling around about how to deal with converting my string 
type to/from Python. I've managed to get one part of the puzzle solved, 
but can't work out from the documentation what the other part is.
The code I have so far is this:
namespace {
     struct to_pystr {
         static PyObject *convert( const fostlib::string &s ) {
             std::wstring u( fostlib::coerce< std::wstring >( s ) );
             return PyUnicode_FromWideChar( u.c_str(), u.length() );
         }
         static PyTypeObject const* get_pytype() {
             return &PyUnicode_Type;
         }
     };
}
BOOST_PYTHON_MODULE( crypto ) {
     using namespace boost::python;
     // fostlib::string fostlib::sha1( const fostlib::string& )
     def( "sha1", fostlib::sha1 );
     implicitly_convertible< fostlib::native_string, fostlib::string >();
     to_python_converter< fostlib::string, to_pystr, false >();
}
This works ok if the Python type is str, but it doesn't work for unicode 
strings:
Boost.Python.ArgumentError: Python argument types in 
     crypto.sha1(unicode) 
did not match C++ signature: 
     sha1(fostlib::string)
What I guess I need is some way to wrap the Python call 
PyUnicode_FromObject in order to construct my string type, probably from 
PyObject, but certainly from unicode. From the documentation at [1] it 
isn't at all clear what actually needs to be done -- I can't even tell 
from that what the function signature I need to code to is. I think this 
[2] is relevant, but can't work out in what way -- no mention is made of 
where the reference object comes from and nor is there an example of the 
implementation of a simple extractor concept -- the internal 
implementations of the ones given on the page are far from illuminating.
[1]http://www.boost.org/doc/libs/1_36_0/libs/python/doc/v2/Extractor.html
[2]http://www.boost.org/doc/libs/1_36_0/libs/python/doc/v2/lvalue_from_pytype.html
It looks like there's some good emails linked from this page [3], but 
unfortunately the links to the Python list archives are broken.
[3]http://www.boost.org/doc/libs/1_36_0/libs/python/doc/internals.html
I did think about exposing my string class, but the __unicode__ operator 
doesn't seem to be supported. Adding it to the relevant Boost.Python 
headers doesn't appear to be enough to get it working and I don't think 
that is the right way to do this either.
So, basically, I guess there ought to be a fairly simple way to add this 
sort of type conversion into the Boost.Python type registry, but it 
appears to be undocumented and I can't see anything for this in the 
examples or the tests in the library either.
K