Embedding Python in Windows Applications

Gerson Kurz gerson.kurz at t-online.de
Sun Dec 31 09:54:33 EST 2000


On Sun, 31 Dec 2000 00:25:09 GMT, Mark Hammond <MarkH at ActiveState.com>
wrote:

>Python itself always prints via "sys.stdout" and "sys.stderr".  Thus, if 
>the first thing you do is to set sys.stdout and sys.stderr to some 
>object of yours, again you have full control over where this output 
>goes.  But again, only you know what makes sense.

Thanks, but how would you do that ? For starters, I have tried the
following test code (in your excellent PythonWin):

-----------------------------------------------
import sys

class myclass:
    def __init__(self):
        self.file = open("D:\\test.log","w")
    
    def write(self,str):       
        self.file.write(str)

    def __del__(self):
        self.file.close()

old_stdout = sys.stdout
sys.stdout = myclass()
print "Hello, world"
sys.stdout = old_stdout
print "Done."
-----------------------------------------------

Now, as expected the first print statement will write to the file,
BUT, the second statement will raise an IO Error.

Traceback (most recent call last):
  File "d:\python20\pythonwin\pywin\framework\scriptutils.py", line
298, in RunScript
    debugger.run(codeObject, __main__.__dict__, start_stepping=0)
  File "d:\python20\pythonwin\pywin\debugger\__init__.py", line 60, in
run
    _GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
  File "d:\python20\pythonwin\pywin\debugger\debugger.py", line 582,
in run
    _doexec(cmd, globals, locals)
  File "d:\python20\pythonwin\pywin\debugger\debugger.py", line 921,
in _doexec
    exec cmd in globals, locals
  File "D:\Python20\Script1.py", line 17, in ?
    print "Done."
  File "D:\Python20\Script1.py", line 6, in write
    
IOError: (0, 'Error')

Secondly, can you tell me which methods I have to implement in my
pseudo-stdout ? And why don't I have to derive my class from some base
file class (or do I) ?




More information about the Python-list mailing list