# Buggy code
def data_for_url(url):
    """Get the data to show for the given URL.

    Args:
        url: The QUrl to show.

    Return:
        A (mimetype, data) tuple.
    """
    # normalize path and strip redundant trailing slashes
    norm_url = url.adjusted(QUrl.NormalizePathSegments | QUrl.StripTrailingSlash)
    if norm_url != url:
        raise Redirect(norm_url)

    path = url.path()
    host = url.host()
    query = urlutils.query_string(url)
    # A url like "qute:foo" is split as "scheme:path", not "scheme:host".
    log.misc.debug(
        "url: {}, path: {}, host {}".format(url.toDisplayString(), path, host)
    )
    if not path or not host:
        new_url = QUrl()
        new_url.setScheme("qute")
        # When path is absent, e.g. qute://help (with no trailing slash)
        if host:
            new_url.setHost(host)
        # When host is absent, e.g. qute:help
        else:
            new_url.setHost(path)

        new_url.setPath("/")
        if query:
            new_url.setQuery(query)
        if new_url.host():  # path was a valid host
            raise Redirect(new_url)

    try:
        handler = _HANDLERS[host]
    except KeyError:
        raise NoHandlerFound(url)

    try:
        mimetype, data = handler(url)
    except OSError as e:
        # FIXME:qtwebengine how to handle this?
        raise QuteSchemeOSError(e)
    except QuteSchemeError as e:
        raise

    assert mimetype is not None, url
    if mimetype == "text/html" and isinstance(data, str):
        # We let handlers return HTML as text
        data = data.encode("utf-8", errors="xmlcharrefreplace")

    return mimetype, data


# Traceback (AssertionError)
commands   command:run:489 command called: navigate ['up', '-t']
commands   command:run:504 Calling qutebrowser.browser.commands.CommandDispatcher.navigate(<qutebrowser.browser.commands.CommandDispatcher>, 'up', True, False, False, 1)
webview    tabbedbrowser:tabopen:445 Creating new tab with URL PyQt5.QtCore.QUrl('qute://help/img/cheatsheet-big.png/..'), background False, related True, idx None
[...]
webview    webview:acceptNavigationRequest:296 navigation request: url qute://help/img/cheatsheet-big.png/.., type NavigationTypeTyped, is_main_frame True
misc       webenginequtescheme:requestStarted:50 Got request for qute://help/img/cheatsheet-big.png/..
misc       qutescheme:data_for_url:143 url: qute://help/img/cheatsheet-big.png/.., path: /img/cheatsheet-big.png/.., host help
misc       crashsignal:exception_hook:216 Uncaught exception
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/qutebrowser/browser/webengine/webenginequtescheme.py", line 52, in requestStarted
mimetype, data = qutescheme.data_for_url(url)
File "/usr/lib/python3.6/site-packages/qutebrowser/browser/qutescheme.py", line 158, in data_for_url
mimetype, data = handler(url)
File "/usr/lib/python3.6/site-packages/qutebrowser/browser/qutescheme.py", line 118, in wrapper
return self._function(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/qutebrowser/browser/qutescheme.py", line 329, in qute_help
assert mimetype is not None, url
AssertionError: PyQt5.QtCore.QUrl('qute://help/img/cheatsheet-big.png/..')

# Fixed code
def data_for_url(url):
    """Get the data to show for the given URL.

    Args:
        url: The QUrl to show.

    Return:
        A (mimetype, data) tuple.
    """
    norm_url = url.adjusted(QUrl.NormalizePathSegments | QUrl.StripTrailingSlash)
    if norm_url != url:
        raise Redirect(norm_url)

    path = url.path()
    host = url.host()
    query = urlutils.query_string(url)
    # A url like "qute:foo" is split as "scheme:path", not "scheme:host".
    log.misc.debug(
        "url: {}, path: {}, host {}".format(url.toDisplayString(), path, host)
    )
    if not path or not host:
        new_url = QUrl()
        new_url.setScheme("qute")
        # When path is absent, e.g. qute://help (with no trailing slash)
        if host:
            new_url.setHost(host)
        # When host is absent, e.g. qute:help
        else:
            new_url.setHost(path)

        new_url.setPath("/")
        if query:
            new_url.setQuery(query)
        if new_url.host():  # path was a valid host
            raise Redirect(new_url)

    try:
        handler = _HANDLERS[host]
    except KeyError:
        raise NoHandlerFound(url)

    try:
        mimetype, data = handler(url)
    except OSError as e:
        # FIXME:qtwebengine how to handle this?
        raise QuteSchemeOSError(e)
    except QuteSchemeError as e:
        raise

    assert mimetype is not None, url
    if mimetype == "text/html" and isinstance(data, str):
        # We let handlers return HTML as text
        data = data.encode("utf-8", errors="xmlcharrefreplace")

    return mimetype, data