diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 12503e9..f158ef2 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2423,6 +2423,14 @@ class InteractiveShell(SingletonConfigurable): with self.builtin_trap: args = (magic_arg_s, cell) result = fn(*args, **kwargs) + + # The code below prevents the output from being displayed + # when using magics with decodator @output_can_be_silenced + # when the last Python token in the expression is a ';'. + if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False): + if DisplayHook.semicolon_at_end_of_expression(cell): + return None + return result def find_line_magic(self, magic_name): @@ -3199,6 +3207,7 @@ class InteractiveShell(SingletonConfigurable): # Execute the user code interactivity = "none" if silent else self.ast_node_interactivity + has_raised = await self.run_ast_nodes(code_ast.body, cell_name, interactivity=interactivity, compiler=compiler, result=result) diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index 55408d4..e64b959 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -422,6 +422,7 @@ def test_time(): def test_time_no_output_with_semicolon(): ip = get_ipython() + # Test %time cases with tt.AssertPrints(" 123456"): with tt.AssertPrints("Wall time: ", suppress=False): with tt.AssertPrints("CPU times: ", suppress=False): @@ -447,6 +448,32 @@ def test_time_no_output_with_semicolon(): with tt.AssertPrints("CPU times: ", suppress=False): ip.run_cell("%time 123000+456 # ;Comment") + # Test %%time cases + with tt.AssertPrints("123456"): + with tt.AssertPrints("Wall time: ", suppress=False): + with tt.AssertPrints("CPU times: ", suppress=False): + ip.run_cell("%%time\n123000+456\n\n\n") + + with tt.AssertNotPrints("123456"): + with tt.AssertPrints("Wall time: ", suppress=False): + with tt.AssertPrints("CPU times: ", suppress=False): + ip.run_cell("%%time\n123000+456;\n\n\n") + + with tt.AssertPrints("123456"): + with tt.AssertPrints("Wall time: ", suppress=False): + with tt.AssertPrints("CPU times: ", suppress=False): + ip.run_cell("%%time\n123000+456 # Comment\n\n\n") + + with tt.AssertNotPrints("123456"): + with tt.AssertPrints("Wall time: ", suppress=False): + with tt.AssertPrints("CPU times: ", suppress=False): + ip.run_cell("%%time\n123000+456; # Comment\n\n\n") + + with tt.AssertPrints("123456"): + with tt.AssertPrints("Wall time: ", suppress=False): + with tt.AssertPrints("CPU times: ", suppress=False): + ip.run_cell("%%time\n123000+456 # ;Comment\n\n\n") + def test_time_last_not_expression(): ip.run_cell("%%time\n"