X Tutup
Skip to content

Commit 672bf78

Browse files
committed
Decode module names in attr_matches (fixes bpython#453)
Also delay set construction as long as possible Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
1 parent a194976 commit 672bf78

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

bpython/importcompletion.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,27 @@ def catch_warnings():
5656
fully_loaded = False
5757

5858

59+
def try_decode_module(module, encoding):
60+
"""Try to decode module names."""
61+
if not py3 and not isinstance(module, unicode):
62+
try:
63+
return module.decode(encoding)
64+
except UnicodeDecodeError:
65+
# Not importable anyway, ignore it
66+
return None
67+
return module
68+
69+
5970
def module_matches(cw, prefix=''):
6071
"""Modules names to replace cw with"""
6172
full = '%s.%s' % (prefix, cw) if prefix else cw
62-
matches = set(name for name in modules
63-
if (name.startswith(full) and
64-
name.find('.', len(full)) == -1))
73+
matches = (name for name in modules
74+
if (name.startswith(full) and
75+
name.find('.', len(full)) == -1))
6576
if prefix:
6677
return set(match[len(prefix)+1:] for match in matches)
6778
else:
68-
return matches
79+
return set(matches)
6980

7081
def attr_matches(cw, prefix='', only_modules=False):
7182
"""Attributes to replace name with"""
@@ -75,17 +86,19 @@ def attr_matches(cw, prefix='', only_modules=False):
7586
return set()
7687
module = sys.modules[module_name]
7788
if only_modules:
78-
matches = set(name for name in dir(module)
79-
if (name.startswith(name_after_dot) and
80-
'%s.%s' % (module_name, name)) in sys.modules)
89+
matches = (name for name in dir(module)
90+
if (name.startswith(name_after_dot) and
91+
'%s.%s' % (module_name, name)) in sys.modules)
8192
else:
82-
matches = set(name for name in dir(module)
83-
if name.startswith(name_after_dot))
93+
matches = (name for name in dir(module)
94+
if name.startswith(name_after_dot))
8495
module_part, _, _ = cw.rpartition('.')
8596
if module_part:
86-
matches = set('%s.%s' % (module_part, m) for m in matches)
97+
matches = ('%s.%s' % (module_part, m) for m in matches)
98+
99+
return set(filter(lambda x: x is not None,
100+
(try_decode_module(match, 'ascii') for match in matches)))
87101

88-
return matches
89102
def module_attr_matches(name):
90103
"""Only attributes which are modules to replace name with"""
91104
return attr_matches(name, prefix='', only_modules=True)
@@ -182,12 +195,9 @@ def find_all_modules(path=None):
182195
if not p:
183196
p = os.curdir
184197
for module in find_modules(p):
185-
if not py3 and not isinstance(module, unicode):
186-
try:
187-
module = module.decode(sys.getfilesystemencoding())
188-
except UnicodeDecodeError:
189-
# Not importable anyway, ignore it
190-
continue
198+
module = try_decode_module(module, sys.getfilesystemencoding())
199+
if module is None:
200+
continue
191201
modules.add(module)
192202
yield
193203

0 commit comments

Comments
 (0)
X Tutup