def unicode_urlencode(obj, charset='utf-8', for_qs=False):
 if (not isinstance(obj, string_types)):
  obj = text_type(obj)
 if isinstance(obj, text_type):
  obj = obj.encode(charset)
 safe = (((not for_qs) and '/') or '')
 rv = text_type(url_quote(obj, safe))
 if for_qs:
  rv = rv.replace('%20', '+')
 return rv

def unicode_urlencode ( obj , charset = 'utf-8' , for_qs = False ) : if ( not isinstance ( obj , string_types ) ) : obj = text_type ( obj ) if isinstance ( obj , text_type ) : obj = obj . encode ( charset ) safe = ( ( ( not for_qs ) and '/' ) or '' ) rv = text_type ( url_quote ( obj , safe ) ) if for_qs : rv = rv . replace ( '%20' , '+' ) return rv
url escapes a single bytestring or unicode string with the given charset if applicable to url safe quoting under all rules that need to be considered under all supported python versions .