---------- Forwarded message ----------
From: Roman Yakovenko <rom...@gm...>
Date: Mon, Apr 7, 2008 at 4:31 PM
Subject: Re: [pygccxml-commit] string type mismatch between python and C++
To: Johnson Lu <joh...@op...>
On Mon, Apr 7, 2008 at 9:55 AM, Johnson Lu <joh...@op...> wrote:
> Hello Mr. Yakovenko:
Roman is just fine :-)
> The compiler I am using is VC 7.1, and we also use the openTop
> libraries.
>
> I am wrapping a class that is part of a large multi-platform product,
> and in our product we have some fairly convoluted header includes that,
> I think, make strings into different types base on the platform being
> compiled on. On VC7.1 simple string became unsigned shorts (heaven only
> knows).
>
> Here is a simple snippet of that function as generated by PY++:
>
> struct ClassificationOutput_wrapper : ClassificationOutput, bp::wrapper<
> ClassificationOutput > {
> .
> .
> .
> virtual void setApplicationName( ::ot::String const &
> applicationName ) {
> if( bp::override func_setApplicationName = this->get_override(
> "setApplicationName" ) )
> func_setApplicationName( applicationName );
> else
> this->ClassificationOutput::setApplicationName(
> applicationName );
> }
> void default_setApplicationName( ::ot::String const &
> applicationName ) {
> ClassificationOutput::setApplicationName( applicationName );
> }
> .
> .
> .
> }
> BOOST_PYTHON_MODULE(pyplusplus){
> bp::class_< ClassificationOutput_wrapper, boost::noncopyable >(
> "ClassificationOutput" )
> .def( bp::init< >() )
> .
> .
> .
> .def(
> "setApplicationName"
> , &::ClassificationOutput::setApplicationName
> , &ClassificationOutput_wrapper::default_setApplicationName
> , ( bp::arg("applicationName") ) )
>
>
> And here is the original .h snippet for the function...
>
> class ClassificationBuiltinImplDeclSpec ClassificationOutput : public
> IClassificationModifiableOutput
> {
> .
> .
> .
> virtual void setApplicationName(const String& applicationName) ;
>
> And my test code simply creates a C++ ClassificationOutput object:
>
> ClassificationOutput mm;
> mm.setApplicationName(OPTIER_T("monty python"));
> mm.setUserName(OPTIER_T("jclu"));
> .
> .
> .
> bpl::object pyCls(boost::ref(mm));
> main_namespace["output"] = pyCls;
>
> Pass it to Python via name space addition to the python dictionary, then
> used a simple python code:
> output.setApplicationName("anApplication") # and this line does
> not work.
> output.getUserName() # returns jclu, so python can read from
> C++
>
> All seem very straight forward, except not quite so, for me.
>
> Again, really appreciate your help.
Okey, I think I understand the problem and was able to reproduce it.
Solution:
1. http://boost.org/doc/libs/1_35_0/libs/python/doc/v2/faq.html#custom_string
2. http://language-binding.net/pyplusplus/documentation/inserting_code.html#insert-code-to-module
Explanation: I guess Boost.Python was compiled with "Yes
(/Zc:wchar_t)" option turned on. Thus class std::basic_string<
unsigned short > and std::basic_string< wchar_t > are 2 different
classes. So you will have to define your own string conversion or
recompile all your source code with the option.
HTH
--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
|