code: def stdchannel_redirected(stdchannel):
      
      try:
          s = io.StringIO()
          old = getattr(sys, stdchannel)
          setattr(sys, stdchannel, s)
          yield s
      finally:
          setattr(sys, stdchannel, old)
docs: Redirects stdout or stderr to a StringIO object. As of python 3.4, there is a
      standard library contextmanager for this, but backwards compatibility!