# Path: werkzeug/_compat.py
# PY2 = sys.version_info[0] == 2
#     def fix_tuple_repr(obj):
#         def __repr__(self):
#     def implements_iterator(cls):
#     def implements_to_string(cls):
#     def native_string_result(func):
#         def wrapper(*args, **kwargs):
#     def implements_bool(cls):
#     def make_literal_wrapper(reference):
#     def normalize_string_tuple(tup):
#     def try_coerce_native(s):
#     def wsgi_decoding_dance(s, charset='utf-8', errors='replace'):
#     def wsgi_encoding_dance(s, charset='utf-8', errors='replace'):
#     def to_bytes(x, charset=sys.getdefaultencoding(), errors='strict'):
#     def to_native(x, charset=sys.getdefaultencoding(), errors='strict'):
#     def iter_bytes(b):
#     def reraise(tp, value, tb=None):
#     def make_literal_wrapper(reference):
#     def normalize_string_tuple(tup):
#     def wsgi_get_bytes(s):
#     def wsgi_decoding_dance(s, charset='utf-8', errors='replace'):
#     def wsgi_encoding_dance(s, charset='utf-8', errors='replace'):
#     def to_bytes(x, charset=sys.getdefaultencoding(), errors='strict'):
#     def to_native(x, charset=sys.getdefaultencoding(), errors='strict'):
# def to_unicode(x, charset=sys.getdefaultencoding(), errors='strict',
#                allow_none_charset=False):
# 
# Path: werkzeug/_internal.py
# def _encode_idna(domain):
#     # If we're given bytes, make sure they fit into ASCII
#     if not isinstance(domain, text_type):
#         domain.decode('ascii')
#         return domain
# 
#     # Otherwise check if it's already ascii, then return
#     try:
#         return domain.encode('ascii')
#     except UnicodeError:
#         pass
# 
#     # Otherwise encode each part separately
#     parts = domain.split('.')
#     for idx, part in enumerate(parts):
#         parts[idx] = part.encode('idna')
#     return b'.'.join(parts)
# 
# def _decode_idna(domain):
#     # If the input is a string try to encode it to ascii to
#     # do the idna decoding.  if that fails because of an
#     # unicode error, then we already have a decoded idna domain
#     if isinstance(domain, text_type):
#         try:
#             domain = domain.encode('ascii')
#         except UnicodeError:
#             return domain
# 
#     # Decode each part separately.  If a part fails, try to
#     # decode it with ascii and silently ignore errors.  This makes
#     # most sense because the idna codec does not have error handling
#     parts = domain.split(b'.')
#     for idx, part in enumerate(parts):
#         try:
#             parts[idx] = part.decode('idna')
#         except UnicodeError:
#             parts[idx] = part.decode('ascii', 'ignore')
# 
#     return '.'.join(parts)


import re
from werkzeug._compat import text_type, PY2, to_unicode, \
     to_native, implements_to_string, try_coerce_native, \
     normalize_string_tuple, make_literal_wrapper, \
     fix_tuple_repr
from werkzeug._internal import _encode_idna, _decode_idna
from werkzeug.datastructures import MultiDict, iter_multi_items
from collections import namedtuple
    from werkzeug.wsgi import make_chunk_iter
        if value is None:
            continue
        if not isinstance(key, bytes):
            key = text_type(key).encode(charset)
        if not isinstance(value, bytes):
            value = text_type(value).encode(charset)
        yield url_quote(key) + '=' + url_quote_plus(value)


def _url_unquote_legacy(value, unsafe=''):
    try:
        return url_unquote(value, charset='utf-8',
                           errors='strict', unsafe=unsafe)
    except UnicodeError:
        return url_unquote(value, charset='latin1', unsafe=unsafe)


def url_parse(url, scheme=None, allow_fragments=True):
    """Parses a URL from a string into a :class:`URL` tuple.  If the URL
    is lacking a scheme it can be provided as second argument. Otherwise,
    it is ignored.  Optionally fragments can be stripped from the URL
    by setting `allow_fragments` to `False`.

    The inverse of this function is :func:`url_unparse`.

    :param url: the URL to parse.
    :param scheme: the default schema to use if the URL is schemaless.
    :param allow_fragments: if set to `False` a fragment will be removed
                            from the URL.
    """

