# Buggy code
def keypress(self, xxx_todo_changeme, key):
    (maxcol,) = xxx_todo_changeme
    key = common.shortcuts(key)
    if key == "a":
        self.flow.resume(self.master)
        signals.flowlist_change.send(self)
    elif key == "d":
        if self.flow.killable:
            self.flow.kill(self.master)
        self.master.view.remove(self.master.view.focus.flow)
    elif key == "D":
        cp = self.flow.copy()
        self.master.view.add(cp)
        self.master.view.focus.flow = cp
    elif key == "m":
        self.flow.marked = not self.flow.marked
        signals.flowlist_change.send(self)
    elif key == "M":
        self.master.view.toggle_marked()
    elif key == "r":
        try:
            self.master.replay_request(self.flow)
        except exceptions.ReplayException as e:
            signals.add_log("Replay error: %s" % e, "warn")
        signals.flowlist_change.send(self)
    elif key == "S":

        def stop_server_playback(response):
            if response == "y":
                self.master.options.server_replay = []

        a = self.master.addons.get("serverplayback")
        if a.count():
            signals.status_prompt_onekey.send(
                prompt="Stop current server replay?",
                keys=(
                    ("yes", "y"),
                    ("no", "n"),
                ),
                callback=stop_server_playback,
            )
        else:
            signals.status_prompt_onekey.send(
                prompt="Server Replay",
                keys=(
                    ("all flows", "a"),
                    ("this flow", "t"),
                ),
                callback=self.server_replay_prompt,
            )
    elif key == "U":
        for f in self.master.view:
            f.marked = False
        signals.flowlist_change.send(self)
    elif key == "V":
        if not self.flow.modified():
            signals.status_message.send(message="Flow not modified.")
            return
        self.flow.revert()
        signals.flowlist_change.send(self)
        signals.status_message.send(message="Reverted.")
    elif key == "w":
        signals.status_prompt_onekey.send(
            self,
            prompt="Save",
            keys=(
                ("listed flows", "l"),
                ("this flow", "t"),
            ),
            callback=self.save_flows_prompt,
        )
    elif key == "X":
        if self.flow.killable:
            self.flow.kill(self.master)
    elif key == "enter":
        if self.flow.request:
            self.master.view_flow(self.flow)
    elif key == "|":
        signals.status_prompt_path.send(
            prompt="Send flow to script",
            callback=self.master.run_script_once,
            args=(self.flow,),
        )
    elif key == "E":
        signals.status_prompt_onekey.send(
            self,
            prompt="Export to file",
            keys=[(e[0], e[1]) for e in export.EXPORTERS],
            callback=common.export_to_clip_or_file,
            args=(None, self.flow, common.ask_save_path),
        )
    elif key == "C":
        signals.status_prompt_onekey.send(
            self,
            prompt="Export to clipboard",
            keys=[(e[0], e[1]) for e in export.EXPORTERS],
            callback=common.export_to_clip_or_file,
            args=(None, self.flow, common.copy_to_clipboard_or_prompt),
        )
    elif key == "b":
        common.ask_save_body(None, self.flow)
    else:
        return 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, xxx_todo_changeme, key):
    (maxcol,) = xxx_todo_changeme
    key = common.shortcuts(key)
    if key == "a":
        self.flow.resume(self.master)
        signals.flowlist_change.send(self)
    elif key == "d":
        if self.flow.killable:
            self.flow.kill(self.master)
        self.master.view.remove(self.master.view.focus.flow)
    elif key == "D":
        cp = self.flow.copy()
        self.master.view.add(cp)
        self.master.view.focus.flow = cp
    elif key == "m":
        self.flow.marked = not self.flow.marked
        signals.flowlist_change.send(self)
    elif key == "r":
        try:
            self.master.replay_request(self.flow)
        except exceptions.ReplayException as e:
            signals.add_log("Replay error: %s" % e, "warn")
        signals.flowlist_change.send(self)
    elif key == "S":

        def stop_server_playback(response):
            if response == "y":
                self.master.options.server_replay = []

        a = self.master.addons.get("serverplayback")
        if a.count():
            signals.status_prompt_onekey.send(
                prompt="Stop current server replay?",
                keys=(
                    ("yes", "y"),
                    ("no", "n"),
                ),
                callback=stop_server_playback,
            )
        else:
            signals.status_prompt_onekey.send(
                prompt="Server Replay",
                keys=(
                    ("all flows", "a"),
                    ("this flow", "t"),
                ),
                callback=self.server_replay_prompt,
            )
    elif key == "U":
        for f in self.master.view:
            f.marked = False
        signals.flowlist_change.send(self)
    elif key == "V":
        if not self.flow.modified():
            signals.status_message.send(message="Flow not modified.")
            return
        self.flow.revert()
        signals.flowlist_change.send(self)
        signals.status_message.send(message="Reverted.")
    elif key == "w":
        signals.status_prompt_onekey.send(
            self,
            prompt="Save",
            keys=(
                ("listed flows", "l"),
                ("this flow", "t"),
            ),
            callback=self.save_flows_prompt,
        )
    elif key == "X":
        if self.flow.killable:
            self.flow.kill(self.master)
    elif key == "enter":
        if self.flow.request:
            self.master.view_flow(self.flow)
    elif key == "|":
        signals.status_prompt_path.send(
            prompt="Send flow to script",
            callback=self.master.run_script_once,
            args=(self.flow,),
        )
    elif key == "E":
        signals.status_prompt_onekey.send(
            self,
            prompt="Export to file",
            keys=[(e[0], e[1]) for e in export.EXPORTERS],
            callback=common.export_to_clip_or_file,
            args=(None, self.flow, common.ask_save_path),
        )
    elif key == "C":
        signals.status_prompt_onekey.send(
            self,
            prompt="Export to clipboard",
            keys=[(e[0], e[1]) for e in export.EXPORTERS],
            callback=common.export_to_clip_or_file,
            args=(None, self.flow, common.copy_to_clipboard_or_prompt),
        )
    elif key == "b":
        common.ask_save_body(None, self.flow)
    else:
        return key