# Buggy code
def get_jedi_script_from_document(document, locals, globals):
    import jedi  # We keep this import in-line, to improve start-up time.
    # Importing Jedi is 'slow'.

    try:
        return jedi.Interpreter(
            document.text,
            column=document.cursor_position_col,
            line=document.cursor_position_row + 1,
            path="input-text",
            namespaces=[locals, globals],
        )
    except ValueError:
        # Invalid cursor position.
        # ValueError('`column` parameter is not in a valid range.')
        return None
    except AttributeError:
        # Workaround for #65: https://github.com/jonathanslenders/python-prompt-toolkit/issues/65
        # See also: https://github.com/davidhalter/jedi/issues/508
        return None
    except IndexError:
        # Workaround Jedi issue #514: for https://github.com/davidhalter/jedi/issues/514
        return None
    except KeyError:
        # Workaroud for a crash when the input is "u'", the start of a unicode string.
        return None


# Traceback (jedi.parser.pgen2.parse.ParseError)
File "/home/jonathan/git/ptpython/ptpython/utils.py", line 52, t
namespaces=[locals, globals])
File "/home/jonathan/.virtualenvs/python35/lib/python3.5/site-p_
self._parser.module())
File "/home/jonathan/.virtualenvs/python35/lib/python3.5/site-pe
return self._parser().module
File "/home/jonathan/.virtualenvs/python35/lib/python3.5/site-pr
result = func(self)
File "/home/jonathan/.virtualenvs/python35/lib/python3.5/site-pr
parser = Parser(self._grammar, self._source, self._path)
File "/home/jonathan/.virtualenvs/python35/lib/python3.5/site-p_
self.module = p.parse(self._tokenize(tokenizer))
File "/home/jonathan/.virtualenvs/python35/lib/python3.5/site-pe
raise ParseError("incomplete input", type, value, start_pos)
jedi.parser.pgen2.parse.ParseError: incomplete input: type='ENDMA)

During handling of the above exception, another exception occurre:

Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 923, in _bootstrapr
self.run()
File "/usr/lib/python3.5/threading.py", line 871, in run
self._target(*self._args, **self._kwargs)
File "/home/jonathan/git/python-prompt-toolkit/prompt_toolkit/in
completions = list(buffer.completer.get_completions(document,)
File "/home/jonathan/git/ptpython/ptpython/completer.py", line s
script = get_jedi_script_from_document(document, self.get_loc)
File "/home/jonathan/git/ptpython/ptpython/utils.py", line 67, t
except jedi.ParseError:

# Fixed code
def get_jedi_script_from_document(document, locals, globals):
    import jedi  # We keep this import in-line, to improve start-up time.
    # Importing Jedi is 'slow'.

    try:
        return jedi.Interpreter(
            document.text,
            column=document.cursor_position_col,
            line=document.cursor_position_row + 1,
            path="input-text",
            namespaces=[locals, globals],
        )
    except ValueError:
        # Invalid cursor position.
        # ValueError('`column` parameter is not in a valid range.')
        return None
    except AttributeError:
        # Workaround for #65: https://github.com/jonathanslenders/python-prompt-toolkit/issues/65
        # See also: https://github.com/davidhalter/jedi/issues/508
        return None
    except IndexError:
        # Workaround Jedi issue #514: for https://github.com/davidhalter/jedi/issues/514
        return None
    except KeyError:
        # Workaroud for a crash when the input is "u'", the start of a unicode string.
        return None
    except Exception:
        # Workaround for: https://github.com/jonathanslenders/ptpython/issues/91
        return None