Skip to content

Commit 7a18cc7

Browse files
committed
Merge
2 parents 6117c76 + 5498a38 commit 7a18cc7

File tree

4 files changed

+88
-8
lines changed

4 files changed

+88
-8
lines changed

bpython/cli.py

100755100644
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
import unicodedata
3838
import errno
3939

40-
from locale import LC_ALL, getpreferredencoding, setlocale
40+
from locale import LC_ALL, setlocale
41+
import locale
4142
from types import ModuleType
4243

4344
# These are used for syntax hilighting.
@@ -60,14 +61,15 @@
6061
from bpython.pager import page
6162
import bpython.args
6263

63-
6464
def log(x):
6565
f = open('/tmp/bpython.log', 'a')
6666
f.write('%s\n' % (x,))
6767

6868
py3 = sys.version_info[0] == 3
6969
stdscr = None
7070

71+
def getpreferredencoding():
72+
return locale.getpreferredencoding() or sys.getdefaultencoding()
7173

7274
def calculate_screen_lines(tokens, width, cursor=0):
7375
"""Given a stream of tokens and a screen width plus an optional

bpython/repl.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
class Interpreter(code.InteractiveInterpreter):
6262

63-
def __init__(self, locals=None, encoding=sys.getdefaultencoding()):
63+
def __init__(self, locals=None, encoding=None):
6464
"""The syntaxerror callback can be set at any time and will be called
6565
on a caught syntax error. The purpose for this in bpython is so that
6666
the repl can be instantiated after the interpreter (which it
@@ -69,7 +69,7 @@ def __init__(self, locals=None, encoding=sys.getdefaultencoding()):
6969
specifically, this is so that autoindentation does not occur after a
7070
traceback."""
7171

72-
self.encoding = encoding
72+
self.encoding = encoding or sys.getdefaultencoding()
7373
self.syntaxerror_callback = None
7474
# Unfortunately code.InteractiveInterpreter is a classic class, so no super()
7575
code.InteractiveInterpreter.__init__(self, locals)
@@ -242,7 +242,10 @@ def next(self):
242242
return self.matches[self.index]
243243

244244
def previous(self):
245-
self.index = (self.index - 1) % len(self.matches)
245+
if self.index <= 0:
246+
self.index = len(self.matches)
247+
self.index -= 1
248+
246249
return self.matches[self.index]
247250

248251
def update(self, current_word='', matches=[]):

bpython/test/test_repl.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import unittest
2+
from itertools import islice
3+
24
from bpython import repl
35

46

@@ -52,7 +54,80 @@ def test_forward(self):
5254
self.history.forward()
5355
self.assertEqual(self.history.forward(), '#999')
5456

57+
def test_append(self):
58+
self.history.append('print "foo\n"\n')
59+
self.history.append('\n')
60+
61+
self.assertEqual(self.history.back(), 'print "foo\n"')
62+
63+
def test_enter(self):
64+
self.history.enter('#lastnumber!')
65+
66+
self.assertEqual(self.history.back(), '#999')
67+
self.assertEqual(self.history.forward(), '#lastnumber!')
68+
69+
def test_reset(self):
70+
self.history.enter('#lastnumber!')
71+
self.history.reset()
72+
73+
self.assertEqual(self.history.back(), '#999')
74+
self.assertEqual(self.history.forward(), '')
75+
76+
77+
class TestMatchesIterator(unittest.TestCase):
78+
79+
def setUp(self):
80+
self.matches = ['bobby', 'bobbies', 'bobberina']
81+
self.matches_iterator = repl.MatchesIterator(current_word='bob',
82+
matches=self.matches)
83+
84+
def test_next(self):
85+
self.assertEqual(self.matches_iterator.next(), self.matches[0])
86+
87+
for x in range(len(self.matches) - 1):
88+
self.matches_iterator.next()
89+
90+
self.assertEqual(self.matches_iterator.next(), self.matches[0])
91+
self.assertEqual(self.matches_iterator.next(), self. matches[1])
92+
self.assertNotEqual(self.matches_iterator.next(), self.matches[1])
93+
94+
def test_previous(self):
95+
self.assertEqual(self.matches_iterator.previous(), self.matches[2])
96+
97+
for x in range(len(self.matches) - 1):
98+
self.matches_iterator.previous()
99+
100+
self.assertNotEqual(self.matches_iterator.previous(), self.matches[0])
101+
self.assertEqual(self.matches_iterator.previous(), self.matches[1])
102+
self.assertEqual(self.matches_iterator.previous(), self.matches[0])
103+
104+
def test_nonzero(self):
105+
"""self.matches_iterator should be False at start,
106+
then True once we active a match.
107+
"""
108+
self.assertFalse(self.matches_iterator)
109+
self.matches_iterator.next()
110+
self.assertTrue(self.matches_iterator)
111+
112+
def test_iter(self):
113+
slice = islice(self.matches_iterator, 0, 9)
114+
self.assertEqual(list(slice), self.matches * 3)
115+
116+
def test_current(self):
117+
self.assertRaises(ValueError, self.matches_iterator.current)
118+
self.matches_iterator.next()
119+
self.assertEqual(self.matches_iterator.current(), self.matches[0])
120+
121+
def test_update(self):
122+
slice = islice(self.matches_iterator, 0, 3)
123+
self.assertEqual(list(slice), self.matches)
124+
125+
newmatches = ['string', 'str', 'set']
126+
self.matches_iterator.update('s', newmatches)
55127

128+
newslice = islice(newmatches, 0, 3)
129+
self.assertNotEqual(list(slice), self.matches)
130+
self.assertEqual(list(newslice), newmatches)
56131

57132

58133
if __name__ == '__main__':

bpython/urwid.py

100755100644
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
22
#
33
# The MIT License
44
#
@@ -392,7 +392,7 @@ def __init__(self, config, statusbar=None):
392392
def confirm(self, q):
393393
"""Ask for yes or no and return boolean"""
394394
return self.statusbar.prompt(q).lower().startswith('y')
395-
395+
396396
def notify(self, s, n=10):
397397
return self.statusbar.message(s, n)
398398

@@ -598,7 +598,7 @@ def getstdout(self):
598598
def ask_confirmation(self, q):
599599
"""Ask for yes or no and return boolean"""
600600
return self.statusbar.prompt(q).lower().startswith('y')
601-
601+
602602
def reevaluate(self):
603603
"""Clear the buffer, redraw the screen and re-evaluate the history"""
604604

0 commit comments

Comments
 (0)