code: def secure_write(fname, binary=False):
      """Opens a file in the most restricted pattern available for
      writing content. This limits the file mode to `0o0600` and yields
      the resulting opened filed handle.
  
      Parameters
      ----------
  
      fname : unicode
          The path to the file to write
  
      binary: boolean
          Indicates that the file is binary
      """
      mode = "wb" if binary else "w"
      open_flag = os.O_CREAT | os.O_WRONLY | os.O_TRUNC
      try:
          os.remove(fname)
      except (IOError, OSError):
          # Skip any issues with the file not existing
          pass
  
      if os.name == "nt":
          if allow_insecure_writes:
              # Mounted file systems can have a number of failure modes inside this block.
              # For windows machines in insecure mode we simply skip this to avoid failures :/
              issue_insecure_write_warning()
          else:
              # Python on windows does not respect the group and public bits for chmod, so we need
              # to take additional steps to secure the contents.
              # Touch file pre-emptively to avoid editing permissions in open files in Windows
              fd = os.open(fname, open_flag, 0o0600)
              os.close(fd)
              open_flag = os.O_WRONLY | os.O_TRUNC
              win32_restrict_file_to_user(fname)
  
      with os.fdopen(os.open(fname, open_flag, 0o0600), mode) as f:
          if os.name != "nt":
              # Enforce that the file got the requested permissions before writing
              file_mode = get_file_mode(fname)
              if 0o0600 != file_mode:
                  if allow_insecure_writes:
                      issue_insecure_write_warning()
                  else:
                      raise RuntimeError(
                          "Permissions assignment failed for secure file: '{file}'."
                          " Got '{permissions}' instead of '0o0600'.".format(
                              file=fname, permissions=oct(file_mode)
                          )
                      )
          yield f
  
imports: import os
  import sys
  import stat
  import errno
  import tempfile
  import warnings
  from ipython_genutils import py3compat
  from contextlib import contextmanager
  from distutils.util import strtobool
  from ipython_genutils import py3compat
label_desc: Use image node_id 14 for calling a global function with example usage: secure_write(fname, binary) without return types