Skip to main content
GameDev.net gamedev.net
🔒 Locked

Getting 'prints' from an embedded Python?

Started by Ezbez Oct 20, 2006 at 6:00 AM 4 replies 2.5k views
Original Post
Ezbez
Ezbez
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.
jochen
jochen
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. :-)
Why did Doug Gregor create the empty directory?
Ezbez
Ezbez
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.
jochen
jochen
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
Why did Doug Gregor create the empty directory?
Ezbez
Ezbez
I got it working now, many thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.