def urlencode2(query, doseq=<NUM_LIT:0>, safe="<STR_LIT>", querydelimiter="<STR_LIT:&>"):
if hasattr(query,"<STR_LIT>"):<EOL><INDENT>query = query.items()<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>if len(query) and not isinstance(query[<NUM_LIT:0>], tuple):<EOL><INDENT>raise TypeError<EOL><DEDENT><DEDENT>except TypeError:<EOL><INDENT>ty,va,tb = sys.exc_info()<EOL>raise TypeError("<STR_LIT>" + tb)<EOL><DEDENT><DEDENT>l = []<EOL>if not doseq:<EOL><INDENT>for k, v in query:<EOL><INDENT>k = quote_plus(str(k), safe=safe)<EOL>v = quote_plus(str(v), safe=safe)<EOL>l.append(k + '<STR_LIT:=>' + v)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for k, v in query:<EOL><INDENT>k = quote_plus(str(k), safe=safe)<EOL>if isinstance(v, str):<EOL><INDENT>v = quote_plus(v, safe=safe)<EOL>l.append(k + '<STR_LIT:=>' + v)<EOL><DEDENT>elif _is_unicode(v):<EOL><INDENT>v = quote_plus(v.encode("<STR_LIT>","<STR_LIT:replace>"))<EOL>l.append(k + '<STR_LIT:=>' + v)<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>len(v)<EOL><DEDENT>except TypeError:<EOL><INDENT>v = quote_plus(str(v), safe=safe)<EOL>l.append(k + '<STR_LIT:=>' + v)<EOL><DEDENT>else:<EOL><INDENT>for elt in v:<EOL><INDENT>l.append(k + '<STR_LIT:=>' + quote_plus(str(elt)))<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>return querydelimiter.join(l)<EOL>
Encode a sequence of two-element tuples or dictionary into a URL query string.

    If any values in the query arg are sequences and doseq is true, each
    sequence element is converted to a separate parameter.

    If the query arg is a sequence of two-element tuples, the order of the
    parameters in the output will match the order of parameters in the
    input.