Subject: Re: [BUG] Sticky-sh POSIX_TRAPS are function-local
From: Peter Stephenson <[email-masked]>
To: [email-masked]
Date: Thu, 25 Feb 2016 11:53:39 +0000
List-Id: Zsh Workers List <zsh-workers.zsh.org>

On Wed, 17 Feb 2016 00:38:15 +0100
Martijn Dekker <[email-masked]> wrote:
> However, since POSIX traps are inherently global, the expectation that
> POSIX traps wipe out previous traps only applies to global traps. Could
> you make an exception for function-local traps?

I think this makes nested native mode EXIT traps work while preserving
a global POSIX mode EXIT trap.

pws

diff --git a/Src/signals.c b/Src/signals.c
index 32452ae..1344395 100644
--- a/Src/signals.c
+++ b/Src/signals.c
@@ -920,9 +920,14 @@ removetrap(int sig)
      * Note that we save the trap here even if there isn't an existing
      * one, to aid in removing this one.  However, if there's
      * already one at the current locallevel we just overwrite it.
+     *
+     * Note we save EXIT traps based on the *current* setting of
+     * POSIXTRAPS --- so if there is POSIX EXIT trap set but
+     * we are in native mode it can be saved, replaced by a function
+     * trap, and then restored.
      */
     if (!dontsavetrap &&
-	(sig == SIGEXIT ? !exit_trap_posix : isset(LOCALTRAPS)) &&
+	(sig == SIGEXIT ? !isset(POSIXTRAPS) : isset(LOCALTRAPS)) &&
 	locallevel &&
 	(!trapped || locallevel > (sigtrapped[sig] >> ZSIG_SHIFT)))
 	dosavetrap(sig, locallevel);
diff --git a/Test/C03traps.ztst b/Test/C03traps.ztst
index d8183a4..f4466b5 100644
--- a/Test/C03traps.ztst
+++ b/Test/C03traps.ztst
@@ -419,6 +419,23 @@
 >end of program
 >EXIT TRAP TRIGGERED
 
+   (cd ..; $ZTST_exe -fc '
+     echo entering program
+     emulate sh -c '\''trap "echo POSIX exit trap triggered" EXIT'\''
+     fn() {
+        trap "echo native zsh function-local exit trap triggered" EXIT
+        echo entering native zsh function
+     }
+     fn
+     echo exiting program
+   ')
+0:POSX EXIT trap can have nested native mode EXIT trap
+>entering program
+>entering native zsh function
+>native zsh function-local exit trap triggered
+>exiting program
+>POSIX exit trap triggered
+
    (set -e
     printf "a\nb\n" | while read line
     do