[C++-sig] Re: what happened with this std::string parameter inboost.python

Donnie Leen Kingdom.lin at yeah.net
Mon Dec 20 18:45:32 CET 2004


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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20041221/fb9545fa/attachment.htm>


More information about the Cplusplus-sig mailing list