[C++-sig] missing to_python converter for wchar_t

Roman Yakovenko roman.yakovenko at gmail.com
Thu Jan 31 20:20:11 CET 2008


2008/1/31 progetto dex <progettodex at gmail.com>:
> Hi all,
> it seems that I'm facing a well-known problem (I've found some messages
> dated 2006 or earlier), but maybe in the near past something has changed...
>
> I'm wrapping a third party API that uses "const wchar_t* const" arguments,
> and Python says that a converter is missing.
>  Before creating a layer to convert from/to std::wstring, I post a stripped
> down example. First, the C++ stuff:
>
> // C++
> #include <boost/python.hpp>
>
> using namespace boost::python;
>
> class Receiver
>  {
> public:
>     virtual void Receive(const wchar_t* const message) { }
> };
>
> class Sender
> {
>     Receiver& receiver;
>
> public:
>     Sender(Receiver& r) : receiver(r) { }
>     void Send() { receiver.Receive(L"test message"); }
>  };
>
> struct PyReceiver : Receiver
> {
>     PyObject* self;
>     PyReceiver(PyObject* s) : self(s) { }
>
>     void Receive(const wchar_t* const message)
>         { return call_method<void>(self, "Receive", message); }
>
>     void Receive_default(const wchar_t* const message)
>         { return this->Receiver::Receive(message); }
> };
>
> BOOST_PYTHON_MODULE(MyModule)
> {
>     class_<Receiver, PyReceiver, boost::noncopyable>("Receiver")
>          .def("Receive", &Receiver::Receive, &PyReceiver::Receive_default)
>     ;
>
>     class_<Sender, boost::noncopyable>("Sender", init<Receiver&>())
>         .def("Send", &Sender::Send)
>      ;
> }
>
> Now, a trivial Python test case:
>
> #python
> import MyModule
>
> class Receiver(MyModule.Receiver):
>     def Receive (self, msg):
>         print "MSG: " + msg
>
> r = Receiver()
>  s = MyModule.Sender(r)
> s.Send()
>
> If I supply a to_python wchar_t converter, only the first letter of the
> message is printed.
> I'm I missing something?

Untested: I think that replacing "const wchar_t* const" with "const
wchar_t* " should work.

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



More information about the Cplusplus-sig mailing list