Call the given cmd in a subprocess using os.system on Windows or subprocess.call using the system shell on other platforms. Parameters ---------- cmd : str Command to execute.
(self, cmd)
| 2475 | self.user_ns['_exit_code'] = system(self.var_expand(cmd, depth=1)) |
| 2476 | |
| 2477 | def system_raw(self, cmd): |
| 2478 | """Call the given cmd in a subprocess using os.system on Windows or |
| 2479 | subprocess.call using the system shell on other platforms. |
| 2480 | |
| 2481 | Parameters |
| 2482 | ---------- |
| 2483 | cmd : str |
| 2484 | Command to execute. |
| 2485 | """ |
| 2486 | cmd = self.var_expand(cmd, depth=1) |
| 2487 | # protect os.system from UNC paths on Windows, which it can't handle: |
| 2488 | if sys.platform == 'win32': |
| 2489 | from IPython.utils._process_win32 import AvoidUNCPath |
| 2490 | with AvoidUNCPath() as path: |
| 2491 | if path is not None: |
| 2492 | cmd = '"pushd %s &&"%s' % (path, cmd) |
| 2493 | try: |
| 2494 | ec = os.system(cmd) |
| 2495 | except KeyboardInterrupt: |
| 2496 | print('\n' + self.get_exception_only(), file=sys.stderr) |
| 2497 | ec = -2 |
| 2498 | else: |
| 2499 | # For posix the result of the subprocess.call() below is an exit |
| 2500 | # code, which by convention is zero for success, positive for |
| 2501 | # program failure. Exit codes above 128 are reserved for signals, |
| 2502 | # and the formula for converting a signal to an exit code is usually |
| 2503 | # signal_number+128. To more easily differentiate between exit |
| 2504 | # codes and signals, ipython uses negative numbers. For instance |
| 2505 | # since control-c is signal 2 but exit code 130, ipython's |
| 2506 | # _exit_code variable will read -2. Note that some shells like |
| 2507 | # csh and fish don't follow sh/bash conventions for exit codes. |
| 2508 | executable = os.environ.get('SHELL', None) |
| 2509 | try: |
| 2510 | # Use env shell instead of default /bin/sh |
| 2511 | ec = subprocess.call(cmd, shell=True, executable=executable) |
| 2512 | except KeyboardInterrupt: |
| 2513 | # intercept control-C; a long traceback is not useful here |
| 2514 | print('\n' + self.get_exception_only(), file=sys.stderr) |
| 2515 | ec = 130 |
| 2516 | if ec > 128: |
| 2517 | ec = -(ec - 128) |
| 2518 | |
| 2519 | # We explicitly do NOT return the subprocess status code, because |
| 2520 | # a non-None value would trigger :func:`sys.displayhook` calls. |
| 2521 | # Instead, we store the exit_code in user_ns. Note the semantics |
| 2522 | # of _exit_code: for control-c, _exit_code == -signal.SIGNIT, |
| 2523 | # but raising SystemExit(_exit_code) will give status 254! |
| 2524 | self.user_ns['_exit_code'] = ec |
| 2525 | |
| 2526 | # use piped system by default, because it is better behaved |
| 2527 | system = system_piped |