Skip to content
This repository

Drop consecutive duplicates when refilling readline history #1003

Merged
merged 4 commits into from over 2 years ago

3 participants

Thomas Kluyver Fernando Perez Min RK
Thomas Kluyver
Collaborator

I'd better fix that bug if I'm getting a tshirt for it.

This operates at the cell level, even if the user disables multiline_history in the terminal, so you can define a multiline function twice, and only have to flip through it once after you start a new session.

There's an added bonus fix for setting the next input at the terminal on Python 3.

Thomas Kluyver
Collaborator

Aside: the equivalent thing also needs fixing in the Qt console. I'll see if I can track down the relevant code and add it to this PR.

Thomas Kluyver
Collaborator

Found the relevant bit for the Qt console too.

IPython/core/interactiveshell.py
@@ -1804,6 +1806,7 @@ class InteractiveShell(SingletonConfigurable, Magic):
1804 1806
                     for line in cell.splitlines():
1805 1807
                         self.readline.add_history(py3compat.unicode_to_str(line,
1806 1808
                                                                            stdin_encoding))
  1809
+                last_cell = cell.rstrip()
2
Min RK Owner
minrk added a note November 15, 2011

You can eliminate these three strip()/rstrip() calls by using a single stripped = cell.rstrip(), and if stripped and stripped != last_cell:, yes?

Thomas Kluyver Collaborator

Good point. Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Fernando Perez
Owner

Looks great, works great, and fixes a really annoying little regression.

Great job, as always. Totally deserving of the issue #1000 award!! :) Will merge now, thanks a lot.

Fernando Perez fperez merged commit 5e71407 into from November 20, 2011
Fernando Perez fperez closed this November 20, 2011
Fernando Perez fperez referenced this pull request from a commit January 10, 2012
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
16  IPython/core/interactiveshell.py
@@ -1794,16 +1794,20 @@ def refill_readline_hist(self):
1794 1794
         # Load the last 1000 lines from history
1795 1795
         self.readline.clear_history()
1796 1796
         stdin_encoding = sys.stdin.encoding or "utf-8"
  1797
+        last_cell = u""
1797 1798
         for _, _, cell in self.history_manager.get_tail(1000,
1798 1799
                                                         include_latest=True):
1799  
-            if cell.strip(): # Ignore blank lines
  1800
+            # Ignore blank lines and consecutive duplicates
  1801
+            cell = cell.rstrip()
  1802
+            if cell and (cell != last_cell):
1800 1803
                 if self.multiline_history:
1801  
-                      self.readline.add_history(py3compat.unicode_to_str(cell.rstrip(),
1802  
-                                                                         stdin_encoding))
  1804
+                      self.readline.add_history(py3compat.unicode_to_str(cell,
  1805
+                                                                stdin_encoding))
1803 1806
                 else:
1804 1807
                     for line in cell.splitlines():
1805 1808
                         self.readline.add_history(py3compat.unicode_to_str(line,
1806  
-                                                                           stdin_encoding))
  1809
+                                                                stdin_encoding))
  1810
+                last_cell = cell
1807 1811
 
1808 1812
     def set_next_input(self, s):
1809 1813
         """ Sets the 'default' input string for the next command line.
@@ -1815,9 +1819,7 @@ def set_next_input(self, s):
1815 1819
         [D:\ipython]|1> _ip.set_next_input("Hello Word")
1816 1820
         [D:\ipython]|2> Hello Word_  # cursor is here
1817 1821
         """
1818  
-        if isinstance(s, unicode):
1819  
-            s = s.encode(self.stdin_encoding, 'replace')
1820  
-        self.rl_next_input = s
  1822
+        self.rl_next_input = py3compat.cast_bytes_py2(s)
1821 1823
 
1822 1824
     # Maybe move this to the terminal subclass?
1823 1825
     def pre_readline(self):
8  IPython/frontend/qt/console/ipython_widget.py
@@ -197,7 +197,13 @@ def _handle_history_reply(self, msg):
197 197
         # reset retry flag
198 198
         self._retrying_history_request = False
199 199
         history_items = content['history']
200  
-        items = [ line.rstrip() for _, _, line in history_items ]
  200
+        items = []
  201
+        last_cell = u""
  202
+        for _, _, cell in history_items:
  203
+            cell = cell.rstrip()
  204
+            if cell != last_cell:
  205
+                items.append(cell)
  206
+                last_cell = cell
201 207
         self._set_history(items)
202 208
 
203 209
     def _handle_pyout(self, msg):
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.