We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Mitmproxy crashes, when we try to write a value from file into grideditor cell
##### Steps to reproduce the problem:

1. Create a file. Put inside:
```
abc
```
2. Run mitmproxy.
3. Press `n` -> `Enter` -> `Enter` -> `e` -> `cookies`. You will get into `cookies` grideditor.
4. Press `a` -> `Esc` -> `r` (or `R`, also relevant for it).
5. Input the path to the file with `abc`: `console.grideditor.load` `~/your_file_with_abc` -> `Enter`.

You will see:
```
Traceback (most recent call last):
  File "/home/kajoj/Mitmproxy/mitmproxy/mitmproxy/tools/console/master.py", line 216, in run
    self.loop.run()
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 278, in run
    self._run()
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 376, in _run
    self.event_loop.run()
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 682, in run
    self._loop()
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 710, in _loop
    self._entering_idle()
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 671, in _entering_idle
    callback()
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 564, in entering_idle
    self.draw_screen()
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py", line 578, in draw_screen
    canvas = self._topmost_widget.render(self.screen_size, focus=True)
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/widget.py", line 141, in cached_render
    canv = fn(self, size, focus=focus)
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/container.py", line 1083, in render
    focus and self.focus_part == 'body')
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/widget.py", line 141, in cached_render
    canv = fn(self, size, focus=focus)
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/decoration.py", line 225, in render
    canv = self._original_widget.render(size, focus=focus)
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/widget.py", line 141, in cached_render
    canv = fn(self, size, focus=focus)
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/container.py", line 1083, in render
    focus and self.focus_part == 'body')
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/widget.py", line 141, in cached_render
    canv = fn(self, size, focus=focus)
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/widget.py", line 1750, in render
    canv = get_delegate(self).render(size, focus=focus)
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/widget.py", line 141, in cached_render
    canv = fn(self, size, focus=focus)
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/widget.py", line 1750, in render
    canv = get_delegate(self).render(size, focus=focus)
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/widget.py", line 141, in cached_render
    canv = fn(self, size, focus=focus)
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/container.py", line 1083, in render
    focus and self.focus_part == 'body')
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/widget.py", line 141, in cached_render
    canv = fn(self, size, focus=focus)
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/listbox.py", line 455, in render
    (maxcol, maxrow), focus=focus)
  File "/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/listbox.py", line 340, in calculate_visible
    focus_widget, focus_pos = self.body.get_focus()
  File "/home/kajoj/Mitmproxy/mitmproxy/mitmproxy/tools/console/grideditor/base.py", line 227, in get_focus
    self.lst[self.focus]
  File "/home/kajoj/Mitmproxy/mitmproxy/mitmproxy/tools/console/grideditor/base.py", line 83, in __init__
    w = self.editor.columns[i].Display(v)
  File "/home/kajoj/Mitmproxy/mitmproxy/mitmproxy/tools/console/grideditor/col_text.py", line 18, in Display
    return TDisplay(data, self.encoding_args)
  File "/home/kajoj/Mitmproxy/mitmproxy/mitmproxy/tools/console/grideditor/col_text.py", line 31, in __init__
    super().__init__(data.encode(*self.encoding_args))
AttributeError: 'bytes' object has no attribute 'encode'
```

##### Any other comments? What have you tried so far?

This bug is relevant for cookies, form, path, query and set-cookies grideditors.
I didn't check carefully, but it seems to be relevant for v2.0.2 as well.


##### System information

Mitmproxy: 3.0.0.dev113 (commit 93425d4) 
Python:    3.5.2
OpenSSL:   OpenSSL 1.1.0g  2 Nov 2017
Platform:  Linux-4.4.0-112-generic-x86_64-with-Ubuntu-16.04-xenial