# Path: werkzeug/_compat.py
# PY2 = sys.version_info[0] == 2
#     def fix_tuple_repr(obj):
#         def __repr__(self):
#     def implements_iterator(cls):
#     def implements_to_string(cls):
#     def native_string_result(func):
#         def wrapper(*args, **kwargs):
#     def implements_bool(cls):
#     def make_literal_wrapper(reference):
#     def normalize_string_tuple(tup):
#     def try_coerce_native(s):
#     def wsgi_decoding_dance(s, charset='utf-8', errors='replace'):
#     def wsgi_encoding_dance(s, charset='utf-8', errors='replace'):
#     def to_bytes(x, charset=sys.getdefaultencoding(), errors='strict'):
#     def to_native(x, charset=sys.getdefaultencoding(), errors='strict'):
#     def iter_bytes(b):
#     def reraise(tp, value, tb=None):
#     def make_literal_wrapper(reference):
#     def normalize_string_tuple(tup):
#     def wsgi_get_bytes(s):
#     def wsgi_decoding_dance(s, charset='utf-8', errors='replace'):
#     def wsgi_encoding_dance(s, charset='utf-8', errors='replace'):
#     def to_bytes(x, charset=sys.getdefaultencoding(), errors='strict'):
#     def to_native(x, charset=sys.getdefaultencoding(), errors='strict'):
# def to_unicode(x, charset=sys.getdefaultencoding(), errors='strict',
#                allow_none_charset=False):
# 
# Path: werkzeug/_internal.py
# def _encode_idna(domain):
#     # If we're given bytes, make sure they fit into ASCII
#     if not isinstance(domain, text_type):
#         domain.decode('ascii')
#         return domain
# 
#     # Otherwise check if it's already ascii, then return
#     try:
#         return domain.encode('ascii')
#     except UnicodeError:
#         pass
# 
#     # Otherwise encode each part separately
#     parts = domain.split('.')
#     for idx, part in enumerate(parts):
#         parts[idx] = part.encode('idna')
#     return b'.'.join(parts)
# 
# def _decode_idna(domain):
#     # If the input is a string try to encode it to ascii to
#     # do the idna decoding.  if that fails because of an
#     # unicode error, then we already have a decoded idna domain
#     if isinstance(domain, text_type):
#         try:
#             domain = domain.encode('ascii')
#         except UnicodeError:
#             return domain
# 
#     # Decode each part separately.  If a part fails, try to
#     # decode it with ascii and silently ignore errors.  This makes
#     # most sense because the idna codec does not have error handling
#     parts = domain.split(b'.')
#     for idx, part in enumerate(parts):
#         try:
#             parts[idx] = part.decode('idna')
#         except UnicodeError:
#             parts[idx] = part.decode('ascii', 'ignore')
# 
#     return '.'.join(parts)


# Path: werkzeug/urls.py
import re
from werkzeug._compat import text_type, PY2, to_unicode, \
     to_native, implements_to_string, try_coerce_native, \
     normalize_string_tuple, make_literal_wrapper, \
     fix_tuple_repr
from werkzeug._internal import _encode_idna, _decode_idna
from werkzeug.datastructures import MultiDict, iter_multi_items
from collections import namedtuple
    from werkzeug.wsgi import make_chunk_iter

        if value is None:
            continue
        if not isinstance(key, bytes):
            key = text_type(key).encode(charset)
        if not isinstance(value, bytes):
            value = text_type(value).encode(charset)
        yield url_quote(key) + '=' + url_quote_plus(value)


def _url_unquote_legacy(value, unsafe=''):
    try:
        return url_unquote(value, charset='utf-8',
                           errors='strict', unsafe=unsafe)
    except UnicodeError:
        return url_unquote(value, charset='latin1', unsafe=unsafe)


def url_parse(url, scheme=None, allow_fragments=True):
    """Parses a URL from a string into a :class:`URL` tuple.  If the URL
    is lacking a scheme it can be provided as second argument. Otherwise,
    it is ignored.  Optionally fragments can be stripped from the URL
    by setting `allow_fragments` to `False`.

    The inverse of this function is :func:`url_unparse`.

    :param url: the URL to parse.
    :param scheme: the default schema to use if the URL is schemaless.
    :param allow_fragments: if set to `False` a fragment will be removed
                            from the URL.
    """

    s = make_literal_wrapper(url)