How to capture stdout in an exec'ed string?

Preston Landers preston at jump.net
Thu Nov 18 04:22:08 CET 1999


Thanks a lot Mark!  It only took a few microseconds after I posted to
realize what I needed to do.  For some reason the concept of saving
and restoring the stdout handler didn't occur to me.  I was thinking
about namespace issues which are not really related.

---Preston

"Mark Hammond" <mhammond at skippinet.com.au> writes:

> Something like this should work:
> 
> try:
>    oldout = sys.stdout
>    sys.stdout = My_STDOUT()
>    try:
>      cruft = my_ancient_class()
>      cruft.do_your_thang()
>    finally:
>      sys.stdout = oldout
> except SystemExit, msg:
>    process(msg)
> 
> 
> Mark.




More information about the Python-list mailing list