# Buggy code
def keypress(self, size, key):
    key = common.shortcuts(key)
    if key == "A":
        for f in self.master.view:
            if f.intercepted:
                f.resume(self.master)
        signals.flowlist_change.send(self)
    elif key == "z":
        self.master.view.clear()
    elif key == "e":
        self.master.toggle_eventlog()
    elif key == "g":
        if len(self.master.view):
            self.master.view.focus.index = 0
    elif key == "G":
        if len(self.master.view):
            self.master.view.focus.index = len(self.master.view) - 1
    elif key == "f":
        signals.status_prompt.send(
            prompt="Filter View",
            text=self.master.options.filter,
            callback=self.master.options.setter("filter"),
        )
    elif key == "L":
        signals.status_prompt_path.send(
            self, prompt="Load flows", callback=self.master.load_flows_callback
        )
    elif key == "n":
        signals.status_prompt_onekey.send(
            prompt="Method", keys=common.METHOD_OPTIONS, callback=self.get_method
        )
    elif key == "o":
        orders = [(i[1], i[0]) for i in view.orders]
        lookup = dict([(i[0], i[1]) for i in view.orders])

        def change_order(k):
            self.master.options.order = lookup[k]

        signals.status_prompt_onekey.send(
            prompt="Order", keys=orders, callback=change_order
        )
    elif key == "F":
        o = self.master.options
        o.focus_follow = not o.focus_follow
    elif key == "v":
        val = not self.master.options.order_reversed
        self.master.options.order_reversed = val
    elif key == "W":
        if self.master.options.streamfile:
            self.master.options.streamfile = None
        else:
            signals.status_prompt_path.send(
                self,
                prompt="Stream flows to",
                callback=lambda path: self.master.options.update(streamfile=path),
            )
    else:
        return urwid.ListBox.keypress(self, size, key)


# Traceback (ValueError)
Traceback (most recent call last):
File "/usr/local/Cellar/mitmproxy/0.18.2_1/libexec/lib/python2.7/site-packages/mitmproxy/console/master.py", line 482, in run
self.loop.run()
File "/usr/local/Cellar/mitmproxy/0.18.2_1/libexec/lib/python2.7/site-packages/urwid/main_loop.py", line 278, in run
self._run()
File "/usr/local/Cellar/mitmproxy/0.18.2_1/libexec/lib/python2.7/site-packages/urwid/main_loop.py", line 376, in _run
self.event_loop.run()
File "/usr/local/Cellar/mitmproxy/0.18.2_1/libexec/lib/python2.7/site-packages/urwid/main_loop.py", line 682, in run
self._loop()
File "/usr/local/Cellar/mitmproxy/0.18.2_1/libexec/lib/python2.7/site-packages/urwid/main_loop.py", line 719, in _loop
self._watch_files[fd]()
File "/usr/local/Cellar/mitmproxy/0.18.2_1/libexec/lib/python2.7/site-packages/urwid/raw_display.py", line 393, in <lambda>
event_loop, callback, self.get_available_raw_input())
File "/usr/local/Cellar/mitmproxy/0.18.2_1/libexec/lib/python2.7/site-packages/urwid/raw_display.py", line 493, in parse_input
callback(processed, processed_codes)
File "/usr/local/Cellar/mitmproxy/0.18.2_1/libexec/lib/python2.7/site-packages/urwid/main_loop.py", line 403, in _update
self.process_input(keys)
File "/usr/local/Cellar/mitmproxy/0.18.2_1/libexec/lib/python2.7/site-packages/urwid/main_loop.py", line 503, in process_input
k = self._topmost_widget.keypress(self.screen_size, k)
File "/usr/local/Cellar/mitmproxy/0.18.2_1/libexec/lib/python2.7/site-packages/mitmproxy/console/window.py", line 86, in keypress
k = super(self.__class__, self).keypress(size, k)
File "/usr/local/Cellar/mitmproxy/0.18.2_1/libexec/lib/python2.7/site-packages/urwid/container.py", line 1128, in keypress
return self.body.keypress( (maxcol, remaining), key )
File "/usr/local/Cellar/mitmproxy/0.18.2_1/libexec/lib/python2.7/site-packages/mitmproxy/console/flowlist.py", line 385, in keypress
return urwid.ListBox.keypress(self, size, key)
File "/usr/local/Cellar/mitmproxy/0.18.2_1/libexec/lib/python2.7/site-packages/urwid/listbox.py", line 987, in keypress
key = focus_widget.keypress((maxcol,),key)
File "/usr/local/Cellar/mitmproxy/0.18.2_1/libexec/lib/python2.7/site-packages/mitmproxy/console/flowlist.py", line 179, in keypress
self.state.disable_marked_filter()
File "/usr/local/Cellar/mitmproxy/0.18.2_1/libexec/lib/python2.7/site-packages/mitmproxy/console/master.py", line 183, in disable_marked_filter
self.set_focus_flow(nearest_marked)
File "/usr/local/Cellar/mitmproxy/0.18.2_1/libexec/lib/python2.7/site-packages/mitmproxy/console/master.py", line 107, in set_focus_flow
self.set_focus(self.view.index(f))
File "/usr/local/Cellar/mitmproxy/0.18.2_1/libexec/lib/python2.7/site-packages/mitmproxy/flow/state.py", line 36, in index
return self._list.index(f)
ValueError: None is not in list

mitmproxy has crashed!
Please lodge a bug report at:
https://github.com/mitmproxy/mitmproxy
Shutting down...

# Fixed code
def keypress(self, size, key):
    key = common.shortcuts(key)
    if key == "A":
        for f in self.master.view:
            if f.intercepted:
                f.resume(self.master)
        signals.flowlist_change.send(self)
    elif key == "z":
        self.master.view.clear()
    elif key == "e":
        self.master.toggle_eventlog()
    elif key == "g":
        if len(self.master.view):
            self.master.view.focus.index = 0
    elif key == "G":
        if len(self.master.view):
            self.master.view.focus.index = len(self.master.view) - 1
    elif key == "f":
        signals.status_prompt.send(
            prompt="Filter View",
            text=self.master.options.filter,
            callback=self.master.options.setter("filter"),
        )
    elif key == "L":
        signals.status_prompt_path.send(
            self, prompt="Load flows", callback=self.master.load_flows_callback
        )
    elif key == "M":
        self.master.view.toggle_marked()
    elif key == "n":
        signals.status_prompt_onekey.send(
            prompt="Method", keys=common.METHOD_OPTIONS, callback=self.get_method
        )
    elif key == "o":
        orders = [(i[1], i[0]) for i in view.orders]
        lookup = dict([(i[0], i[1]) for i in view.orders])

        def change_order(k):
            self.master.options.order = lookup[k]

        signals.status_prompt_onekey.send(
            prompt="Order", keys=orders, callback=change_order
        )
    elif key == "F":
        o = self.master.options
        o.focus_follow = not o.focus_follow
    elif key == "v":
        val = not self.master.options.order_reversed
        self.master.options.order_reversed = val
    elif key == "W":
        if self.master.options.streamfile:
            self.master.options.streamfile = None
        else:
            signals.status_prompt_path.send(
                self,
                prompt="Stream flows to",
                callback=lambda path: self.master.options.update(streamfile=path),
            )
    else:
        return urwid.ListBox.keypress(self, size, key)