Is it possible to temporarily redirect stdout/stderr in Python (i.e. for the duration of a method)?
Edit:
The problem with the current solutions (which I at first remembered but then forgot) is that they don't redirect; rather, they just replace the streams in their entirety. Hence, if a method has a local copy of one the variable for any reason (e.g. because the stream was passed as a parameter to something), it won't work.
Any solutions?
stderr
orstdout
wired-in, you can't always avoid using that kind of code. – Brandybrandyn.write()
). All references to sys.stdout point to it. Have it proxy to a changeable stream, defaulting to stdout. You should then have the ability to switch the proxied stream at will. I haven't tried this; I'm thinking out loud. – Gambesonsys.stdout
andsys.stderr
. There issys.__stdout__
andsys.__stderr__
. (reference) – Maniac