# Buggy code
def prepare_url(self, url, params):
    """Prepares the given HTTP URL."""
    #: Accept objects that have string representations.
    #: We're unable to blindy call unicode/str functions
    #: as this will include the bytestring indicator (b'')
    #: on python 3.x.
    #: https://github.com/kennethreitz/requests/pull/2238
    if isinstance(url, bytes):
        url = url.decode("utf8")
    else:
        url = unicode(url) if is_py2 else str(url)

    # Don't do any URL preparation for non-HTTP schemes like `mailto`,
    # `data` etc to work around exceptions from `url_parse`, which
    # handles RFC 3986 only.
    if ":" in url and not url.lower().startswith("http"):
        self.url = url
        return

    # Support for unicode domain names and paths.
    try:
        scheme, auth, host, port, path, query, fragment = parse_url(url)
    except LocationParseError as e:
        raise InvalidURL(*e.args)

    if not scheme:
        raise MissingSchema(
            "Invalid URL {0!r}: No schema supplied. "
            "Perhaps you meant http://{0}?".format(url)
        )

    if not host:
        raise InvalidURL("Invalid URL %r: No host supplied" % url)

    # Only want to apply IDNA to the hostname
    try:
        host = host.encode("idna").decode("utf-8")
    except UnicodeError:
        raise InvalidURL("URL has an invalid label.")

    # Carefully reconstruct the network location
    netloc = auth or ""
    if netloc:
        netloc += "@"
    netloc += host
    if port:
        netloc += ":" + str(port)

    # Bare domains aren't valid URLs.
    if not path:
        path = "/"

    if is_py2:
        if isinstance(scheme, str):
            scheme = scheme.encode("utf-8")
        if isinstance(netloc, str):
            netloc = netloc.encode("utf-8")
        if isinstance(path, str):
            path = path.encode("utf-8")
        if isinstance(query, str):
            query = query.encode("utf-8")
        if isinstance(fragment, str):
            fragment = fragment.encode("utf-8")

    enc_params = self._encode_params(params)
    if enc_params:
        if query:
            query = "%s&%s" % (query, enc_params)
        else:
            query = enc_params

    url = requote_uri(urlunparse([scheme, netloc, path, None, query, fragment]))
    self.url = url


# Traceback (UnicodeEncodeError)
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
<ipython-input-3-bf1281984b3a> in <module>()
----> 1 requests.get(url)

/Library/Python/2.7/site-packages/requests-2.6.0-py2.7.egg/requests/api.pyc in get(url, **kwargs)
66
67     kwargs.setdefault('allow_redirects', True)
---> 68     return request('get', url, **kwargs)
69
70

/Library/Python/2.7/site-packages/requests-2.6.0-py2.7.egg/requests/api.pyc in request(method, url, **kwargs)
48
49     session = sessions.Session()
---> 50     response = session.request(method=method, url=url, **kwargs)
51     # By explicitly closing the session, we avoid leaving sockets open which
52     # can trigger a ResourceWarning in some cases, and look like a memory leak

/Library/Python/2.7/site-packages/requests-2.6.0-py2.7.egg/requests/sessions.pyc in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
448             hooks = hooks,
449         )
--> 450         prep = self.prepare_request(req)
451
452         proxies = proxies or {}

/Library/Python/2.7/site-packages/requests-2.6.0-py2.7.egg/requests/sessions.pyc in prepare_request(self, request)
379             auth=merge_setting(auth, self.auth),
380             cookies=merged_cookies,
--> 381             hooks=merge_hooks(request.hooks, self.hooks),
382         )
383         return p

/Library/Python/2.7/site-packages/requests-2.6.0-py2.7.egg/requests/models.pyc in prepare(self, method, url, headers, files, data, params, auth, cookies, hooks, json)
302
303         self.prepare_method(method)
--> 304         self.prepare_url(url, params)
305         self.prepare_headers(headers)
306         self.prepare_cookies(cookies)

/Library/Python/2.7/site-packages/requests-2.6.0-py2.7.egg/requests/models.pyc in prepare_url(self, url, params)
359         if not scheme:
360             raise MissingSchema("Invalid URL {0!r}: No schema supplied. "
--> 361                                 "Perhaps you meant http://{0}?".format(url))
362
363         if not host:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 1: ordinal not in range(128)

# Fixed code
def prepare_url(self, url, params):
    """Prepares the given HTTP URL."""
    #: Accept objects that have string representations.
    #: We're unable to blindy call unicode/str functions
    #: as this will include the bytestring indicator (b'')
    #: on python 3.x.
    #: https://github.com/kennethreitz/requests/pull/2238
    if isinstance(url, bytes):
        url = url.decode("utf8")
    else:
        url = unicode(url) if is_py2 else str(url)

    # Don't do any URL preparation for non-HTTP schemes like `mailto`,
    # `data` etc to work around exceptions from `url_parse`, which
    # handles RFC 3986 only.
    if ":" in url and not url.lower().startswith("http"):
        self.url = url
        return

    # Support for unicode domain names and paths.
    try:
        scheme, auth, host, port, path, query, fragment = parse_url(url)
    except LocationParseError as e:
        raise InvalidURL(*e.args)

    if not scheme:
        raise MissingSchema(
            "Invalid URL {0!r}: No schema supplied. "
            "Perhaps you meant http://{0}?".format(to_native_string(url, "utf8"))
        )

    if not host:
        raise InvalidURL("Invalid URL %r: No host supplied" % url)

    # Only want to apply IDNA to the hostname
    try:
        host = host.encode("idna").decode("utf-8")
    except UnicodeError:
        raise InvalidURL("URL has an invalid label.")

    # Carefully reconstruct the network location
    netloc = auth or ""
    if netloc:
        netloc += "@"
    netloc += host
    if port:
        netloc += ":" + str(port)

    # Bare domains aren't valid URLs.
    if not path:
        path = "/"

    if is_py2:
        if isinstance(scheme, str):
            scheme = scheme.encode("utf-8")
        if isinstance(netloc, str):
            netloc = netloc.encode("utf-8")
        if isinstance(path, str):
            path = path.encode("utf-8")
        if isinstance(query, str):
            query = query.encode("utf-8")
        if isinstance(fragment, str):
            fragment = fragment.encode("utf-8")

    enc_params = self._encode_params(params)
    if enc_params:
        if query:
            query = "%s&%s" % (query, enc_params)
        else:
            query = enc_params

    url = requote_uri(urlunparse([scheme, netloc, path, None, query, fragment]))
    self.url = url