Skip to content

Commit 6aeca89

Browse files
committed
Support system_raise_on_error in ZMQInteractiveShell
Add support for IPython's system_raise_on_error configuration option in ZMQInteractiveShell.system_piped() override. This complements the IPython PR that adds the system_raise_on_error config option (see ipython/ipython PR). Since ZMQInteractiveShell overrides system_piped() for Windows UNC path handling, it needs to also check the system_raise_on_error config and raise CalledProcessError on non-zero exit status. Changes: - Import CalledProcessError from subprocess - Capture exit_code from system() call - Raise CalledProcessError when system_raise_on_error is True and exit_code is non-zero This enables Jupyter notebook users to halt execution on shell command failures when they opt-in via: get_ipython().system_raise_on_error = True
1 parent c80f2d4 commit 6aeca89

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

ipykernel/zmqshell.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import contextvars
1818
import os
1919
import sys
20+
from subprocess import CalledProcessError
2021
import threading
2122
import typing
2223
import warnings
@@ -783,9 +784,15 @@ def system_piped(self, cmd):
783784
with AvoidUNCPath() as path:
784785
if path is not None:
785786
cmd = f"pushd {path} &&{cmd}"
786-
self.user_ns["_exit_code"] = system(cmd)
787+
exit_code = system(cmd)
788+
self.user_ns["_exit_code"] = exit_code
787789
else:
788-
self.user_ns["_exit_code"] = system(self.var_expand(cmd, depth=1))
790+
exit_code = system(self.var_expand(cmd, depth=1))
791+
self.user_ns["_exit_code"] = exit_code
792+
793+
# Raise an exception if the command failed and system_raise_on_error is True
794+
if self.system_raise_on_error and exit_code != 0:
795+
raise CalledProcessError(exit_code, cmd)
789796

790797
# Ensure new system_piped implementation is used
791798
system = system_piped

0 commit comments

Comments
 (0)