Skip to content

Commit e7b3565

Browse files
author
Michele Mesiti
committed
more robust logic to get last command exit value
fixes #151
1 parent f07a743 commit e7b3565

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

bash_kernel/exit_code_checker.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import re
2+
3+
exit_code_format = "BASHKERNEL_RETCODE<<<{}>>>"
4+
exit_code_re = re.compile(exit_code_format.format("([0-9]+)"))
5+
exit_code_printf_format = exit_code_format.format("%d")
6+
exit_code_command_checker = f'{{ printf "{exit_code_printf_format}" $?; }} 2>/dev/null'
7+
8+
9+
def get_last_exit_code(bashwrapper):
10+
output = bashwrapper.run_command(exit_code_command_checker)
11+
matches = exit_code_re.findall(output)
12+
return int(matches[0])

bash_kernel/kernel.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
version_pat = re.compile(r'version (\d+(\.\d+)+)')
1717

1818
from .display import (extract_contents, build_cmds)
19+
from .exit_code_checker import get_last_exit_code
1920

2021
class IREPLWrapper(replwrap.REPLWrapper):
2122
"""A subclass of REPLWrapper that gives incremental output
@@ -224,7 +225,7 @@ def do_execute(self, code, silent, store_history=True,
224225
return {'status': 'abort', 'execution_count': self.execution_count}
225226

226227
try:
227-
exitcode = int(self.bashwrapper.run_command('{ echo $?; } 2>/dev/null').rstrip().split("\r\n")[0])
228+
exitcode = get_last_exit_code(self.bashwrapper)
228229
except Exception as exc:
229230
exitcode = 1
230231

0 commit comments

Comments
 (0)