From 05bfcf38788982320d54550b3aeb0f5853aedb7f 2018-12-12 03:13:39 From: Jesse Widner Date: 2018-12-12 03:13:39 Subject: [PATCH] Adds posix aliases after a %reset. For example, running %reset in IPython, and then trying to run %clear raises a UsageError. --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index f98e49c..a6c0ebb 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -1433,6 +1433,13 @@ class InteractiveShell(SingletonConfigurable): self.alias_manager.clear_aliases() self.alias_manager.init_aliases() + # Now define aliases that only make sense on the terminal, because they + # need direct access to the console in a way that we can't emulate in + # GUI or web frontend + if os.name == 'posix': + for cmd in ('clear', 'more', 'less', 'man'): + self.alias_manager.soft_define_alias(cmd, cmd) + # Flush the private list of module references kept for script # execution protection self.clear_main_mod_cache() diff --git a/IPython/core/tests/test_interactiveshell.py b/IPython/core/tests/test_interactiveshell.py index 39fa41b..26dbd0d 100644 --- a/IPython/core/tests/test_interactiveshell.py +++ b/IPython/core/tests/test_interactiveshell.py @@ -495,6 +495,16 @@ class InteractiveShellTestCase(unittest.TestCase): self.assertFalse(ip.last_execution_result.success) self.assertIsInstance(ip.last_execution_result.error_in_exec, NameError) + def test_reset_aliasing(self): + """ Check that standard posix aliases work after %reset. """ + if os.name != 'posix': + return + + ip.reset() + for cmd in ('clear', 'more', 'less', 'man'): + res = ip.run_cell('%' + cmd) + self.assertEqual(res.success, True) + class TestSafeExecfileNonAsciiPath(unittest.TestCase): diff --git a/IPython/terminal/interactiveshell.py b/IPython/terminal/interactiveshell.py index 6fc8980..d31cc6a 100644 --- a/IPython/terminal/interactiveshell.py +++ b/IPython/terminal/interactiveshell.py @@ -447,7 +447,7 @@ class TerminalInteractiveShell(InteractiveShell): # need direct access to the console in a way that we can't emulate in # GUI or web frontend if os.name == 'posix': - for cmd in ['clear', 'more', 'less', 'man']: + for cmd in ('clear', 'more', 'less', 'man'): self.alias_manager.soft_define_alias(cmd, cmd)