Redirect question
Alex Martelli
aleax at aleax.it
Thu Nov 21 12:43:03 EST 2002
Karsten Weinert wrote:
> Well, after solving my error in the Redirect class with all your help,
> I now encountered a new problem. Here is the program:
>
> import sys, win32ui
>
> class Redirect:
> def __init__(self):
> self.s=""
> def write(self, s):
> self.s+=s
> def flush(self):
> win32ui.MessageBox(self.s)
>
> sys.stdout = Redirect()
> sys.stderr = Redirect()
> print 'Hidey Ho'
> print "Hidey hidey hidey ho"
> sys.stdout.flush() # is displayed
> # from here, no output is produced, e.g. no messagebox
> assert 1==0, "Impossible Assertion"
> sys.stderr.flush()
>
> What am I doing wrong? Enclosing the assert statement with try/finally
> or try/except only displays an empty message box.
>
> Kind regards,
> Karsten
The sys.stderr.flush call can never execute, since it follows a statement
that raises an exception.
To follow in more detail what is happening, I suggest you add print calls
(to sys.__stdout__ for example, to make sure they're going to get out)
tracing the behaviour of self.s in Redirect's methods, and run the whole
script via python.exe, not pythonw.exe, from a console (aka "Dos Box").
Alex
More information about the Python-list
mailing list