|
20 | 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21 | 21 | # THE SOFTWARE. |
22 | 22 |
|
23 | | -from bpython._py3compat import py3 |
| 23 | +from bpython._py3compat import py3, try_decode |
24 | 24 | from bpython.line import current_word, current_import, \ |
25 | 25 | current_from_import_from, current_from_import_import |
26 | 26 |
|
|
42 | 42 | fully_loaded = False |
43 | 43 |
|
44 | 44 |
|
45 | | -def try_decode_module(module, encoding): |
46 | | - """Try to decode module names.""" |
47 | | - if not py3 and not isinstance(module, unicode): |
48 | | - try: |
49 | | - return module.decode(encoding) |
50 | | - except UnicodeDecodeError: |
51 | | - # Not importable anyway, ignore it |
52 | | - return None |
53 | | - return module |
54 | | - |
55 | | - |
56 | 45 | def module_matches(cw, prefix=''): |
57 | 46 | """Modules names to replace cw with""" |
58 | 47 | full = '%s.%s' % (prefix, cw) if prefix else cw |
@@ -83,7 +72,7 @@ def attr_matches(cw, prefix='', only_modules=False): |
83 | 72 | if module_part: |
84 | 73 | matches = ('%s.%s' % (module_part, m) for m in matches) |
85 | 74 |
|
86 | | - generator = (try_decode_module(match, 'ascii') for match in matches) |
| 75 | + generator = (try_decode(match, 'ascii') for match in matches) |
87 | 76 | return set(filter(lambda x: x is not None, generator)) |
88 | 77 |
|
89 | 78 |
|
@@ -177,14 +166,15 @@ def find_all_modules(path=None): |
177 | 166 | """Return a list with all modules in `path`, which should be a list of |
178 | 167 | directory names. If path is not given, sys.path will be used.""" |
179 | 168 | if path is None: |
180 | | - modules.update(sys.builtin_module_names) |
| 169 | + modules.update(try_decode(m, 'ascii') |
| 170 | + for m in sys.builtin_module_names) |
181 | 171 | path = sys.path |
182 | 172 |
|
183 | 173 | for p in path: |
184 | 174 | if not p: |
185 | 175 | p = os.curdir |
186 | 176 | for module in find_modules(p): |
187 | | - module = try_decode_module(module, sys.getfilesystemencoding()) |
| 177 | + module = try_decode(module, 'ascii') |
188 | 178 | if module is None: |
189 | 179 | continue |
190 | 180 | modules.add(module) |
|
0 commit comments