Subject: [BUG] Sticky-sh POSIX_TRAPS are function-local
From: Martijn Dekker <[email-masked]>
To: [email-masked]
Date: Mon, 15 Feb 2016 06:11:13 +0100
List-Id: Zsh Workers List <zsh-workers.zsh.org>

The POSIX_TRAPS option for the EXIT trap does not work if both of the
following conditions apply:

- zsh was launched as zsh, not sh
- the EXIT trap is set from a shell function with sticky sh emulation

Script showing the bug (on zsh-5.2-142-gac5d83b):

	#! /bin/zsh
	echo start program
	emulate sh -c 'testfn() {
	        echo start function
		set -o | grep posixtraps
		trap "echo EXIT TRAP TRIGGERED" EXIT
		echo end function
	}'
	testfn
	echo program continuing
	echo end of program

Actual output:

	start program
	start function
	noposixtraps          off
	end function
	EXIT TRAP TRIGGERED
	program continuing
	end of program

Expected output:

	start program
	start function
	noposixtraps          off
	end function
	program continuing
	end of program
	EXIT TRAP TRIGGERED

Note how the EXIT trap is triggered upon exit from the function that set
it, and not upon exit from the shell, even though 'emulate sh' should
activate POSIX_TRAPS so the function should have set a global trap.

Thanks,

- M.

(How I found this bug: my cross-platform shell library, modernish
<https://github.com/modernish/modernish>, can be used with native zsh
with the command
   emulate sh -c '. modernish'
Sticky sh emulation is fantastic: the library's features mix in
seamlessly with native zsh, even the crazy alias-based ones such as the
new loop constructs. The only thing that is *not* working are the
functions that set EXIT traps, i.e.. 'pushtrap' -- stack-based traps, so
modernish modules can set their own clean-up traps without interfering
with others. On native zsh with modernish in sticky sh emulation, those
get executed instantly upon setting them.)