We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
UnicodeEncodeError with os.path.isdir in NetworkManager.createRequest
Report with `LC_ALL=C`:

```
10:56:42 DEBUG    init       app:process_pos_args:275 Startup URL file:///tmp/people.inf.elte.hu/mersaai/helpvizsgahoz/cpp%20vizsg%ED%B3%83%ED%B2%A1ra/index.html
[...]
10:56:42 ERROR    misc       utils:wrapper:616 Error in qutebrowser.browser.network.networkmanager.NetworkManager.createRequest
Traceback (most recent call last):
  File "/usr/lib/python3.5/site-packages/qutebrowser/utils/utils.py", line 614, in wrapper
    return func(*args, **kwargs)
  File "/usr/lib/python3.5/site-packages/qutebrowser/browser/network/networkmanager.py", line 365, in createRequest
    op, req, outgoing_data)
  File "/usr/lib/python3.5/site-packages/qutebrowser/browser/network/filescheme.py", line 116, in createRequest
    if os.path.isdir(path):
  File "/usr/lib/python3.5/genericpath.py", line 42, in isdir
    st = os.stat(s)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 55-60: ordinal not in range(128)
```


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `qutebrowser/browser/webkit/network/filescheme.py`
Content:
```
1 # vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2 
3 # Copyright 2014-2017 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
4 # Copyright 2015-2017 Antoni Boucher (antoyo) <bouanto@zoho.com>
5 #
6 # This file is part of qutebrowser.
7 #
8 # qutebrowser is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # qutebrowser is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with qutebrowser.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 # pylint complains when using .render() on jinja templates, so we make it shut
22 # up for this whole module.
23 
24 """Handler functions for file:... pages."""
25 
26 import os
27 
28 from qutebrowser.browser.webkit.network import schemehandler, networkreply
29 from qutebrowser.utils import jinja
30 
31 
32 def get_file_list(basedir, all_files, filterfunc):
33     """Get a list of files filtered by a filter function and sorted by name.
34 
35     Args:
36         basedir: The parent directory of all files.
37         all_files: The list of files to filter and sort.
38         filterfunc: The filter function.
39 
40     Return:
41         A list of dicts. Each dict contains the name and absname keys.
42     """
43     items = []
44     for filename in all_files:
45         absname = os.path.join(basedir, filename)
46         if filterfunc(absname):
47             items.append({'name': filename, 'absname': absname})
48     return sorted(items, key=lambda v: v['name'].lower())
49 
50 
51 def is_root(directory):
52     """Check if the directory is the root directory.
53 
54     Args:
55         directory: The directory to check.
56 
57     Return:
58         Whether the directory is a root directory or not.
59     """
60     # If you're curious as why this works:
61     # dirname('/') = '/'
62     # dirname('/home') = '/'
63     # dirname('/home/') = '/home'
64     # dirname('/home/foo') = '/home'
65     # basically, for files (no trailing slash) it removes the file part, and
66     # for directories, it removes the trailing slash, so the only way for this
67     # to be equal is if the directory is the root directory.
68     return os.path.dirname(directory) == directory
69 
70 
71 def parent_dir(directory):
72     """Return the parent directory for the given directory.
73 
74     Args:
75         directory: The path to the directory.
76 
77     Return:
78         The path to the parent directory.
79     """
80     return os.path.normpath(os.path.join(directory, os.pardir))
81 
82 
83 def dirbrowser_html(path):
84     """Get the directory browser web page.
85 
86     Args:
87         path: The directory path.
88 
89     Return:
90         The HTML of the web page.
91     """
92     title = "Browse directory: {}".format(path)
93 
94     if is_root(path):
95         parent = None
96     else:
97         parent = parent_dir(path)
98 
99     try:
100         all_files = os.listdir(path)
101     except OSError as e:
102         html = jinja.render('error.html',
103                             title="Error while reading directory",
104                             url='file:///{}'.format(path), error=str(e))
105         return html.encode('UTF-8', errors='xmlcharrefreplace')
106 
107     files = get_file_list(path, all_files, os.path.isfile)
108     directories = get_file_list(path, all_files, os.path.isdir)
109     html = jinja.render('dirbrowser.html', title=title, url=path,
110                         parent=parent, files=files, directories=directories)
111     return html.encode('UTF-8', errors='xmlcharrefreplace')
112 
113 
114 class FileSchemeHandler(schemehandler.SchemeHandler):
115 
116     """Scheme handler for file: URLs."""
117 
118     def createRequest(self, _op, request, _outgoing_data):
119         """Create a new request.
120 
121         Args:
122              request: const QNetworkRequest & req
123              _op: Operation op
124              _outgoing_data: QIODevice * outgoingData
125 
126         Return:
127             A QNetworkReply for directories, None for files.
128         """
129         path = request.url().toLocalFile()
130         if os.path.isdir(path):
131             data = dirbrowser_html(path)
132             return networkreply.FixedDataNetworkReply(
133                 request, data, 'text/html', self.parent())
134
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/qutebrowser/browser/webkit/network/filescheme.py b/qutebrowser/browser/webkit/network/filescheme.py
--- a/qutebrowser/browser/webkit/network/filescheme.py
+++ b/qutebrowser/browser/webkit/network/filescheme.py
@@ -127,7 +127,10 @@
             A QNetworkReply for directories, None for files.
         """
         path = request.url().toLocalFile()
-        if os.path.isdir(path):
-            data = dirbrowser_html(path)
-            return networkreply.FixedDataNetworkReply(
-                request, data, 'text/html', self.parent())
+        try:
+            if os.path.isdir(path):
+                data = dirbrowser_html(path)
+                return networkreply.FixedDataNetworkReply(
+                    request, data, 'text/html', self.parent())
+        except UnicodeEncodeError:
+            return None

{"golden_diff": "diff --git a/qutebrowser/browser/webkit/network/filescheme.py b/qutebrowser/browser/webkit/network/filescheme.py\n--- a/qutebrowser/browser/webkit/network/filescheme.py\n+++ b/qutebrowser/browser/webkit/network/filescheme.py\n@@ -127,7 +127,10 @@\n             A QNetworkReply for directories, None for files.\n         \"\"\"\n         path = request.url().toLocalFile()\n-        if os.path.isdir(path):\n-            data = dirbrowser_html(path)\n-            return networkreply.FixedDataNetworkReply(\n-                request, data, 'text/html', self.parent())\n+        try:\n+            if os.path.isdir(path):\n+                data = dirbrowser_html(path)\n+                return networkreply.FixedDataNetworkReply(\n+                    request, data, 'text/html', self.parent())\n+        except UnicodeEncodeError:\n+            return None\n", "issue": "UnicodeEncodeError with os.path.isdir in NetworkManager.createRequest\nReport with `LC_ALL=C`:\n\n```\n10:56:42 DEBUG    init       app:process_pos_args:275 Startup URL file:///tmp/people.inf.elte.hu/mersaai/helpvizsgahoz/cpp%20vizsg%ED%B3%83%ED%B2%A1ra/index.html\n[...]\n10:56:42 ERROR    misc       utils:wrapper:616 Error in qutebrowser.browser.network.networkmanager.NetworkManager.createRequest\nTraceback (most recent call last):\n  File \"/usr/lib/python3.5/site-packages/qutebrowser/utils/utils.py\", line 614, in wrapper\n    return func(*args, **kwargs)\n  File \"/usr/lib/python3.5/site-packages/qutebrowser/browser/network/networkmanager.py\", line 365, in createRequest\n    op, req, outgoing_data)\n  File \"/usr/lib/python3.5/site-packages/qutebrowser/browser/network/filescheme.py\", line 116, in createRequest\n    if os.path.isdir(path):\n  File \"/usr/lib/python3.5/genericpath.py\", line 42, in isdir\n    st = os.stat(s)\nUnicodeEncodeError: 'ascii' codec can't encode characters in position 55-60: ordinal not in range(128)\n```\n\n", "before_files": [{"content": "# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:\n\n# Copyright 2014-2017 Florian Bruhin (The Compiler) <mail@qutebrowser.org>\n# Copyright 2015-2017 Antoni Boucher (antoyo) <bouanto@zoho.com>\n#\n# This file is part of qutebrowser.\n#\n# qutebrowser is free software: you can redistribute it and/or modify\n# it under the terms of the GNU General Public License as published by\n# the Free Software Foundation, either version 3 of the License, or\n# (at your option) any later version.\n#\n# qutebrowser is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with qutebrowser.  If not, see <http://www.gnu.org/licenses/>.\n#\n# pylint complains when using .render() on jinja templates, so we make it shut\n# up for this whole module.\n\n\"\"\"Handler functions for file:... pages.\"\"\"\n\nimport os\n\nfrom qutebrowser.browser.webkit.network import schemehandler, networkreply\nfrom qutebrowser.utils import jinja\n\n\ndef get_file_list(basedir, all_files, filterfunc):\n    \"\"\"Get a list of files filtered by a filter function and sorted by name.\n\n    Args:\n        basedir: The parent directory of all files.\n        all_files: The list of files to filter and sort.\n        filterfunc: The filter function.\n\n    Return:\n        A list of dicts. Each dict contains the name and absname keys.\n    \"\"\"\n    items = []\n    for filename in all_files:\n        absname = os.path.join(basedir, filename)\n        if filterfunc(absname):\n            items.append({'name': filename, 'absname': absname})\n    return sorted(items, key=lambda v: v['name'].lower())\n\n\ndef is_root(directory):\n    \"\"\"Check if the directory is the root directory.\n\n    Args:\n        directory: The directory to check.\n\n    Return:\n        Whether the directory is a root directory or not.\n    \"\"\"\n    # If you're curious as why this works:\n    # dirname('/') = '/'\n    # dirname('/home') = '/'\n    # dirname('/home/') = '/home'\n    # dirname('/home/foo') = '/home'\n    # basically, for files (no trailing slash) it removes the file part, and\n    # for directories, it removes the trailing slash, so the only way for this\n    # to be equal is if the directory is the root directory.\n    return os.path.dirname(directory) == directory\n\n\ndef parent_dir(directory):\n    \"\"\"Return the parent directory for the given directory.\n\n    Args:\n        directory: The path to the directory.\n\n    Return:\n        The path to the parent directory.\n    \"\"\"\n    return os.path.normpath(os.path.join(directory, os.pardir))\n\n\ndef dirbrowser_html(path):\n    \"\"\"Get the directory browser web page.\n\n    Args:\n        path: The directory path.\n\n    Return:\n        The HTML of the web page.\n    \"\"\"\n    title = \"Browse directory: {}\".format(path)\n\n    if is_root(path):\n        parent = None\n    else:\n        parent = parent_dir(path)\n\n    try:\n        all_files = os.listdir(path)\n    except OSError as e:\n        html = jinja.render('error.html',\n                            title=\"Error while reading directory\",\n                            url='file:///{}'.format(path), error=str(e))\n        return html.encode('UTF-8', errors='xmlcharrefreplace')\n\n    files = get_file_list(path, all_files, os.path.isfile)\n    directories = get_file_list(path, all_files, os.path.isdir)\n    html = jinja.render('dirbrowser.html', title=title, url=path,\n                        parent=parent, files=files, directories=directories)\n    return html.encode('UTF-8', errors='xmlcharrefreplace')\n\n\nclass FileSchemeHandler(schemehandler.SchemeHandler):\n\n    \"\"\"Scheme handler for file: URLs.\"\"\"\n\n    def createRequest(self, _op, request, _outgoing_data):\n        \"\"\"Create a new request.\n\n        Args:\n             request: const QNetworkRequest & req\n             _op: Operation op\n             _outgoing_data: QIODevice * outgoingData\n\n        Return:\n            A QNetworkReply for directories, None for files.\n        \"\"\"\n        path = request.url().toLocalFile()\n        if os.path.isdir(path):\n            data = dirbrowser_html(path)\n            return networkreply.FixedDataNetworkReply(\n                request, data, 'text/html', self.parent())\n", "path": "qutebrowser/browser/webkit/network/filescheme.py"}], "after_files": [{"content": "# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:\n\n# Copyright 2014-2017 Florian Bruhin (The Compiler) <mail@qutebrowser.org>\n# Copyright 2015-2017 Antoni Boucher (antoyo) <bouanto@zoho.com>\n#\n# This file is part of qutebrowser.\n#\n# qutebrowser is free software: you can redistribute it and/or modify\n# it under the terms of the GNU General Public License as published by\n# the Free Software Foundation, either version 3 of the License, or\n# (at your option) any later version.\n#\n# qutebrowser is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with qutebrowser.  If not, see <http://www.gnu.org/licenses/>.\n#\n# pylint complains when using .render() on jinja templates, so we make it shut\n# up for this whole module.\n\n\"\"\"Handler functions for file:... pages.\"\"\"\n\nimport os\n\nfrom qutebrowser.browser.webkit.network import schemehandler, networkreply\nfrom qutebrowser.utils import jinja\n\n\ndef get_file_list(basedir, all_files, filterfunc):\n    \"\"\"Get a list of files filtered by a filter function and sorted by name.\n\n    Args:\n        basedir: The parent directory of all files.\n        all_files: The list of files to filter and sort.\n        filterfunc: The filter function.\n\n    Return:\n        A list of dicts. Each dict contains the name and absname keys.\n    \"\"\"\n    items = []\n    for filename in all_files:\n        absname = os.path.join(basedir, filename)\n        if filterfunc(absname):\n            items.append({'name': filename, 'absname': absname})\n    return sorted(items, key=lambda v: v['name'].lower())\n\n\ndef is_root(directory):\n    \"\"\"Check if the directory is the root directory.\n\n    Args:\n        directory: The directory to check.\n\n    Return:\n        Whether the directory is a root directory or not.\n    \"\"\"\n    # If you're curious as why this works:\n    # dirname('/') = '/'\n    # dirname('/home') = '/'\n    # dirname('/home/') = '/home'\n    # dirname('/home/foo') = '/home'\n    # basically, for files (no trailing slash) it removes the file part, and\n    # for directories, it removes the trailing slash, so the only way for this\n    # to be equal is if the directory is the root directory.\n    return os.path.dirname(directory) == directory\n\n\ndef parent_dir(directory):\n    \"\"\"Return the parent directory for the given directory.\n\n    Args:\n        directory: The path to the directory.\n\n    Return:\n        The path to the parent directory.\n    \"\"\"\n    return os.path.normpath(os.path.join(directory, os.pardir))\n\n\ndef dirbrowser_html(path):\n    \"\"\"Get the directory browser web page.\n\n    Args:\n        path: The directory path.\n\n    Return:\n        The HTML of the web page.\n    \"\"\"\n    title = \"Browse directory: {}\".format(path)\n\n    if is_root(path):\n        parent = None\n    else:\n        parent = parent_dir(path)\n\n    try:\n        all_files = os.listdir(path)\n    except OSError as e:\n        html = jinja.render('error.html',\n                            title=\"Error while reading directory\",\n                            url='file:///{}'.format(path), error=str(e))\n        return html.encode('UTF-8', errors='xmlcharrefreplace')\n\n    files = get_file_list(path, all_files, os.path.isfile)\n    directories = get_file_list(path, all_files, os.path.isdir)\n    html = jinja.render('dirbrowser.html', title=title, url=path,\n                        parent=parent, files=files, directories=directories)\n    return html.encode('UTF-8', errors='xmlcharrefreplace')\n\n\nclass FileSchemeHandler(schemehandler.SchemeHandler):\n\n    \"\"\"Scheme handler for file: URLs.\"\"\"\n\n    def createRequest(self, _op, request, _outgoing_data):\n        \"\"\"Create a new request.\n\n        Args:\n             request: const QNetworkRequest & req\n             _op: Operation op\n             _outgoing_data: QIODevice * outgoingData\n\n        Return:\n            A QNetworkReply for directories, None for files.\n        \"\"\"\n        path = request.url().toLocalFile()\n        try:\n            if os.path.isdir(path):\n                data = dirbrowser_html(path)\n                return networkreply.FixedDataNetworkReply(\n                    request, data, 'text/html', self.parent())\n        except UnicodeEncodeError:\n            return None\n", "path": "qutebrowser/browser/webkit/network/filescheme.py"}]}