# Buggy code
def get_tokens(self, cli, width):
    result = []
    append = result.append
    Signature = Token.Toolbar.Signature

    if cli.line.signatures:
        sig = cli.line.signatures[0]  # Always take the first one.

        append((Token, "           "))
        append((Signature, sig.full_name))
        append((Signature.Operator, "("))

        for i, p in enumerate(sig.params):
            if i == sig.index:
                append((Signature.CurrentName, str(p.name)))
            else:
                append((Signature, str(p.name)))
            append((Signature.Operator, ", "))

        result.pop()  # Pop last comma
        append((Signature.Operator, ")"))
    return result


# Traceback (IndexError)
➜  ~  ptpython --vi
In [1]: def bez(cps, t):
2.     if len(cps) < 2:
3.         return cps[0]
4.     p1 = bez(Traceback (most recent call last):
File "/usr/local/bin/ptpython", line 80, in <module>
_run_repl()       all
File "/usr/local/bin/ptpython", line 77, in _run_repl
startup_paths=startup_paths, always_multiline=always_multiline)
File "/usr/local/lib/python2.7/dist-packages/prompt_toolkit/contrib/repl.py",d
cli.start_repl(startup_paths=startup_paths)
File "/usr/local/lib/python2.7/dist-packages/prompt_toolkit/contrib/repl.py",l
on_exit=AbortAction.RAISE_EXCEPTION)
File "/usr/local/lib/python2.7/dist-packages/prompt_toolkit/__init__.py", lint
self._redraw()
File "/usr/local/lib/python2.7/dist-packages/prompt_toolkit/__init__.py", linw
self.renderer.render(self)
File "/usr/local/lib/python2.7/dist-packages/prompt_toolkit/renderer.py", linr
output = self.render_to_str(cli)
File "/usr/local/lib/python2.7/dist-packages/prompt_toolkit/renderer.py", linr
self.layout.write_to_screen(cli, screen, height)
File "/usr/local/lib/python2.7/dist-packages/prompt_toolkit/layout/__init__.pn
t.write(cli, screen)z(cps, t)
File "/usr/local/lib/python2.7/dist-packages/prompt_toolkit/layout/toolbars.pe
tokens = self.get_tokens(cli, width)
File "/usr/local/lib/python2.7/dist-packages/prompt_toolkit/contrib/python_ins
append((Signature, sig.full_name))
File "/usr/local/lib/python2.7/dist-packages/jedi/api/classes.py", line 295, e
path = [unicode(p) for p in self._path()]
File "/usr/local/lib/python2.7/dist-packages/jedi/api/classes.py", line 170, h
path.insert(0, par.name)
File "/usr/local/lib/python2.7/dist-packages/jedi/api/interpreter.py", line 3_
return getattr(self.parser_module, name)
File "/usr/local/lib/python2.7/dist-packages/jedi/parser/fast.py", line 38, i_
return getattr(self.parsers[0].module, name)
IndexError: list index out of range

# Fixed code
def get_tokens(self, cli, width):
    result = []
    append = result.append
    Signature = Token.Toolbar.Signature

    if cli.line.signatures:
        sig = cli.line.signatures[0]  # Always take the first one.

        append((Token, "           "))
        try:
            append((Signature, sig.full_name))
        except IndexError:
            # Workaround for #37: https://github.com/jonathanslenders/python-prompt-toolkit/issues/37
            # See also: https://github.com/davidhalter/jedi/issues/490
            return []

        append((Signature.Operator, "("))

        for i, p in enumerate(sig.params):
            if i == sig.index:
                append((Signature.CurrentName, str(p.name)))
            else:
                append((Signature, str(p.name)))
            append((Signature.Operator, ", "))

        result.pop()  # Pop last comma
        append((Signature.Operator, ")"))
    return result