code: def captured_fd(stream=2, encoding=None):
      orig_stream = os.dup(stream)  # keep copy of original stream
      try:
          with tempfile.TemporaryFile(mode="a+b") as temp_file:
  
              def read_output(_output=[b""]):
                  if not temp_file.closed:
                      temp_file.seek(0)
                      _output[0] = temp_file.read()
                  return _output[0]
  
              os.dup2(
                  temp_file.fileno(), stream
              )  # replace stream by copy of pipe
              try:
  
                  def get_output():
                      result = read_output()
                      return (
                          result.decode(encoding)
                          if encoding
                          else result
                      )
  
                  yield get_output
              finally:
                  os.dup2(
                      orig_stream, stream
                  )  # restore original stream
                  read_output()  # keep the output in case it's used after closing the context manager
      finally:
          os.close(orig_stream)
  
imports: from __future__ import absolute_import
  import os
  import sys
  import re
  import io
  import codecs
  import shutil
  import tempfile
  from contextlib import contextmanager
label_desc: Use image node_id 22 for calling a global function with example usage: captured_fd(stream, encoding) and returns: _output, unknown