def to_encodable_string(obj, encoding):
 if isinstance(obj, DataToken):
  obj = obj.get_value()
 if isinstance(obj, unicode):
  obj = obj.encode(encoding, errors='ignore')
 else:
  obj = str(obj)
 return obj

def to_encodable_string ( obj , encoding ) : if isinstance ( obj , DataToken ) : obj = obj . get_value ( ) if isinstance ( obj , unicode ) : obj = obj . encode ( encoding , errors = 'ignore' ) else : obj = str ( obj ) return obj
this makes an extra effort to return a string which can be url-encoded from an object that can be one of: * byte-string * unicode * datatoken and in all cases it might have invalid encoding .