Skip to content

Commit 2d06f66

Browse files
committed
Deal with non-ascii filenames in import completion.
This closes issue bpython#139.
1 parent 4d00329 commit 2d06f66

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

bpython/importcompletion.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import os
2626
import sys
2727

28+
py3 = sys.version_info[:2] >= (3, 0)
2829

2930
# The cached list of all known modules
3031
modules = set()
@@ -107,6 +108,10 @@ def find_modules(path):
107108
fo, pathname, _ = imp.find_module(name, [path])
108109
except (ImportError, SyntaxError):
109110
continue
111+
except UnicodeEncodeError:
112+
# Happens with Python 3 when there is a filename in some
113+
# invalid encoding
114+
continue
110115
else:
111116
if fo is not None:
112117
fo.close()
@@ -129,6 +134,12 @@ def find_all_modules(path=None):
129134
if not p:
130135
p = os.curdir
131136
for module in find_modules(p):
137+
if not py3 and not isinstance(module, unicode):
138+
try:
139+
module = module.decode(sys.getfilesystemencoding())
140+
except UnicodeDecodeError:
141+
# Not importable anyway, ignore it
142+
continue
132143
modules.add(module)
133144
yield
134145

0 commit comments

Comments
 (0)