X Tutup
Skip to content

Commit 1b6fba8

Browse files
Merge branch 'fix-unicode'
Updates curtsies version to 2.0, use unicode for all output
2 parents 2df245b + 8d6ba1c commit 1b6fba8

File tree

7 files changed

+41
-19
lines changed

7 files changed

+41
-19
lines changed

bpython/_py3compat.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,13 @@ def prepare_for_exec(arg, encoding=None):
4949
else:
5050
def prepare_for_exec(arg, encoding=None):
5151
return arg.encode(encoding)
52+
53+
54+
def try_decode(s, encoding):
55+
"""Try to decode s which is str names. Return None if not decodable"""
56+
if not py3 and not isinstance(s, unicode):
57+
try:
58+
return s.decode(encoding)
59+
except UnicodeDecodeError:
60+
return None
61+
return s

bpython/autocomplete.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from bpython import inspection
3535
from bpython import importcompletion
3636
from bpython import line as lineparts
37-
from bpython._py3compat import py3
37+
from bpython._py3compat import py3, try_decode
3838
from bpython.lazyre import LazyReCompile
3939

4040

@@ -286,11 +286,17 @@ def matches(self, cursor_offset, line, **kwargs):
286286
n = len(text)
287287
for word in keyword.kwlist:
288288
if method_match(word, n, text):
289+
if not py3:
290+
word = word.decode('ascii') # py2 keywords are all ascii
289291
matches.add(word)
290292
for nspace in [builtins.__dict__, locals_]:
291293
for word, val in nspace.items():
292294
if (method_match(word, len(text), text) and
293295
word != "__builtins__"):
296+
word = try_decode(word, 'ascii')
297+
# if identifier isn't ascii, don't complete (syntax error)
298+
if word is None:
299+
continue
294300
matches.add(_callable_postfix(val, word))
295301
return matches
296302

bpython/curtsiesfrontend/replpainter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,14 @@ def formatted_argspec(argspec, columns, config):
108108
color = bolds[color]
109109

110110
if not py3:
111-
s += color(inspect.strseq(arg, str))
111+
s += color(inspect.strseq(arg, unicode))
112112
else:
113113
s += color(arg)
114114

115115
if kw is not None:
116116
s += punctuation_color('=')
117+
if not py3:
118+
kw = kw.decode('ascii', 'replace')
117119
s += token_color(kw)
118120

119121
if i != len(args) - 1:

bpython/importcompletion.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222

23-
from bpython._py3compat import py3
23+
from bpython._py3compat import py3, try_decode
2424
from bpython.line import current_word, current_import, \
2525
current_from_import_from, current_from_import_import
2626

@@ -42,17 +42,6 @@
4242
fully_loaded = False
4343

4444

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-
5645
def module_matches(cw, prefix=''):
5746
"""Modules names to replace cw with"""
5847
full = '%s.%s' % (prefix, cw) if prefix else cw
@@ -83,7 +72,7 @@ def attr_matches(cw, prefix='', only_modules=False):
8372
if module_part:
8473
matches = ('%s.%s' % (module_part, m) for m in matches)
8574

86-
generator = (try_decode_module(match, 'ascii') for match in matches)
75+
generator = (try_decode(match, 'ascii') for match in matches)
8776
return set(filter(lambda x: x is not None, generator))
8877

8978

@@ -177,14 +166,15 @@ def find_all_modules(path=None):
177166
"""Return a list with all modules in `path`, which should be a list of
178167
directory names. If path is not given, sys.path will be used."""
179168
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)
181171
path = sys.path
182172

183173
for p in path:
184174
if not p:
185175
p = os.curdir
186176
for module in find_modules(p):
187-
module = try_decode_module(module, sys.getfilesystemencoding())
177+
module = try_decode(module, 'ascii')
188178
if module is None:
189179
continue
190180
modules.add(module)

bpython/test/test_autocomplete.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# encoding: utf-8
2+
13
from collections import namedtuple
24
import inspect
35
from bpython._py3compat import py3
@@ -273,6 +275,15 @@ def function():
273275
locals_={'function': function}),
274276
set(('function(', )))
275277

278+
def test_completions_are_unicode(self):
279+
for m in self.com.matches(1, 'a', locals_={'abc': 10}):
280+
self.assertIsInstance(m, type(u''))
281+
282+
@unittest.skipIf(py3, "in Python 3 invalid identifiers are passed through")
283+
def test_ignores_nonascii_encodable(self):
284+
self.assertSetEqual(self.com.matches(1, 'abc', locals_={'abcß': 10}),
285+
set())
286+
276287

277288
class TestParameterNameCompletion(unittest.TestCase):
278289
def test_set_of_params_returns_when_matches_found(self):

bpython/test/test_importcompletion.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import unicode_literals
2+
13
from bpython import importcompletion
24

35
try:
@@ -29,7 +31,8 @@ class TestRealComplete(unittest.TestCase):
2931

3032
@classmethod
3133
def setUpClass(cls):
32-
[_ for _ in importcompletion.find_iterator]
34+
for _ in importcompletion.find_iterator:
35+
pass
3336
__import__('sys')
3437
__import__('os')
3538

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def initialize_options(self):
210210
install_requires = [
211211
'pygments',
212212
'requests',
213-
'curtsies >=0.1.18, <0.2.0',
213+
'curtsies >=0.1.18',
214214
'greenlet',
215215
'six >=1.4'
216216
]

0 commit comments

Comments
 (0)
X Tutup