code: def open_stream(
      filename: "t.Union[str, os.PathLike[str]]",
      mode: str = "r",
      encoding: t.Optional[str] = None,
      errors: t.Optional[str] = "strict",
      atomic: bool = False,
  ) -> t.Tuple[t.IO[t.Any], bool]:
      binary = "b" in mode
      filename = os.fspath(filename)
  
      # Standard streams first. These are simple because they ignore the
      # atomic flag. Use fsdecode to handle Path("-").
      if os.fsdecode(filename) == "-":
          if any(m in mode for m in ["w", "a", "x"]):
              if binary:
                  return get_binary_stdout(), False
              return (
                  get_text_stdout(encoding=encoding, errors=errors),
                  False,
              )
          if binary:
              return get_binary_stdin(), False
          return get_text_stdin(encoding=encoding, errors=errors), False
  
      # Non-atomic writes directly go out through the regular open functions.
      if not atomic:
          return _wrap_io_open(filename, mode, encoding, errors), True
  
      # Some usability stuff for atomic writes
      if "a" in mode:
          raise ValueError(
              "Appending to an existing file is not supported, because that"
              " would involve an expensive `copy`-operation to a temporary"
              " file. Open the file in normal `w`-mode and copy explicitly"
              " if that's what you're after."
          )
      if "x" in mode:
          raise ValueError("Use the `overwrite`-parameter instead.")
      if "w" not in mode:
          raise ValueError(
              "Atomic writes only make sense with `w`-mode."
          )
  
      # Atomic writes are more complicated.  They work by opening a file
      # as a proxy in the same folder and then using the fdopen
      # functionality to wrap it in a Python file.  Then we wrap it in an
      # atomic file that moves the file over on close.
      import errno
      import random
  
      try:
          perm: t.Optional[int] = os.stat(filename).st_mode
      except OSError:
          perm = None
  
      flags = os.O_RDWR | os.O_CREAT | os.O_EXCL
  
      if binary:
          flags |= getattr(os, "O_BINARY", 0)
  
      while True:
          tmp_filename = os.path.join(
              os.path.dirname(filename),
              f".__atomic-write{random.randrange(1 << 32):08x}",
          )
          try:
              fd = os.open(
                  tmp_filename, flags, 0o666 if perm is None else perm
              )
              break
          except OSError as e:
              if e.errno == errno.EEXIST or (
                  os.name == "nt"
                  and e.errno == errno.EACCES
                  and os.path.isdir(e.filename)
                  and os.access(e.filename, os.W_OK)
              ):
                  continue
              raise
  
      if perm is not None:
          os.chmod(
              tmp_filename, perm
          )  # in case perm includes bits in umask
  
      f = _wrap_io_open(fd, mode, encoding, errors)
      af = _AtomicFile(f, tmp_filename, os.path.realpath(filename))
      return t.cast(t.IO[t.Any], af), True
  
imports: import codecs
  import io
  import os
  import re
  import sys
  import typing
  from weakref import WeakKeyDictionary
label_desc: Use image node_id 21 for calling a global function with example usage: open_stream(filename, mode, encoding, errors, atomic) and returns: t,  True, get_text_stdin,  False, _wrap_io_open,  True, get_text_stdout,  False, get_binary_stdin,  False, get_binary_stdout,  False