Sign in to follow this  

Getting 'prints' from an embedded Python?

This topic is 4073 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've embedded Python into my game. I'd like to be able to print text out from Python, but doing that doesn't seem to actually output anything. Since all the tutorials that start off with "Hello, world" scripts have a console opened and the Python output went to that console, I was thinking that because I didn't have a console, it wasn't actually going anywhere. In that case, how can I 'grab' the Python prints in my C++ program? Or, how can I redirect them to a file? Thanks in advanced.

Share this post


Link to post
Share on other sites
If you export a method that takes a string as a parameter, you can install that as pythons std::out sink. It's pretty simple if u e.g. use boost::python.

Unfortunatly I don't have src code available right now. If nbdy else does before me, I will post some code, when I get home from work. :-)

Share this post


Link to post
Share on other sites
Yes, I am using Boost.Python. I'd love to see that source code when you're back, since I can't find any information about this in the Boost.Python documentation.

Share this post


Link to post
Share on other sites
ah I c sbdy already supplied a tutorial....

However I just put together some code from a former project right now.
I have to appologize, I didn't test it before posting, cause I'm in a little hurry, but since I promised to to so, here is some code:


void TraceCB( const std::string& i_String )
{
LOG_STRING( i_String );
}





/////////////////////////////////////////////////////////////////////

void ErrorCB( const std::string& i_String )
{
LOG_STRING( i_String );
}





/////////////////////////////////////////////////////////////////////

BOOST_PYTHON_MODULE( LogModule )
{
using namespace boost::python;

def( "TraceCB", TraceCB );
def( "ErrorCB", ErrorCB );
}





/////////////////////////////////////////////////////////////////////

void InitializeLogModule( void )
{
initLogModule()
}





/////////////////////////////////////////////////////////////////////

bool RunTestScript( const std::string& i_Script )
{
using namespace boost::python;

try
{
object MainModule( handle<>( borrowed( PyImport_AddModule("__main__") ) ) );
object MainNamespace = MainModule.attr( "__dict__" );

handle<> Result( allow_null( PyRun_String( FixEndLines( i_Script ).c_str(),
Py_file_input,
MainNamespace.ptr(),
MainNamespace.ptr() )));

if ( !Result )
{
EOKANE_ERROR( "Python script execution failed." );
PyErr_Print();

return false;
}

return true;

}

catch( ... )
{
EOKANE_ERROR( "Critical error while executing python script." );
PyErr_Print();
}

return false;
}


int main( int argc, char** argv )
{
Py_InitializeEx( 0 );

if ( Py_IsInitialized() )
{
InitializeLogModule();

std::string Script( "import sys"
"import LogModule"
""
"class StdOutRedirector:"
" def write( i_Self, i_String ):"
" LogModule.TraceCB( i_String )"
""
"class StdErrorRedirector:"
" def write( i_Self, i_String ):"
" LogModule.ErrorCB( i_String )"
""
"sys.stdout = StdOutRedirector();"
"sys.stderr = StdErrorRedirector();"
""
"print \"Hello World\"" );

RunTestScript( Script );

Py_Finalize();
}

}



if you can't get it going, just drop me a line and I'll try to put together a little project u can step through with the debugger... that's what I like best for myself.

Cheers Jochen

Share this post


Link to post
Share on other sites

This topic is 4073 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this