Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions IPython/core/interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -2521,6 +2521,23 @@ 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", "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 "
"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
Expand Down
13 changes: 10 additions & 3 deletions IPython/core/tests/test_interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down