##// 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 # handling seems rather unpredictable...
229 # handling seems rather unpredictable...
230 self.write("\nKeyboardInterrupt in interact()\n")
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 """Store multiple lines as a single entry in history"""
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
235 # do nothing without readline or disabled multiline
238 if self.hlen_before_cell == hlen:
236 if not self.has_readline or not self.multiline_history:
239 return
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):
248 for i in range(hlen - hlen_before_cell):
242 self.readline.remove_history_item(hlen - i - 1)
249 self.readline.remove_history_item(hlen - i - 1)
243 stdin_encoding = sys.stdin.encoding or "utf-8"
250 stdin_encoding = sys.stdin.encoding or "utf-8"
244 self.readline.add_history(py3compat.unicode_to_str(source_raw.rstrip(),
251 self.readline.add_history(py3compat.unicode_to_str(source_raw.rstrip(),
245 stdin_encoding))
252 stdin_encoding))
246 self.hlen_before_cell = self.readline.get_current_history_length()
253 return self.readline.get_current_history_length()
247
254
248 def interact(self, display_banner=None):
255 def interact(self, display_banner=None):
249 """Closely emulate the interactive Python console."""
256 """Closely emulate the interactive Python console."""
@@ -267,7 +274,7 b' class TerminalInteractiveShell(InteractiveShell):'
267
274
268 if self.has_readline:
275 if self.has_readline:
269 self.readline_startup_hook(self.pre_readline)
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 # exit_now is set by a call to %Exit or %Quit, through the
278 # exit_now is set by a call to %Exit or %Quit, through the
272 # ask_exit callback.
279 # ask_exit callback.
273
280
@@ -299,7 +306,8 b' class TerminalInteractiveShell(InteractiveShell):'
299 try:
306 try:
300 self.write('\nKeyboardInterrupt\n')
307 self.write('\nKeyboardInterrupt\n')
301 source_raw = self.input_splitter.source_raw_reset()[1]
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 more = False
311 more = False
304 except KeyboardInterrupt:
312 except KeyboardInterrupt:
305 pass
313 pass
@@ -328,7 +336,8 b' class TerminalInteractiveShell(InteractiveShell):'
328 if not more:
336 if not more:
329 source_raw = self.input_splitter.source_raw_reset()[1]
337 source_raw = self.input_splitter.source_raw_reset()[1]
330 self.run_cell(source_raw, store_history=True)
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 # We are off again...
342 # We are off again...
334 __builtin__.__dict__['__IPYTHON__active'] -= 1
343 __builtin__.__dict__['__IPYTHON__active'] -= 1
@@ -31,7 +31,7 b' class InteractiveShellTestCase(unittest.TestCase):'
31 ip = get_ipython()
31 ip = get_ipython()
32 ip.has_readline = False
32 ip.has_readline = False
33 ip.readline = None
33 ip.readline = None
34 ip._replace_rlhist_multiline(u'source')
34 ip._replace_rlhist_multiline(u'source', 0)
35
35
36 @skipif(not get_ipython().has_readline, 'no readline')
36 @skipif(not get_ipython().has_readline, 'no readline')
37 def test_replace_multiline_hist_disabled(self):
37 def test_replace_multiline_hist_disabled(self):
@@ -42,11 +42,12 b' class InteractiveShellTestCase(unittest.TestCase):'
42 ghist = [u'line1', u'line2']
42 ghist = [u'line1', u'line2']
43 for h in ghist:
43 for h in ghist:
44 ip.readline.add_history(h)
44 ip.readline.add_history(h)
45 ip.hlen_before_cell = ip.readline.get_current_history_length()
45 hlen_b4_cell = ip.readline.get_current_history_length()
46 ip._replace_rlhist_multiline(u'sourc€\nsource2')
46 hlen_b4_cell = ip._replace_rlhist_multiline(u'sourc€\nsource2',
47 hlen_b4_cell)
47
48
48 self.assertEquals(ip.readline.get_current_history_length(),
49 self.assertEquals(ip.readline.get_current_history_length(),
49 ip.hlen_before_cell)
50 hlen_b4_cell)
50 hist = self.rl_hist_entries(ip.readline, 2)
51 hist = self.rl_hist_entries(ip.readline, 2)
51 self.assertEquals(hist, ghist)
52 self.assertEquals(hist, ghist)
52
53
@@ -55,10 +56,10 b' class InteractiveShellTestCase(unittest.TestCase):'
55 """Test that multiline replace function adds history"""
56 """Test that multiline replace function adds history"""
56 ip = get_ipython()
57 ip = get_ipython()
57
58
58 ip.hlen_before_cell = ip.readline.get_current_history_length()
59 hlen_b4_cell = ip.readline.get_current_history_length()
59 ip._replace_rlhist_multiline(u'sourc€')
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 ip.readline.get_current_history_length())
63 ip.readline.get_current_history_length())
63
64
64 @skipif(not get_ipython().has_readline, 'no readline')
65 @skipif(not get_ipython().has_readline, 'no readline')
@@ -72,12 +73,13 b' class InteractiveShellTestCase(unittest.TestCase):'
72 ip.readline.add_history(h)
73 ip.readline.add_history(h)
73
74
74 #start cell
75 #start cell
75 ip.hlen_before_cell = ip.readline.get_current_history_length()
76 hlen_b4_cell = ip.readline.get_current_history_length()
76 # nothing added to rl history, should do nothing
77 # nothing added to rl history, should do nothing
77 ip._replace_rlhist_multiline(u'sourc€\nsource2')
78 hlen_b4_cell = ip._replace_rlhist_multiline(u'sourc€\nsource2',
79 hlen_b4_cell)
78
80
79 self.assertEquals(ip.readline.get_current_history_length(),
81 self.assertEquals(ip.readline.get_current_history_length(),
80 ip.hlen_before_cell)
82 hlen_b4_cell)
81 hist = self.rl_hist_entries(ip.readline, 2)
83 hist = self.rl_hist_entries(ip.readline, 2)
82 self.assertEquals(hist, ghist)
84 self.assertEquals(hist, ghist)
83
85
@@ -90,18 +92,20 b' class InteractiveShellTestCase(unittest.TestCase):'
90
92
91 ip.readline.add_history(u'line0')
93 ip.readline.add_history(u'line0')
92 #start cell
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 ip.readline.add_history('l€ne1')
96 ip.readline.add_history('l€ne1')
95 ip.readline.add_history('line2')
97 ip.readline.add_history('line2')
96 #replace cell with single line
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 ip.readline.add_history('l€ne3')
101 ip.readline.add_history('l€ne3')
99 ip.readline.add_history('line4')
102 ip.readline.add_history('line4')
100 #replace cell with single line
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 self.assertEquals(ip.readline.get_current_history_length(),
107 self.assertEquals(ip.readline.get_current_history_length(),
104 ip.hlen_before_cell)
108 hlen_b4_cell)
105 hist = self.rl_hist_entries(ip.readline, 3)
109 hist = self.rl_hist_entries(ip.readline, 3)
106 self.assertEquals(hist, ['line0', 'l€ne1\nline2', 'l€ne3\nline4'])
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 ip.readline.add_history(u'line0')
119 ip.readline.add_history(u'line0')
116 #start cell
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 ip.readline.add_history('l€ne1')
122 ip.readline.add_history('l€ne1')
119 ip.readline.add_history('line2')
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 ip.readline.add_history('')
126 ip.readline.add_history('')
122 ip._replace_rlhist_multiline(u'')
127 hlen_b4_cell = ip._replace_rlhist_multiline(u'', hlen_b4_cell)
123 ip.readline.add_history('l€ne3')
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 ip.readline.add_history(' ')
130 ip.readline.add_history(' ')
126 ip._replace_rlhist_multiline(' ')
131 hlen_b4_cell = ip._replace_rlhist_multiline(' ', hlen_b4_cell)
127 ip.readline.add_history('\t')
132 ip.readline.add_history('\t')
128 ip.readline.add_history('\t ')
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 ip.readline.add_history('line4')
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 self.assertEquals(ip.readline.get_current_history_length(),
138 self.assertEquals(ip.readline.get_current_history_length(),
134 ip.hlen_before_cell)
139 hlen_b4_cell)
135 hist = self.rl_hist_entries(ip.readline, 4)
140 hist = self.rl_hist_entries(ip.readline, 4)
136 # expect no empty cells in history
141 # expect no empty cells in history
137 self.assertEquals(hist, ['line0', 'l€ne1\nline2', 'l€ne3', 'line4'])
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