##// END OF EJS Templates
make hlen_before_cell a function argument instead of class member
Julian Taylor -
Show More
@@ -229,21 +229,28 b' class TerminalInteractiveShell(InteractiveShell):'
229 229 # handling seems rather unpredictable...
230 230 self.write("\nKeyboardInterrupt in interact()\n")
231 231
232 def _replace_rlhist_multiline(self, source_raw):
232 def _replace_rlhist_multiline(self, source_raw, hlen_before_cell):
233 233 """Store multiple lines as a single entry in history"""
234 if self.multiline_history and self.has_readline and source_raw.rstrip():
235 hlen = self.readline.get_current_history_length()
236 234
237 # nothing changed do nothing, e.g. when rl removes consecutive dups
238 if self.hlen_before_cell == hlen:
239 return
235 # do nothing without readline or disabled multiline
236 if not self.has_readline or not self.multiline_history:
237 return hlen_before_cell
238
239 # skip empty cells
240 if not source_raw.rstrip():
241 return hlen_before_cell
242
243 # nothing changed do nothing, e.g. when rl removes consecutive dups
244 hlen = self.readline.get_current_history_length()
245 if hlen == hlen_before_cell:
246 return hlen_before_cell
240 247
241 for i in range(hlen - self.hlen_before_cell):
242 self.readline.remove_history_item(hlen - i - 1)
243 stdin_encoding = sys.stdin.encoding or "utf-8"
244 self.readline.add_history(py3compat.unicode_to_str(source_raw.rstrip(),
245 stdin_encoding))
246 self.hlen_before_cell = self.readline.get_current_history_length()
248 for i in range(hlen - hlen_before_cell):
249 self.readline.remove_history_item(hlen - i - 1)
250 stdin_encoding = sys.stdin.encoding or "utf-8"
251 self.readline.add_history(py3compat.unicode_to_str(source_raw.rstrip(),
252 stdin_encoding))
253 return self.readline.get_current_history_length()
247 254
248 255 def interact(self, display_banner=None):
249 256 """Closely emulate the interactive Python console."""
@@ -267,7 +274,7 b' class TerminalInteractiveShell(InteractiveShell):'
267 274
268 275 if self.has_readline:
269 276 self.readline_startup_hook(self.pre_readline)
270 self.hlen_before_cell = self.readline.get_current_history_length()
277 hlen_b4_cell = self.readline.get_current_history_length()
271 278 # exit_now is set by a call to %Exit or %Quit, through the
272 279 # ask_exit callback.
273 280
@@ -299,7 +306,8 b' class TerminalInteractiveShell(InteractiveShell):'
299 306 try:
300 307 self.write('\nKeyboardInterrupt\n')
301 308 source_raw = self.input_splitter.source_raw_reset()[1]
302 self._replace_rlhist_multiline(source_raw)
309 hlen_b4_cell = \
310 self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
303 311 more = False
304 312 except KeyboardInterrupt:
305 313 pass
@@ -328,7 +336,8 b' class TerminalInteractiveShell(InteractiveShell):'
328 336 if not more:
329 337 source_raw = self.input_splitter.source_raw_reset()[1]
330 338 self.run_cell(source_raw, store_history=True)
331 self._replace_rlhist_multiline(source_raw)
339 hlen_b4_cell = \
340 self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
332 341
333 342 # We are off again...
334 343 __builtin__.__dict__['__IPYTHON__active'] -= 1
@@ -31,7 +31,7 b' class InteractiveShellTestCase(unittest.TestCase):'
31 31 ip = get_ipython()
32 32 ip.has_readline = False
33 33 ip.readline = None
34 ip._replace_rlhist_multiline(u'source')
34 ip._replace_rlhist_multiline(u'source', 0)
35 35
36 36 @skipif(not get_ipython().has_readline, 'no readline')
37 37 def test_replace_multiline_hist_disabled(self):
@@ -42,11 +42,12 b' class InteractiveShellTestCase(unittest.TestCase):'
42 42 ghist = [u'line1', u'line2']
43 43 for h in ghist:
44 44 ip.readline.add_history(h)
45 ip.hlen_before_cell = ip.readline.get_current_history_length()
46 ip._replace_rlhist_multiline(u'sourc€\nsource2')
45 hlen_b4_cell = ip.readline.get_current_history_length()
46 hlen_b4_cell = ip._replace_rlhist_multiline(u'sourc€\nsource2',
47 hlen_b4_cell)
47 48
48 49 self.assertEquals(ip.readline.get_current_history_length(),
49 ip.hlen_before_cell)
50 hlen_b4_cell)
50 51 hist = self.rl_hist_entries(ip.readline, 2)
51 52 self.assertEquals(hist, ghist)
52 53
@@ -55,10 +56,10 b' class InteractiveShellTestCase(unittest.TestCase):'
55 56 """Test that multiline replace function adds history"""
56 57 ip = get_ipython()
57 58
58 ip.hlen_before_cell = ip.readline.get_current_history_length()
59 ip._replace_rlhist_multiline(u'sourc€')
59 hlen_b4_cell = ip.readline.get_current_history_length()
60 hlen_b4_cell = ip._replace_rlhist_multiline(u'sourc€', hlen_b4_cell)
60 61
61 self.assertEquals(ip.hlen_before_cell,
62 self.assertEquals(hlen_b4_cell,
62 63 ip.readline.get_current_history_length())
63 64
64 65 @skipif(not get_ipython().has_readline, 'no readline')
@@ -72,12 +73,13 b' class InteractiveShellTestCase(unittest.TestCase):'
72 73 ip.readline.add_history(h)
73 74
74 75 #start cell
75 ip.hlen_before_cell = ip.readline.get_current_history_length()
76 # nothing added to rl history, should do nothing
77 ip._replace_rlhist_multiline(u'sourc€\nsource2')
76 hlen_b4_cell = ip.readline.get_current_history_length()
77 # nothing added to rl history, should do nothing
78 hlen_b4_cell = ip._replace_rlhist_multiline(u'sourc€\nsource2',
79 hlen_b4_cell)
78 80
79 81 self.assertEquals(ip.readline.get_current_history_length(),
80 ip.hlen_before_cell)
82 hlen_b4_cell)
81 83 hist = self.rl_hist_entries(ip.readline, 2)
82 84 self.assertEquals(hist, ghist)
83 85
@@ -90,18 +92,20 b' class InteractiveShellTestCase(unittest.TestCase):'
90 92
91 93 ip.readline.add_history(u'line0')
92 94 #start cell
93 ip.hlen_before_cell = ip.readline.get_current_history_length()
95 hlen_b4_cell = ip.readline.get_current_history_length()
94 96 ip.readline.add_history('l€ne1')
95 97 ip.readline.add_history('line2')
96 98 #replace cell with single line
97 ip._replace_rlhist_multiline(u'l€ne1\nline2')
99 hlen_b4_cell = ip._replace_rlhist_multiline(u'l€ne1\nline2',
100 hlen_b4_cell)
98 101 ip.readline.add_history('l€ne3')
99 102 ip.readline.add_history('line4')
100 103 #replace cell with single line
101 ip._replace_rlhist_multiline(u'l€ne3\nline4')
104 hlen_b4_cell = ip._replace_rlhist_multiline(u'l€ne3\nline4',
105 hlen_b4_cell)
102 106
103 107 self.assertEquals(ip.readline.get_current_history_length(),
104 ip.hlen_before_cell)
108 hlen_b4_cell)
105 109 hist = self.rl_hist_entries(ip.readline, 3)
106 110 self.assertEquals(hist, ['line0', 'l€ne1\nline2', 'l€ne3\nline4'])
107 111
@@ -114,24 +118,25 b' class InteractiveShellTestCase(unittest.TestCase):'
114 118
115 119 ip.readline.add_history(u'line0')
116 120 #start cell
117 ip.hlen_before_cell = ip.readline.get_current_history_length()
121 hlen_b4_cell = ip.readline.get_current_history_length()
118 122 ip.readline.add_history('l€ne1')
119 123 ip.readline.add_history('line2')
120 ip._replace_rlhist_multiline(u'l€ne1\nline2')
124 hlen_b4_cell = ip._replace_rlhist_multiline(u'l€ne1\nline2',
125 hlen_b4_cell)
121 126 ip.readline.add_history('')
122 ip._replace_rlhist_multiline(u'')
127 hlen_b4_cell = ip._replace_rlhist_multiline(u'', hlen_b4_cell)
123 128 ip.readline.add_history('l€ne3')
124 ip._replace_rlhist_multiline(u'l€ne3')
129 hlen_b4_cell = ip._replace_rlhist_multiline(u'l€ne3', hlen_b4_cell)
125 130 ip.readline.add_history(' ')
126 ip._replace_rlhist_multiline(' ')
131 hlen_b4_cell = ip._replace_rlhist_multiline(' ', hlen_b4_cell)
127 132 ip.readline.add_history('\t')
128 133 ip.readline.add_history('\t ')
129 ip._replace_rlhist_multiline('\t')
134 hlen_b4_cell = ip._replace_rlhist_multiline('\t', hlen_b4_cell)
130 135 ip.readline.add_history('line4')
131 ip._replace_rlhist_multiline(u'line4')
136 hlen_b4_cell = ip._replace_rlhist_multiline(u'line4', hlen_b4_cell)
132 137
133 138 self.assertEquals(ip.readline.get_current_history_length(),
134 ip.hlen_before_cell)
139 hlen_b4_cell)
135 140 hist = self.rl_hist_entries(ip.readline, 4)
136 141 # expect no empty cells in history
137 142 self.assertEquals(hist, ['line0', 'l€ne1\nline2', 'l€ne3', 'line4'])
General Comments 0
You need to be logged in to leave comments. Login now