code: def _stdio_stream_as(src_fd, dst_fd, dst_sys_attribute, mode):
    
    if src_fd == -1:
      src = open('/dev/null', mode)
      src_fd = src.fileno()
  
    # Capture the python and os level file handles.
    old_dst = getattr(sys, dst_sys_attribute)
    old_dst_fd = os.dup(dst_fd)
    if src_fd != dst_fd:
      os.dup2(src_fd, dst_fd)
  
    # Open up a new file handle to temporarily replace the python-level io object, then yield.
    new_dst = os.fdopen(dst_fd, mode)
    setattr(sys, dst_sys_attribute, new_dst)
    try:
      yield
    finally:
      new_dst.close()
  
      # Restore the python and os level file handles.
      os.dup2(old_dst_fd, dst_fd)
      setattr(sys, dst_sys_attribute, old_dst)
docs: Replace the given dst_fd and attribute on `sys` with an open handle to the given src_fd.