Permalink
Browse files

'source' now always updates ${FUNCNAME[@]}.

This matches bash's behavior.

Pointed out by BatmanAoD.
  • Loading branch information...
Andy Chu
Andy Chu committed Jun 23, 2018
1 parent 094636c commit 1ad997dc72468ff4be6128ef8f070c0269a8f85f
Showing with 28 additions and 7 deletions.
  1. +2 −4 core/cmd_exec.py
  2. +7 −3 core/state.py
  3. +17 −0 spec/introspect.test.sh
  4. +2 −0 spec/testdata/echo-funcname.sh
View
@@ -254,13 +254,11 @@ def _Source(self, argv):
# the same variable scope as the caller. The caller could be at either a
# global or a local scope.
source_argv = argv[1:]
if source_argv:
self.mem.PushSourceArgv(source_argv)
self.mem.PushSourceArgv(source_argv)
try:
status = self._EvalHelper(c_parser, path)
finally:
if source_argv:
self.mem.PopSourceArgv()
self.mem.PopSourceArgv(source_argv)
return status
View
@@ -468,12 +468,16 @@ def PopCall(self):
def PushSourceArgv(self, argv):
"""For 'source foo.sh 1 2 3."""
# Match bash's behavior for ${FUNCNAME[@]}. But it would be nicer to add
# the name of the script here?
self.func_name_stack.append('source')
self.argv_stack.append(_ArgFrame(argv))
if argv:
self.argv_stack.append(_ArgFrame(argv))
def PopSourceArgv(self):
def PopSourceArgv(self, argv):
self.func_name_stack.pop()
self.argv_stack.pop()
if argv:
self.argv_stack.pop()
def PushTemp(self):
"""For the temporary scope in 'FOO=bar BAR=baz echo'."""
View
@@ -38,6 +38,22 @@ f
['f']
## END
### FUNCNAME with source
f() {
. spec/testdata/echo-funcname.sh
}
g() {
f
}
g
. spec/testdata/echo-funcname.sh
argv.py "${FUNCNAME[@]}"
## STDOUT:
['source', 'f', 'g']
['source']
[]
## END
### ${BASH_SOURCE[@]} is a stack of source files for function calls
$SH spec/testdata/bash-source.sh
## STDOUT:
@@ -84,3 +100,4 @@ f
['2']
['7']
## END
@@ -0,0 +1,2 @@
# For spec/introspect.test.sh
argv.py ${FUNCNAME[@]}

0 comments on commit 1ad997d

Please sign in to comment.