# Buggy code
def get_completions(self, document):
    """Ask jedi to complete."""
    script = get_jedi_script_from_document(
        document, self.get_locals(), self.get_globals()
    )

    if script:
        try:
            completions = script.completions()
        except TypeError:
            # Issue #9: bad syntax causes completions() to fail in jedi.
            # https://github.com/jonathanslenders/python-prompt-toolkit/issues/9
            pass
        else:
            for c in completions:
                yield Completion(
                    c.name_with_symbols,
                    len(c.complete) - len(c.name_with_symbols),
                    display=c.name_with_symbols,
                )


# Traceback (UnicodeDecodeError)
In [1]: import sException in thread Thread-8:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/home/edd/.local/lib/python2.7/site-packages/prompt_toolkit/__init__.pyn
callback()
File "/home/edd/.local/lib/python2.7/site-packages/prompt_toolkit/__init__.pyn
completions = list(line.completer.get_completions(document))
File "/home/edd/.local/lib/python2.7/site-packages/prompt_toolkit/contrib/ipys
for c in super(IPythonCompleter, self).get_completions(document):
File "/home/edd/.local/lib/python2.7/site-packages/prompt_toolkit/contrib/pyts
completions = script.completions()
File "/home/edd/.local/lib/python2.7/site-packages/jedi/api/__init__.py", lins
completions = get_completions(user_stmt, b)
File "/home/edd/.local/lib/python2.7/site-packages/jedi/api/__init__.py", lins
return self._simple_complete(path, like)
File "/home/edd/.local/lib/python2.7/site-packages/jedi/api/__init__.py", line
return super(Interpreter, self)._simple_complete(path, like)
File "/home/edd/.local/lib/python2.7/site-packages/jedi/api/__init__.py", line
names = s.get_defined_names(on_import_stmt=True)
File "/home/edd/.local/lib/python2.7/site-packages/jedi/evaluate/imports.py",s
names += self._get_module_names([path])
File "/home/edd/.local/lib/python2.7/site-packages/jedi/evaluate/imports.py",s
for module_loader, name, is_pkg in pkgutil.iter_modules(search_path):
File "/usr/local/lib/python2.7/pkgutil.py", line 147, in iter_modules
for name, ispkg in iter_importer_modules(i, prefix):
File "/usr/local/lib/python2.7/pkgutil.py", line 202, in iter_modules
filenames.sort()  # handle packages before same-named modules
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 25: ordina)

# Fixed code
def get_completions(self, document):
    """Ask jedi to complete."""
    script = get_jedi_script_from_document(
        document, self.get_locals(), self.get_globals()
    )

    if script:
        try:
            completions = script.completions()
        except TypeError:
            # Issue #9: bad syntax causes completions() to fail in jedi.
            # https://github.com/jonathanslenders/python-prompt-toolkit/issues/9
            pass
        except UnicodeDecodeError:
            # Issue #43: UnicodeDecodeError on OpenBSD
            # https://github.com/jonathanslenders/python-prompt-toolkit/issues/43
            pass
        else:
            for c in completions:
                yield Completion(
                    c.name_with_symbols,
                    len(c.complete) - len(c.name_with_symbols),
                    display=c.name_with_symbols,
                )