From 47045d65798f28c9ef1e0846771b9274a1777346 2013-09-01 23:28:48 From: Théophile Studer Date: 2013-09-01 23:28:48 Subject: [PATCH] Use default OS shell to run system commands Instead of using os.system which uses /bin/sh, this uses subprocess.call (the replacement of os.system) to run the command using the default shell of the OS. With this, one can use more advanced commands for bash, zsh, ksh, ... I also edited the docstring, added comments and fixed the handling of return codes. --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index d7ec6c8..d8c080b 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2218,7 +2218,8 @@ class InteractiveShell(SingletonConfigurable): self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) def system_raw(self, cmd): - """Call the given cmd in a subprocess using os.system + """Call the given cmd in a subprocess using os.system on Windows or + subprocess.call using the system shell on other platforms. Parameters ---------- @@ -2236,11 +2237,12 @@ class InteractiveShell(SingletonConfigurable): ec = os.system(cmd) else: cmd = py3compat.unicode_to_str(cmd) - ec = subprocess.call(cmd, shell=True, executable=os.environ.get('SHELL')) - # The high byte is the exit code, the low byte is a signal number - # that we discard for now. See the docs for os.wait() - if ec > 255: - ec >>= 8 + # Call the cmd using the OS shell, instead of the default /bin/sh, if set. + ec = subprocess.call(cmd, shell=True, executable=os.environ.get('SHELL', None)) + # Returns either the exit code or minus the value of the signal number. + # See the docs for subprocess.Popen.returncode . + if ec < 0: + ec = -ec # We explicitly do NOT return the subprocess status code, because # a non-None value would trigger :func:`sys.displayhook` calls.