From 197ca03b9521cd37a3bf6ced53c65bd50b773b6d 2011-10-31 21:56:14
From: Julian Taylor <jtaylor.debian@googlemail.com>
Date: 2011-10-31 21:56:14
Subject: [PATCH] don't try to replace history when rl history unchanged

preserves readlines consecutive duplicate removal for single lines

---

diff --git a/IPython/frontend/terminal/interactiveshell.py b/IPython/frontend/terminal/interactiveshell.py
index 5f8d483..39bbc5a 100644
--- a/IPython/frontend/terminal/interactiveshell.py
+++ b/IPython/frontend/terminal/interactiveshell.py
@@ -233,6 +233,11 @@ class TerminalInteractiveShell(InteractiveShell):
         """Store multiple lines as a single entry in history"""
         if self.multiline_history and self.has_readline and source_raw.rstrip():
             hlen = self.readline.get_current_history_length()
+
+            # nothing changed do nothing, e.g. when rl removes consecutive dups
+            if self.hlen_before_cell == hlen:
+                return
+
             for i in range(hlen - self.hlen_before_cell):
                 self.readline.remove_history_item(hlen - i - 1)
             stdin_encoding = sys.stdin.encoding or "utf-8"
diff --git a/IPython/frontend/terminal/tests/test_interactivshell.py b/IPython/frontend/terminal/tests/test_interactivshell.py
index 50de5c3..85616eb 100644
--- a/IPython/frontend/terminal/tests/test_interactivshell.py
+++ b/IPython/frontend/terminal/tests/test_interactivshell.py
@@ -73,12 +73,13 @@ class InteractiveShellTestCase(unittest.TestCase):
 
         #start cell
         ip.hlen_before_cell = ip.readline.get_current_history_length()
+        # nothing added to rl history, should do nothing
         ip._replace_rlhist_multiline(u'sourc€\nsource2')
 
         self.assertEquals(ip.readline.get_current_history_length(),
                           ip.hlen_before_cell)
-        hist = self.rl_hist_entries(ip.readline, 3)
-        self.assertEquals(hist, ghist + ['sourc€\nsource2'])
+        hist = self.rl_hist_entries(ip.readline, 2)
+        self.assertEquals(hist, ghist)
 
 
     @skipif(not get_ipython().has_readline, 'no readline')