Skip to content

Commit 6e2f135

Browse files
committed
Allow Pyrun to deal with sys.exit termination
1 parent 5eb0022 commit 6e2f135

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

autoload/pymode/run.vim

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,18 @@ fun! pymode#run#Run(line1, line2) "{{{
1717
try
1818
py context = globals()
1919
py context['raw_input'] = context['input'] = lambda s: vim.eval('input("{0}")'.format(s))
20-
py execfile(vim.eval('expand("%:p")'), context)
20+
python << ENDPYTHON
21+
try:
22+
execfile(vim.eval('expand("%:p")'), context)
23+
# Vim cannot handle a SystemExit error raised by Python, so we need to trap it here, and
24+
# handle it specially
25+
except SystemExit as e:
26+
if e.code:
27+
# A non-false code indicates abnormal termination. A false code will be treated as a
28+
# successful run, and the error will be hidden from Vim
29+
vim.command('echohl Error | echo "Script exited with code {0}" | echohl none'.format(e.code))
30+
vim.command('return')
31+
ENDPYTHON
2132
py out, err = sys.stdout.getvalue().strip(), sys.stderr.getvalue()
2233
py sys.stdout, sys.stderr = stdout_, stderr_
2334

@@ -38,7 +49,7 @@ else:
3849
EOF
3950

4051
catch /.*/
41-
52+
4253
echohl Error | echo "Run-time error." | echohl none
4354

4455
endtry

0 commit comments

Comments
 (0)