Permalink
Browse files

Allow returning from a sourced script.

Fixes issue #53.
  • Loading branch information...
Andy Chu
Andy Chu committed Nov 20, 2017
1 parent f45c9fa commit 0ab40d570b7e89fb6ceb85afaa91707670063faf
Showing with 17 additions and 0 deletions.
  1. +5 −0 core/cmd_exec.py
  2. +7 −0 spec/builtins.test.sh
  3. +5 −0 spec/return-helper.sh
View
@@ -197,6 +197,11 @@ def _Source(self, argv):
line_reader = reader.FileLineReader(f, self.arena)
_, c_parser = parse_lib.MakeParser(line_reader, self.arena)
return self._EvalHelper(c_parser, path)
except _ControlFlow as e:
if e.IsReturn():
return e.ReturnValue()
else:
raise
except IOError as e:
# TODO: Should point to the source statement that failed.
util.error('source %r failed: %s', path, os.strerror(e.errno))
View
@@ -95,6 +95,13 @@ echo status=$?
# OK bash stdout: status=2
# OK dash stdout: status=127
### Source script that returns
echo one
. spec/return-helper.sh
echo $?
echo two
# stdout-json: "one\nreturn-helper.sh\n42\ntwo\n"
### Exit builtin
exit 3
# status: 3
View
@@ -0,0 +1,5 @@
echo 'return-helper.sh'
return 42
echo 'should not see'

0 comments on commit 0ab40d5

Please sign in to comment.