--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `mitmproxy/tools/console/grideditor/col_text.py`
Content:
```
1 """
2 Welcome to the encoding dance!
3 
4 In a nutshell, text columns are actually a proxy class for byte columns,
5 which just encode/decodes contents.
6 """
7 
8 from mitmproxy.tools.console import signals
9 from mitmproxy.tools.console.grideditor import col_bytes
10 
11 
12 class Column(col_bytes.Column):
13     def __init__(self, heading, encoding="utf8", errors="surrogateescape"):
14         super().__init__(heading)
15         self.encoding_args = encoding, errors
16 
17     def Display(self, data):
18         return TDisplay(data, self.encoding_args)
19 
20     def Edit(self, data):
21         return TEdit(data, self.encoding_args)
22 
23     def blank(self):
24         return ""
25 
26 
27 # This is the same for both edit and display.
28 class EncodingMixin:
29     def __init__(self, data, encoding_args):
30         self.encoding_args = encoding_args
31         super().__init__(data.encode(*self.encoding_args))
32 
33     def get_data(self):
34         data = super().get_data()
35         try:
36             return data.decode(*self.encoding_args)
37         except ValueError:
38             signals.status_message.send(
39                 self,
40                 message="Invalid encoding.",
41                 expire=1000
42             )
43             raise
44 
45 
46 # urwid forces a different name for a subclass.
47 class TDisplay(EncodingMixin, col_bytes.Display):
48     pass
49 
50 
51 class TEdit(EncodingMixin, col_bytes.Edit):
52     pass
53
```
--- 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/mitmproxy/tools/console/grideditor/col_text.py b/mitmproxy/tools/console/grideditor/col_text.py
--- a/mitmproxy/tools/console/grideditor/col_text.py
+++ b/mitmproxy/tools/console/grideditor/col_text.py
@@ -28,7 +28,7 @@
 class EncodingMixin:
     def __init__(self, data, encoding_args):
         self.encoding_args = encoding_args
-        super().__init__(data.encode(*self.encoding_args))
+        super().__init__(data.__str__().encode(*self.encoding_args))
 
     def get_data(self):
         data = super().get_data()

