# Lets override stdout/stdin/stderr NOW
import sys
import engine
class ConRedirect(object):
@staticmethod
def write(s):
engine.ConPrint(s)
sys.stdout = ConRedirect()
sys.stderr = ConRedirect()
print ("blah")
somethingthatdoesntexist.lol # cause a runtime error
After running that code, print("blah") will be rerouted to engine.ConPrint, but stderr doesn't seem to get called at all (no traceback mesages get displayed, not even to the default console). If I comment out the line sys.stderr = stdoutRedirect() then it gets displayed as normal. This is quite frustrating as besides this, embedding python is going quite well. Any clues? Thanks.
🔒 Locked
Rerouting stderr in Python
Advertisement
Original Post
Hi, I'm trying to reroute stderr and stdout in python to my ingame console. It's working for stdout but not stderr! >_<
Playing around some more, I found that if i did:
The traceback report is correctly outputted to the file. What the hell is going on ??? >___<
sys.stderr = open("ilikepie.txt", "w+")The traceback report is correctly outputted to the file. What the hell is going on ??? >___<
Okay. After dreaming about this problem all night (one of the really annoying frustration dreams), I still can't work it out. I've discovered that when I do this:
The except: code path gets executed while displaying a stderr! I'm not throwing any exception from engine.ConPrint(s). In fact, if i put a breakpoint in my engine.ConPrint function (implemented in C++) it never even gets called!
WTF >_<
class ConRedirect: def write(self, s): try: engine.ConPrint(s) except: print("Exception: " + s)The except: code path gets executed while displaying a stderr! I'm not throwing any exception from engine.ConPrint(s). In fact, if i put a breakpoint in my engine.ConPrint function (implemented in C++) it never even gets called!
WTF >_<
Quote:
Original post by cow_in_the_well
The except: code path gets executed while displaying a stderr! I'm not throwing any exception from engine.ConPrint(s). In fact, if i put a breakpoint in my engine.ConPrint function (implemented in C++) it never even gets called!
The Python -> C++ interface itself might be borked. Re-check your binding code.
well, if i call engine.ConPrint("blah") directly, it works. And if I bind ConRedirect to stdout then print("blah") works. Its only with stderr. :(
I'm currently only redirecting stderr, since if i was redirecting stdout and used print() in ConRedirect it'd get into an infinite loop ;). So yeah, I'm currently only trying to get stderr to work (redirecting stdout to engine.ConPrint works).
Hmn. I can't get it to display the exception:
Nothing gets displayed. Also, i just tried calling stderr directly, and it prints it out correctly (no exception).
Thanks
class ConRedirect: def write(self, s): try: engine.ConPrint(s) except: print("Exception: " + sys.exc_info())Nothing gets displayed. Also, i just tried calling stderr directly, and it prints it out correctly (no exception).
sys.stderr = ConRedirect()sys.stderr.write("damnit")Thanks
Okay here it is: http://www.packetfrag.com/PyTest.zip
Its just a experiment app, so its a little messy, but its small enough to see whats going on.
init.py is where i'm doing the python side hookups...
Thanks [smile]
Its just a experiment app, so its a little messy, but its small enough to see whats going on.
init.py is where i'm doing the python side hookups...
Thanks [smile]
thats exactly the point. The error being raised by accesssing somethingthatdoesntexist SHOULD be getting displayed, but it's not. Comment out sys.stderr = ConRedirect() to see what should get outputted.
Oh, and I'm using python 2.4
Oh, and I'm using python 2.4
Quote:
Original post by cow_in_the_well
thats exactly the point. The error being raised by accesssing somethingthatdoesntexist SHOULD be getting displayed, but it's not. Comment out sys.stderr = ConRedirect() to see what should get outputted.
Ah, *hem*, I had taken that bit out. The traceback is printed out through the redirected stderr just fine.
Quote:
Oh, and I'm using python 2.4
I had guessed, from your use of function decorators ;)
ehhh....thats just weird. Bleh. This has burnt me out for the day, I might try it with python 2.3, although that shouldn't matter. I dug through the IDLE lib python code and copied exactly how they are redirecting and it still doesnt work o_O.
Thanks for the help Fruny.
Thanks for the help Fruny.
Okay, thanks to SHilbert, the problem is solved :).
I was rerouting stderr and stdout in the same module i was raising a runtime error (for testing). This was all invoked by calling PyImport_ImportModule("init"). So, there was an exception during the import so python is deinitializing the module and returning. When i go to print the error with PyErr_Print(), its calling the ConRedirect::write function inside the now "derelict" init module.
If i let init.py run to completion, then call another module test.py, and cause an error in there, then it all works fine. If that at all makes sense o_o.
Damn. What an obscure error to trace [smile]
Thanks all...
I was rerouting stderr and stdout in the same module i was raising a runtime error (for testing). This was all invoked by calling PyImport_ImportModule("init"). So, there was an exception during the import so python is deinitializing the module and returning. When i go to print the error with PyErr_Print(), its calling the ConRedirect::write function inside the now "derelict" init module.
If i let init.py run to completion, then call another module test.py, and cause an error in there, then it all works fine. If that at all makes sense o_o.
Damn. What an obscure error to trace [smile]
Thanks all...
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement