Re: what happened with this std::string parameter inboost.python
Another problem(maybe) I found while boost.python deal with std::string: // my function void write1( std::string s ) { // no nothing } // my class to deal with python error output class Cout { public: std::string sbuf; public: void write( std::string s ) { sbuf += s; if ( sbuf.find_first_of( "\n" ) == std::string::npos ) return; printf( "Cout::write\t%s", sbuf.c_str() ); sbuf = ""; } }; I wrapped it by boost.python in my c++ program embedding boost.python, BOOST_PYTHON_MODULE(mym) { def( "write", &write1 ); class_<Cout>( "Cout", init<std::string>() ) .def_readwrite( "sbuf", &Cout::sbuf ) .def( "write", &Cout::write ) ; } and then I call it as following: PyRun_SimpleString( "import mym\n" "import sys\n" "sys.stderr = mym.Cout('')\n" "mym.write('aa','bb')\n" ); it crashed, I can't caught any exception, even I insert the contents of libs/python/test/module_tail.cpp into my program. Donnie Leen
Donnie Leen wrote:
Another problem(maybe) I found while boost.python deal with std::string:
Please post a minimal but *complete* example (that means your program should have a main()) and show the exact command lines used to build your program and to invoke it. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
"David Abrahams" <dave@boost-consulting.com> wrote in message news:cq7dg8$i0o$1@sea.gmane.org...
Please post a minimal but *complete* example (that means your program should have a main()) and show the exact command lines used to build your program and to invoke it.
The example: I build it in win2000+sp4, vc6+sp, stand template library is stlport, link with boost.python debug library, python2.4 #define BOOST_PYTHON_STATIC_MODULE #include <boost/python.hpp> using namespace boost::python; void write1( std::string s ) { printf( "Cout::write\t%s", s.c_str() ); } class Cout { public: std::string sbuf; public: Cout( std::string s ) { sbuf = s; } void write( std::string s ) { sbuf += s; if ( sbuf.find_first_of( "\n" ) == std::string::npos ) return; printf( "Cout::write\t%s", sbuf.c_str() ); sbuf = ""; } }; BOOST_PYTHON_MODULE(mym) { def( "write", &write1 ); class_<Cout>( "Cout", init<std::string>() ) .def_readwrite( "sbuf", &Cout::sbuf ) .def( "write", &Cout::write ) ; } void test() { // mym.write() expect one parameter, I give it two to test the error output PyRun_SimpleString( "import mym\n" "import sys\n" "sys.stderr = mym.Cout('')\n" "mym.write('aa','bb')\n" ); } void main( int argc, char **argv ) { if (PyImport_AppendInittab("mym", initmym) == -1) throw std::runtime_error("Failed to add mym to the interpreter's builtin modules"); Py_Initialize( ); if (handle_exception(test)) { if (PyErr_Occurred()) PyErr_Print(); } Py_Finalize(); } #include "module_tail.cpp"
Donnie Leen wrote:
"David Abrahams" <dave@boost-consulting.com> wrote in message news:cq7dg8$i0o$1@sea.gmane.org...
Please post a minimal but *complete* example (that means your program should have a main()) and
**********************************
show the exact command lines used to build your program and to invoke it.
Thanks, you're partway there. Could you please supply the missing info? Also, which version of STLPort are you using. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
"David Abrahams" <dave@boost-consulting.com> wrote in message news:41CA089D.8080200@boost-consulting.com...
Donnie Leen wrote:
"David Abrahams" <dave@boost-consulting.com> wrote in message news:cq7dg8$i0o$1@sea.gmane.org...
Please post a minimal but *complete* example (that means your program should have a main()) and
**********************************
show the exact command lines used to build your program and to invoke it.
Thanks, you're partway there. Could you please supply the missing info? Also, which version of STLPort are you using.
I build it in win2000+sp4, vc6+sp5, stand template library is stlport v4.5.3, link with boost.python debug library(boost.python version 1.32.0), python2.4 I create a "Win32 console Application" (Empty project) in vc6 IDE, add the source file and use the default vc6 project setting, compile& link both in IDE, not commandline.
Donnie Leen wrote:
"David Abrahams" <dave@boost-consulting.com> wrote in message news:41CA089D.8080200@boost-consulting.com...
Donnie Leen wrote:
"David Abrahams" <dave@boost-consulting.com> wrote in message news:cq7dg8$i0o$1@sea.gmane.org...
Please post a minimal but *complete* example (that means your program should have a main()) and
**********************************
show the exact command lines used to build your program and to invoke it.
Thanks, you're partway there. Could you please supply the missing info? Also, which version of STLPort are you using.
I build it in win2000+sp4, vc6+sp5, stand template library is stlport v4.5.3, link with boost.python debug library(boost.python version 1.32.0), python2.4 I create a "Win32 console Application" (Empty project) in vc6 IDE, add the source file and use the default vc6 project setting, compile& link both in IDE, not commandline.
Ah. Well, then there's something wrong with your Visual Studio project settings. It works for me here when built with bjam. ====== BEGIN OUTPUT ====== Cout::write Traceback (most recent call last): Cout::write File "<string>", line 4, in ? Cout::write Boost.Python.ArgumentError: Python argument types in mym.write(str, str) did not match C++ signature: write(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)Cout::write 'import site' failed; use -v for traceback [3378 refs] EXIT STATUS: 0 ====== END OUTPUT ====== ====== BEGIN JAMFILE ======== import python.jam ; import testing.jam ; run foo.cpp <dll>@boost/libs/python/build/boost_python : # program args : # input files : # requirements $(PYTHON_PROPERTIES) <library-path>$(PYTHON_LIB_PATH) <find-library>$(PYTHON_EMBEDDED_LIBRARY) ; ======== END JAMFILE ========== You can discover the differences by observing the bjam output with "bjam -n -a" and comparing that with the command lines generated by the IDE when it invokes the compiler. HTH, -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
"David Abrahams" <dave@boost-consulting.com> wrote in message news:41CA5811.6060907@boost-consulting.com...
You can discover the differences by observing the bjam output with "bjam -n -a" and comparing that with the command lines generated by the IDE when it invokes the compiler.
I build the program again in release version, it runs ok :) Perhaps it did had something difference between debug and release version, I had tried for many times. Thanks. Donnie Leen
participants (2)
-
David Abrahams -
Donnie Leen