From ba74182e644390a1146c8749f88a5f0d226c14ba Mon Sep 17 00:00:00 2001 From: Ashwin Vishnu <9155111+ashwinvis@users.noreply.github.com> Date: Tue, 13 Oct 2020 06:48:45 +0200 Subject: [PATCH 1/4] Warn if user executes !pip or !conda Fixes #12204 --- IPython/core/interactiveshell.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 22b531ec159..dbaa1ce2d29 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2521,6 +2521,18 @@ def system_raw(self, cmd): Command to execute. """ cmd = self.var_expand(cmd, depth=1) + + # warn if there is an IPython magic alternative. + main_cmd = cmd.split()[0] + has_magic_alternatives = ("pip", "conda") + if main_cmd in has_magic_alternatives: + warnings.warn( + ( + "You executed the system command !{0} which may not work " + "as expected. Try the IPython magic %{0} instead." + ).format(main_cmd) + ) + # protect os.system from UNC paths on Windows, which it can't handle: if sys.platform == 'win32': from IPython.utils._process_win32 import AvoidUNCPath From e0c24607cb40bc4d2cc6f7859526db9a857ee51c Mon Sep 17 00:00:00 2001 From: Sammy Al Hashemi Date: Fri, 7 May 2021 12:09:30 -0400 Subject: [PATCH 2/4] Adding !cd and !ls warnings --- IPython/core/interactiveshell.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index dbaa1ce2d29..5e0119c06ea 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2521,11 +2521,16 @@ def system_raw(self, cmd): Command to execute. """ cmd = self.var_expand(cmd, depth=1) - # warn if there is an IPython magic alternative. main_cmd = cmd.split()[0] - has_magic_alternatives = ("pip", "conda") - if main_cmd in has_magic_alternatives: + has_magic_alternatives = ("pip", "conda", "cd", "ls") + + # had to check if the command was an alias expanded because of `ls` + is_alias_expanded = self.alias_manager.is_alias(main_cmd) and \ + (self.alias_manager.retrieve_alias(main_cmd).strip() + == cmd.strip()) + + if main_cmd in has_magic_alternatives and not is_alias_expanded: warnings.warn( ( "You executed the system command !{0} which may not work " From 7a458e040e3c8a7f6edec78b14159ba98ae337bf Mon Sep 17 00:00:00 2001 From: Sammy Al Hashemi Date: Fri, 7 May 2021 13:03:53 -0400 Subject: [PATCH 3/4] Fixed linter error --- IPython/core/interactiveshell.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 5e0119c06ea..47db3fe2b95 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2526,9 +2526,9 @@ def system_raw(self, cmd): has_magic_alternatives = ("pip", "conda", "cd", "ls") # had to check if the command was an alias expanded because of `ls` - is_alias_expanded = self.alias_manager.is_alias(main_cmd) and \ - (self.alias_manager.retrieve_alias(main_cmd).strip() - == cmd.strip()) + is_alias_expanded = self.alias_manager.is_alias(main_cmd) and ( + self.alias_manager.retrieve_alias(main_cmd).strip() == cmd.strip() + ) if main_cmd in has_magic_alternatives and not is_alias_expanded: warnings.warn( From a61fb992a51218f3f77cba733222af28634ac682 Mon Sep 17 00:00:00 2001 From: Sammy Al Hashemi Date: Fri, 7 May 2021 14:01:04 -0400 Subject: [PATCH 4/4] Adding test for magic warnings --- IPython/core/tests/test_interactiveshell.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/IPython/core/tests/test_interactiveshell.py b/IPython/core/tests/test_interactiveshell.py index 84bea9cd457..9092ce5b455 100644 --- a/IPython/core/tests/test_interactiveshell.py +++ b/IPython/core/tests/test_interactiveshell.py @@ -600,9 +600,16 @@ def test_control_c(self, *mocks): try: self.system("sleep 1 # wont happen") except KeyboardInterrupt: - self.fail("system call should intercept " - "keyboard interrupt from subprocess.call") - self.assertEqual(ip.user_ns['_exit_code'], -signal.SIGINT) + self.fail( + "system call should intercept " + "keyboard interrupt from subprocess.call" + ) + self.assertEqual(ip.user_ns["_exit_code"], -signal.SIGINT) + + def test_magic_warnings(self): + for magic_cmd in ("ls", "pip", "conda", "cd"): + with self.assertWarnsRegex(Warning, "You executed the system command"): + ip.system_raw(magic_cmd) # TODO: Exit codes are currently ignored on Windows. class TestSystemPipedExitCode(ExitCodeChecks):