code: def _fd_or_path_or_tempfile(fd, mode="w+b", tempfile=True):
      close_fd = False
      if fd is None and tempfile:
          fd = TemporaryFile(mode=mode)
          close_fd = True
  
      if isinstance(fd, basestring):
          fd = open(fd, mode=mode)
          close_fd = True
  
      if isinstance(fd, BufferedReader):
          close_fd = True
  
      try:
          if isinstance(fd, os.PathLike):
              fd = open(fd, mode=mode)
              close_fd = True
      except AttributeError:
          # module os has no attribute PathLike, so we're on python < 3.6.
          # The protocol we're trying to support doesn't exist, so just pass.
          pass
  
      return fd, close_fd
  
imports: from __future__ import division
  from io import BufferedReader
  import json
  import os
  import re
  import sys
  from subprocess import Popen, PIPE
  from math import log, ceil
  from tempfile import TemporaryFile
  from warnings import warn
  from functools import wraps
label_desc: Use image node_id 4 for calling a global function with example usage: _fd_or_path_or_tempfile(fd, mode, tempfile) and returns: fd,  close_fd