# Buggy code
def find_next(self, backwards):
    if not self.search_term:
        if self.state.last_search:
            self.search_term = self.state.last_search
        else:
            self.set_highlight(None)
            return
    # Start search at focus + 1
    if backwards:
        rng = xrange(len(self.body) - 1, -1, -1)
    else:
        rng = xrange(1, len(self.body) + 1)
    for i in rng:
        off = (self.focus_position + i) % len(self.body)
        w = self.body[off]
        txt = self.get_text(w)
        if txt and self.search_term in txt:
            self.set_highlight(off)
            self.set_focus(off, coming_from="above")
            self.body._modified()
            return
    else:
        self.set_highlight(None)
        signals.status_message.send(message="Search not found.", expire=1)


# Traceback (NameError)
Traceback (most recent call last):
File "/media/sf_git/mitmproxy/mitmproxy/console/master.py", line 498, in run
self.loop.run()
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 278, in run
self._run()
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 376, in _run
self.event_loop.run()
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 682, in run
self._loop()
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 719, in _loop
self._watch_files[fd]()
File "/usr/local/lib/python3.5/dist-packages/urwid/raw_display.py", line 393, in <lambda>
event_loop, callback, self.get_available_raw_input())
File "/usr/local/lib/python3.5/dist-packages/urwid/raw_display.py", line 493, in parse_input
callback(processed, processed_codes)
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 403, in _update
self.process_input(keys)
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 503, in process_input
k = self._topmost_widget.keypress(self.screen_size, k)
File "/media/sf_git/mitmproxy/mitmproxy/console/window.py", line 42, in keypress
k = super(self.__class__, self).keypress(size, k)
File "/usr/local/lib/python3.5/dist-packages/urwid/container.py", line 1116, in keypress
return self.footer.keypress((maxcol,),key)
File "/media/sf_git/mitmproxy/mitmproxy/console/statusbar.py", line 134, in keypress
return self.master.ab.keypress(*args, **kwargs)
File "/media/sf_git/mitmproxy/mitmproxy/console/statusbar.py", line 89, in keypress
self.prompt_execute(self._w.get_edit_text())
File "/media/sf_git/mitmproxy/mitmproxy/console/statusbar.py", line 112, in prompt_execute
msg = p(txt, *args)
File "/media/sf_git/mitmproxy/mitmproxy/console/searchable.py", line 52, in set_search
self.find_next(False)
File "/media/sf_git/mitmproxy/mitmproxy/console/searchable.py", line 83, in find_next
rng = xrange(1, len(self.body) + 1)
NameError: name 'xrange' is not defined

# Fixed code
def find_next(self, backwards):
    if not self.search_term:
        if self.state.last_search:
            self.search_term = self.state.last_search
        else:
            self.set_highlight(None)
            return
    # Start search at focus + 1
    if backwards:
        rng = range(len(self.body) - 1, -1, -1)
    else:
        rng = range(1, len(self.body) + 1)
    for i in rng:
        off = (self.focus_position + i) % len(self.body)
        w = self.body[off]
        txt = self.get_text(w)
        if txt and self.search_term in txt:
            self.set_highlight(off)
            self.set_focus(off, coming_from="above")
            self.body._modified()
            return
    else:
        self.set_highlight(None)
        signals.status_message.send(message="Search not found.", expire=1)