From 5ed16ec8971328bfb9accdc2b8e35e2077691492 2014-03-20 13:00:22 From: Shashi Gowda Date: 2014-03-20 13:00:22 Subject: [PATCH] Suppress output even when a comment follows ;. Fixes #4525. --- diff --git a/IPython/core/displayhook.py b/IPython/core/displayhook.py index 57ad39d..cd4f77e 100644 --- a/IPython/core/displayhook.py +++ b/IPython/core/displayhook.py @@ -26,6 +26,7 @@ from __future__ import print_function import sys from IPython.core.formatters import _safe_get_formatter_method +from IPython.core import inputsplitter from IPython.config.configurable import Configurable from IPython.utils import io from IPython.utils.py3compat import builtin_mod @@ -100,12 +101,10 @@ class DisplayHook(Configurable): # do not print output if input ends in ';' try: cell = self.shell.history_manager.input_hist_parsed[self.prompt_count] - if cell.rstrip().endswith(';'): - return True + return inputsplitter.remove_comments(cell).rstrip().endswith(';') except IndexError: # some uses of ipshellembed may fail here - pass - return False + return False def start_displayhook(self): """Start the displayhook, initializing resources.""" diff --git a/IPython/core/tests/test_interactiveshell.py b/IPython/core/tests/test_interactiveshell.py index 5a1f795..213e95e 100644 --- a/IPython/core/tests/test_interactiveshell.py +++ b/IPython/core/tests/test_interactiveshell.py @@ -98,13 +98,17 @@ class InteractiveShellTestCase(unittest.TestCase): def test_dont_cache_with_semicolon(self): "Ending a line with semicolon should not cache the returned object (GH-307)" oldlen = len(ip.user_ns['Out']) - a = ip.run_cell('1;', store_history=True) - newlen = len(ip.user_ns['Out']) - self.assertEqual(oldlen, newlen) + for cell in ['1;', '1; #a', '1;1;']: + ip.run_cell(cell, store_history=True) + newlen = len(ip.user_ns['Out']) + self.assertEqual(oldlen, newlen) + i = 0 #also test the default caching behavior - ip.run_cell('1', store_history=True) - newlen = len(ip.user_ns['Out']) - self.assertEqual(oldlen+1, newlen) + for cell in ['1', '1 #;', '1;1']: + ip.run_cell(cell, store_history=True) + newlen = len(ip.user_ns['Out']) + i += 1 + self.assertEqual(oldlen+i, newlen) def test_In_variable(self): "Verify that In variable grows with user input (GH-284)"