def logsafe(val):
 if isinstance(val, six.text_type):
  return val
 elif isinstance(val, bytes):
  return val.decode('utf-8', 'replace')
 elif isinstance(val, subprocess.CalledProcessError):
  try:
   return six.text_type(val)
  except UnicodeDecodeError:
   return str(val).decode('utf-8', 'replace')
 else:
  return val

def logsafe ( val ) : if isinstance ( val , six . text_type ) : return val elif isinstance ( val , bytes ) : return val . decode ( 'utf-8' , 'replace' ) elif isinstance ( val , subprocess . CalledProcessError ) : try : return six . text_type ( val ) except UnicodeDecodeError : return str ( val ) . decode ( 'utf-8' , 'replace' ) else : return val
coerce a potentially "problematic" value so it can be formatted in a unicode log string .