* How to set exit trap from inner function?
@ 2023-02-24 2:08 Sebastian Gniazdowski
2023-02-24 23:41 ` Bart Schaefer
0 siblings, 1 reply; 3+ messages in thread
From: Sebastian Gniazdowski @ 2023-02-24 2:08 UTC (permalink / raw)
To: Zsh hackers list
[-- Attachment #1: Type: text/plain, Size: 968 bytes --]
I want to automatically clear all functions created from outer function.
auto-unset-funcs() {
# Don't leak any functions
typeset -ga ef
ef=( ${(k)functions} )
# No localtraps – sets in outside scope
trap "unset -f -- \"\${(k)functions[@]:|ef}\" &>/dev/null; unset ef"
EXIT
trap "return 1" INT
}
Above code correctly clears new functions when pasted into the outer
function. However, I would like to avoid pasting and simply do:
outer() {
emulate -L zsh
auto-unset-funcs
# freely define new functions…
}
However, the EXIT trap is executed when auto-unset-funcs exits, not when
outer() does… I thought that lack of localoptions localtraps would declare
the EXIT trap in the first function that has it (outer does, via emulate
-L). Doing setopt localoptions nolocaltraps in auto-unset-funcs didn't
help. Any ideas on having such auto-unset-funcs function?
--
Best regards,
Sebastian Gniazdowski
[-- Attachment #2: Type: text/html, Size: 2088 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: How to set exit trap from inner function?
2023-02-24 2:08 How to set exit trap from inner function? Sebastian Gniazdowski
@ 2023-02-24 23:41 ` Bart Schaefer
2023-02-26 9:56 ` Sebastian Gniazdowski
0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2023-02-24 23:41 UTC (permalink / raw)
To: Sebastian Gniazdowski; +Cc: Zsh hackers list
On Thu, Feb 23, 2023 at 6:10 PM Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> I want to automatically clear all functions created from outer function.
>
> Any ideas on having such auto-unset-funcs function?
Unclear what side-effect messing about with localtraps may have on
this, but you can use one EXIT trap to set another EXIT trap:
inner () {
trap "print FROM INNER: \$0; trap 'print FROM OUTER: \$0' EXIT" EXIT
}
outer () {
inner
}
% outer
FROM INNER: outer
FROM OUTER: Src/zsh
Getting the quoting right for something more complicated, is left as
an exercise.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: How to set exit trap from inner function?
2023-02-24 23:41 ` Bart Schaefer
@ 2023-02-26 9:56 ` Sebastian Gniazdowski
0 siblings, 0 replies; 3+ messages in thread
From: Sebastian Gniazdowski @ 2023-02-26 9:56 UTC (permalink / raw)
To: Bart Schaefer; +Cc: Zsh hackers list
[-- Attachment #1: Type: text/plain, Size: 927 bytes --]
Thanks. I've found a less invasive solution – to name all inner functions
witu tmp/… prefix, and unset them with nset -f -m tmp/\*
On Fri, 24 Feb 2023 at 23:41, Bart Schaefer <schaefer@brasslantern.com>
wrote:
> On Thu, Feb 23, 2023 at 6:10 PM Sebastian Gniazdowski
> <sgniazdowski@gmail.com> wrote:
> >
> > I want to automatically clear all functions created from outer function.
> >
> > Any ideas on having such auto-unset-funcs function?
>
> Unclear what side-effect messing about with localtraps may have on
> this, but you can use one EXIT trap to set another EXIT trap:
>
> inner () {
> trap "print FROM INNER: \$0; trap 'print FROM OUTER: \$0' EXIT" EXIT
> }
> outer () {
> inner
> }
>
> % outer
> FROM INNER: outer
> FROM OUTER: Src/zsh
>
>
> Getting the quoting right for something more complicated, is left as
> an exercise.
>
--
Best regards,
Sebastian Gniazdowski
[-- Attachment #2: Type: text/html, Size: 1631 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-02-26 9:57 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-24 2:08 How to set exit trap from inner function? Sebastian Gniazdowski
2023-02-24 23:41 ` Bart Schaefer
2023-02-26 9:56 ` Sebastian Gniazdowski
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).