docs: def is_binary(f):
      """Return True if binary mode."""
      # NOTE: order matters here. We don't bail on Python 2 just yet. Both
      # codecs.open() and io.open() can open in text mode, both set the encoding
      # attribute. We must do that check first.
  
      # If it has a decoding attribute with a value, it is text mode.
      if getattr(f, "encoding", None):
          return False
  
      # Python 2 makes no further distinction.
      if not PY3:
          return True
  
      # If the file has a mode, and it contains b, it is binary.
      try:
          if 'b' in getattr(f, 'mode', ''):
              return True
      except TypeError:
          import gzip
          if isinstance(f, gzip.GzipFile):
              return True  # in gzip mode is an integer
          raise
  
      # Can we sniff?
      try:
          f.seek(0, os.SEEK_CUR)
      except (AttributeError, IOError):
          return False
  
      # Finally, let's sniff by reading a byte.
      byte = f.read(1)
      f.seek(-1, os.SEEK_CUR)
      return hasattr(byte, 'decode')