Sign in to follow this  

embeding python into c++ and sys.displayhook

This topic is 4331 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

hello, i working on embeding python into my c++ engine. I'm using pyCXX library. I have a graphics console system which is actually python console and import new module extended by pyCXX. To get output from python i'm using simple redirecting stdout/stderr:
sys.old_stdout = sys.stdout
sys.old_stderr = sys.stderr

class my_stdout_writer:
   def write(self, text):
      engine_stdout(text)
   def __del__(self):
      sys.stdout = sys.old_stdout

... stderr is quite same...

and then simply:

sys.stdout = my_stdout_writer()
sys.stderr = my_stderr_writer()
so simple.. engine_stdout() is basic c++ extension from my app. everything works fine, except sys.displayhook which doesn't use my_stdout_writer (print dir() works but only dir() not) when i override sys.displayhook this way:
def my_displayhook(o):
   engine_stdout(o)

sys.displayhook = my_displayhook
it's seems that doesn't work. when i try it from python interpret (./python in xterm for example :)) it's ok. any advice where can be mistake please?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Kylotan
sys.displayhook is only for the interactive interpreter - it shouldn't do anything when you have Python embedded as part of an application.


ok. it's clear. so is there any possible workaround?

Share this post


Link to post
Share on other sites
Quote:
Original post by Kylotan
What exactly are you trying to do, and under what circumstances does it not do it?


ok. simple example: python module has methods: run_command(string) and run_script(string). Graphics console input emit signal with commands into python module. run_command is implemented simple way:

..
if PyRun_SimpleString(str) {
if (PyErr_Occurred()) PyErr_Print();
..
}
..

so when i call run_command('print dir()') output of dir will get back through stdout back into my engine, but when i call run_command('dir()') it's not. I like to catch the output of command 'dir()' too. that's the problem. i don't want write print [something] into console all the time. hope it's clear description what i want :)

Share this post


Link to post
Share on other sites
I think that's just part of the interpreter, automatically printing out the result of any expression you enter. I don't think you can get that from within your own programs unless you essentially duplicate the interpreter loop yourself.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Kylotan
I think that's just part of the interpreter, automatically printing out the result of any expression you enter. I don't think you can get that from within your own programs unless you essentially duplicate the interpreter loop yourself.


ok. thanks. i know only high-level api of python. i was trying find simple example at google how to duplicate the interpreter loop. do you know where can i find any hints how to do that please?

Share this post


Link to post
Share on other sites
i have found quite simple solution:


void PythonModule::run_command(const std::string& command)
{
PyRun_SimpleString(command.c_str());
if (PyErr_Occured()) PyErr_Print();
}

void PythonModule::run_interactive_command(const std::string& command) // command from gfx console
{
this->run_command("try:\n"
" print eval(\"" + command + "\")\n"
"except SyntaxError:\n"
" " + command);
}


certainly it's not best solution.

Share this post


Link to post
Share on other sites

This topic is 4331 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