__all__ = (
    'quote',
    'splitnport',
    'urlencode',
    'urlsplit',
    'urlunsplit',
)

try:
    from urllib.parse import (
        quote, splitnport, urlencode, urlsplit, urlunsplit)
except ImportError:
    from urllib import quote, splitnport, urlencode
    from urlparse import urlsplit, urlunsplit

__all__ = (
    'quote',
    'splitnport',
    'urlencode',
    'urlsplit',
    'urlunsplit',
)

try:
    from urllib.parse import (
        quote, splitnport, urlencode, urlsplit, urlunsplit)
except ImportError:
    from urllib import quote, splitnport, urlencode as _urlencode
    from urlparse import urlsplit, urlunsplit

    # urlencode did not encode its parameters in Python 2.x so we
    # need to implement that ourselves for compatibility.
    def urlencode(query, doseq=0, safe='', encoding=None, errors=None):

        if encoding is None:
            encoding = 'utf-8'
        if errors is None:
            errors = 'strict'

        def encode_value(v):
            try:
                return codecs.encode(v, encoding, errors)
            except UnicodeError:
                raise
            except (AttributeError, TypeError):
                return str(v)

        try:
            quoted = []
            for name, value in query:
                quoted.append((encode_value(name), encode_value(value)))
            query = quoted
        except UnicodeError:
            raise
        except (TypeError, ValueError) as exc:  # pragma no cover
            # doesn't look like a sequence of tuples, maybe a dict?
            try:
                quoted = {}
                for name, value in query.items():
                    quoted[encode_value(name)] = encode_value(value)
                query = quoted
            except AttributeError:  # not a dictionary either
                pass

        return _urlencode(query, doseq=doseq)

Fix urlencode for Python < 3.0.
compat: Fix urlencode for Python < 3.0.

The urlencode function handles encoding in Python 3.x so our
compatibility layer should do so to.

  __all__ = (
      'quote',
      'splitnport',
      'urlencode',
      'urlsplit',
      'urlunsplit',
  )
  
  try:
      from urllib.parse import (
          quote, splitnport, urlencode, urlsplit, urlunsplit)
  except ImportError:
-     from urllib import quote, splitnport, urlencode
+     from urllib import quote, splitnport, urlencode as _urlencode
      from urlparse import urlsplit, urlunsplit
  
+     # urlencode did not encode its parameters in Python 2.x so we
+     # need to implement that ourselves for compatibility.
+     def urlencode(query, doseq=0, safe='', encoding=None, errors=None):
+ 
+         if encoding is None:
+             encoding = 'utf-8'
+         if errors is None:
+             errors = 'strict'
+ 
+         def encode_value(v):
+             try:
+                 return codecs.encode(v, encoding, errors)
+             except UnicodeError:
+                 raise
+             except (AttributeError, TypeError):
+                 return str(v)
+ 
+         try:
+             quoted = []
+             for name, value in query:
+                 quoted.append((encode_value(name), encode_value(value)))
+             query = quoted
+         except UnicodeError:
+             raise
+         except (TypeError, ValueError) as exc:  # pragma no cover
+             # doesn't look like a sequence of tuples, maybe a dict?
+             try:
+                 quoted = {}
+                 for name, value in query.items():
+                     quoted[encode_value(name)] = encode_value(value)
+                 query = quoted
+             except AttributeError:  # not a dictionary either
+                 pass
+ 
+         return _urlencode(query, doseq=doseq)
+ 
Fix urlencode for Python < 3.0.
## Code Before:
__all__ = (
    'quote',
    'splitnport',
    'urlencode',
    'urlsplit',
    'urlunsplit',
)

try:
    from urllib.parse import (
        quote, splitnport, urlencode, urlsplit, urlunsplit)
except ImportError:
    from urllib import quote, splitnport, urlencode
    from urlparse import urlsplit, urlunsplit

## Instruction:
Fix urlencode for Python < 3.0.
## Code After:
__all__ = (
    'quote',
    'splitnport',
    'urlencode',
    'urlsplit',
    'urlunsplit',
)

try:
    from urllib.parse import (
        quote, splitnport, urlencode, urlsplit, urlunsplit)
except ImportError:
    from urllib import quote, splitnport, urlencode as _urlencode
    from urlparse import urlsplit, urlunsplit

    # urlencode did not encode its parameters in Python 2.x so we
    # need to implement that ourselves for compatibility.
    def urlencode(query, doseq=0, safe='', encoding=None, errors=None):

        if encoding is None:
            encoding = 'utf-8'
        if errors is None:
            errors = 'strict'

        def encode_value(v):
            try:
                return codecs.encode(v, encoding, errors)
            except UnicodeError:
                raise
            except (AttributeError, TypeError):
                return str(v)

        try:
            quoted = []
            for name, value in query:
                quoted.append((encode_value(name), encode_value(value)))
            query = quoted
        except UnicodeError:
            raise
        except (TypeError, ValueError) as exc:  # pragma no cover
            # doesn't look like a sequence of tuples, maybe a dict?
            try:
                quoted = {}
                for name, value in query.items():
                    quoted[encode_value(name)] = encode_value(value)
                query = quoted
            except AttributeError:  # not a dictionary either
                pass

        return _urlencode(query, doseq=doseq)