{"golden_diff": "diff --git a/mitmproxy/tools/console/grideditor/col_text.py b/mitmproxy/tools/console/grideditor/col_text.py\n--- a/mitmproxy/tools/console/grideditor/col_text.py\n+++ b/mitmproxy/tools/console/grideditor/col_text.py\n@@ -28,7 +28,7 @@\n class EncodingMixin:\n     def __init__(self, data, encoding_args):\n         self.encoding_args = encoding_args\n-        super().__init__(data.encode(*self.encoding_args))\n+        super().__init__(data.__str__().encode(*self.encoding_args))\n \n     def get_data(self):\n         data = super().get_data()\n", "issue": "Mitmproxy crashes, when we try to write a value from file into grideditor cell\n##### Steps to reproduce the problem:\r\n\r\n1. Create a file. Put inside:\r\n```\r\nabc\r\n```\r\n2. Run mitmproxy.\r\n3. Press `n` -> `Enter` -> `Enter` -> `e` -> `cookies`. You will get into `cookies` grideditor.\r\n4. Press `a` -> `Esc` -> `r` (or `R`, also relevant for it).\r\n5. Input the path to the file with `abc`: `console.grideditor.load` `~/your_file_with_abc` -> `Enter`.\r\n\r\nYou will see:\r\n```\r\nTraceback (most recent call last):\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/mitmproxy/tools/console/master.py\", line 216, in run\r\n    self.loop.run()\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py\", line 278, in run\r\n    self._run()\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py\", line 376, in _run\r\n    self.event_loop.run()\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py\", line 682, in run\r\n    self._loop()\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py\", line 710, in _loop\r\n    self._entering_idle()\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py\", line 671, in _entering_idle\r\n    callback()\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py\", line 564, in entering_idle\r\n    self.draw_screen()\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/main_loop.py\", line 578, in draw_screen\r\n    canvas = self._topmost_widget.render(self.screen_size, focus=True)\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/widget.py\", line 141, in cached_render\r\n    canv = fn(self, size, focus=focus)\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/container.py\", line 1083, in render\r\n    focus and self.focus_part == 'body')\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/widget.py\", line 141, in cached_render\r\n    canv = fn(self, size, focus=focus)\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/decoration.py\", line 225, in render\r\n    canv = self._original_widget.render(size, focus=focus)\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/widget.py\", line 141, in cached_render\r\n    canv = fn(self, size, focus=focus)\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/container.py\", line 1083, in render\r\n    focus and self.focus_part == 'body')\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/widget.py\", line 141, in cached_render\r\n    canv = fn(self, size, focus=focus)\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/widget.py\", line 1750, in render\r\n    canv = get_delegate(self).render(size, focus=focus)\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/widget.py\", line 141, in cached_render\r\n    canv = fn(self, size, focus=focus)\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/widget.py\", line 1750, in render\r\n    canv = get_delegate(self).render(size, focus=focus)\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/widget.py\", line 141, in cached_render\r\n    canv = fn(self, size, focus=focus)\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/container.py\", line 1083, in render\r\n    focus and self.focus_part == 'body')\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/widget.py\", line 141, in cached_render\r\n    canv = fn(self, size, focus=focus)\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/listbox.py\", line 455, in render\r\n    (maxcol, maxrow), focus=focus)\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/venv/lib/python3.5/site-packages/urwid/listbox.py\", line 340, in calculate_visible\r\n    focus_widget, focus_pos = self.body.get_focus()\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/mitmproxy/tools/console/grideditor/base.py\", line 227, in get_focus\r\n    self.lst[self.focus]\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/mitmproxy/tools/console/grideditor/base.py\", line 83, in __init__\r\n    w = self.editor.columns[i].Display(v)\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/mitmproxy/tools/console/grideditor/col_text.py\", line 18, in Display\r\n    return TDisplay(data, self.encoding_args)\r\n  File \"/home/kajoj/Mitmproxy/mitmproxy/mitmproxy/tools/console/grideditor/col_text.py\", line 31, in __init__\r\n    super().__init__(data.encode(*self.encoding_args))\r\nAttributeError: 'bytes' object has no attribute 'encode'\r\n```\r\n\r\n##### Any other comments? What have you tried so far?\r\n\r\nThis bug is relevant for cookies, form, path, query and set-cookies grideditors.\r\nI didn't check carefully, but it seems to be relevant for v2.0.2 as well.\r\n\r\n\r\n##### System information\r\n\r\nMitmproxy: 3.0.0.dev113 (commit 93425d4) \r\nPython:    3.5.2\r\nOpenSSL:   OpenSSL 1.1.0g  2 Nov 2017\r\nPlatform:  Linux-4.4.0-112-generic-x86_64-with-Ubuntu-16.04-xenial\r\n\r\n\n", "before_files": [{"content": "\"\"\"\nWelcome to the encoding dance!\n\nIn a nutshell, text columns are actually a proxy class for byte columns,\nwhich just encode/decodes contents.\n\"\"\"\n\nfrom mitmproxy.tools.console import signals\nfrom mitmproxy.tools.console.grideditor import col_bytes\n\n\nclass Column(col_bytes.Column):\n    def __init__(self, heading, encoding=\"utf8\", errors=\"surrogateescape\"):\n        super().__init__(heading)\n        self.encoding_args = encoding, errors\n\n    def Display(self, data):\n        return TDisplay(data, self.encoding_args)\n\n    def Edit(self, data):\n        return TEdit(data, self.encoding_args)\n\n    def blank(self):\n        return \"\"\n\n\n# This is the same for both edit and display.\nclass EncodingMixin:\n    def __init__(self, data, encoding_args):\n        self.encoding_args = encoding_args\n        super().__init__(data.encode(*self.encoding_args))\n\n    def get_data(self):\n        data = super().get_data()\n        try:\n            return data.decode(*self.encoding_args)\n        except ValueError:\n            signals.status_message.send(\n                self,\n                message=\"Invalid encoding.\",\n                expire=1000\n            )\n            raise\n\n\n# urwid forces a different name for a subclass.\nclass TDisplay(EncodingMixin, col_bytes.Display):\n    pass\n\n\nclass TEdit(EncodingMixin, col_bytes.Edit):\n    pass\n", "path": "mitmproxy/tools/console/grideditor/col_text.py"}], "after_files": [{"content": "\"\"\"\nWelcome to the encoding dance!\n\nIn a nutshell, text columns are actually a proxy class for byte columns,\nwhich just encode/decodes contents.\n\"\"\"\n\nfrom mitmproxy.tools.console import signals\nfrom mitmproxy.tools.console.grideditor import col_bytes\n\n\nclass Column(col_bytes.Column):\n    def __init__(self, heading, encoding=\"utf8\", errors=\"surrogateescape\"):\n        super().__init__(heading)\n        self.encoding_args = encoding, errors\n\n    def Display(self, data):\n        return TDisplay(data, self.encoding_args)\n\n    def Edit(self, data):\n        return TEdit(data, self.encoding_args)\n\n    def blank(self):\n        return \"\"\n\n\n# This is the same for both edit and display.\nclass EncodingMixin:\n    def __init__(self, data, encoding_args):\n        self.encoding_args = encoding_args\n        super().__init__(data.__str__().encode(*self.encoding_args))\n\n    def get_data(self):\n        data = super().get_data()\n        try:\n            return data.decode(*self.encoding_args)\n        except ValueError:\n            signals.status_message.send(\n                self,\n                message=\"Invalid encoding.\",\n                expire=1000\n            )\n            raise\n\n\n# urwid forces a different name for a subclass.\nclass TDisplay(EncodingMixin, col_bytes.Display):\n    pass\n\n\nclass TEdit(EncodingMixin, col_bytes.Edit):\n    pass\n", "path": "mitmproxy/tools/console/grideditor/col_text.py"}]}