# Buggy code
def get_jedi_script_from_document(document, locals, globals):
    try:
        return jedi.Interpreter(
            document.text,
            column=document.cursor_position_col,
            line=document.cursor_position_row + 1,
            path="input-text",
            namespaces=[locals, globals],
        )

    except jedi.common.MultiLevelStopIteration:
        # This happens when the document is just a backslash.
        return None
    except ValueError:
        # Invalid cursor position.
        # ValueError('`column` parameter is not in a valid range.')
        return None


# Traceback (AttributeError)
In [1]: yieldException in thread Thread-7:
Traceback (most recent call last):
File "/usr/lib/python3.4/threading.py", line 921, in _bootstrap_inner
self.run()
File "/usr/lib/python3.4/threading.py", line 869, in run
self._target(*self._args, **self._kwargs)
File "/usr/lib/python3.4/site-packages/prompt_toolkit-0.22-py3.4.egg/prompt_toolkit/__init__.py", line 400, in run
completions = list(line.completer.get_completions(document))
File "/usr/lib/python3.4/site-packages/prompt_toolkit-0.22-py3.4.egg/prompt_toolkit/contrib/ipython.py", line 69, in get_completions
for c in super(IPythonCompleter, self).get_completions(document):
File "/usr/lib/python3.4/site-packages/prompt_toolkit-0.22-py3.4.egg/prompt_toolkit/contrib/python_input.py", line 477, in get_completions
script = get_jedi_script_from_document(document, self.get_locals(), self.get_globals())
File "/usr/lib/python3.4/site-packages/prompt_toolkit-0.22-py3.4.egg/prompt_toolkit/contrib/python_input.py", line 457, in get_jedi_script_from_document
namespaces=[locals, globals])
File "/usr/lib/python3.4/site-packages/jedi-0.8.1_final0-py3.4.egg/jedi/api/__init__.py", line 642, in __init__
interpreter.create(self._evaluator, namespaces[0], self._parser.module())
File "/usr/lib/python3.4/site-packages/jedi-0.8.1_final0-py3.4.egg/jedi/api/interpreter.py", line 108, in create
something.parent = ns
AttributeError: 'NoneType' object has no attribute 'parent'

Exception in thread Thread-8:
Traceback (most recent call last):
File "/usr/lib/python3.4/threading.py", line 921, in _bootstrap_inner
self.run()
File "/usr/lib/python3.4/threading.py", line 869, in run
self._target(*self._args, **self._kwargs)
File "/usr/lib/python3.4/site-packages/prompt_toolkit-0.22-py3.4.egg/prompt_toolkit/contrib/python_input.py", line 579, in run
script = get_jedi_script_from_document(document, self.get_locals(), self.get_globals())
File "/usr/lib/python3.4/site-packages/prompt_toolkit-0.22-py3.4.egg/prompt_toolkit/contrib/python_input.py", line 457, in get_jedi_script_from_document
namespaces=[locals, globals])
File "/usr/lib/python3.4/site-packages/jedi-0.8.1_final0-py3.4.egg/jedi/api/__init__.py", line 642, in __init__
interpreter.create(self._evaluator, namespaces[0], self._parser.module())
File "/usr/lib/python3.4/site-packages/jedi-0.8.1_final0-py3.4.egg/jedi/api/interpreter.py", line 108, in create
something.parent = ns
AttributeError: 'NoneType' object has no attribute 'parent'

# Fixed code
def get_jedi_script_from_document(document, locals, globals):
    try:
        return jedi.Interpreter(
            document.text,
            column=document.cursor_position_col,
            line=document.cursor_position_row + 1,
            path="input-text",
            namespaces=[locals, globals],
        )

    except jedi.common.MultiLevelStopIteration:
        # This happens when the document is just a backslash.
        return None
    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