From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8142 invoked from network); 1 Oct 2005 12:05:34 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 1 Oct 2005 12:05:34 -0000 Received: (qmail 11462 invoked from network); 1 Oct 2005 12:05:28 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 1 Oct 2005 12:05:28 -0000 Received: (qmail 4105 invoked by alias); 1 Oct 2005 12:05:24 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 21789 Received: (qmail 12109 invoked from network); 30 Sep 2005 18:43:42 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 30 Sep 2005 18:43:42 -0000 Received: (qmail 89914 invoked from network); 30 Sep 2005 18:43:42 -0000 Received: from ns9.hostinglmi.net (213.194.149.146) by a.mx.sunsite.dk with SMTP; 30 Sep 2005 18:43:40 -0000 Received: from 212.red-80-35-44.staticip.rima-tde.net ([80.35.44.212] helo=localhost) by ns9.hostinglmi.net with esmtpa (Exim 4.52) id 1ELPrE-0002YM-0b for zsh-workers@sunsite.dk; Fri, 30 Sep 2005 20:43:44 +0200 Date: Fri, 30 Sep 2005 20:44:03 +0200 From: DervishD To: Zsh Workers Subject: Fwd: [Exception handling and "trap" vs. TRAPNAL()] Message-ID: <20050930184403.GA617@DervishD> Mail-Followup-To: Zsh Workers Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit User-Agent: Mutt/1.4.2.1i Organization: DervishD X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - ns9.hostinglmi.net X-AntiAbuse: Original Domain - sunsite.dk X-AntiAbuse: Originator/Caller UID/GID - [0 0] / [47 12] X-AntiAbuse: Sender Address Domain - dervishd.net X-Source: X-Source-Args: X-Source-Dir: X-Spam-Checker-Version: SpamAssassin 3.0.4 (2005-06-05) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.0.4 Hi all :) I'm forwarding this message here after looking at the zsh-workers mail archive. There have been problems with "trap" in the past, specially when saving/restoring state, and this problem is maybe caused by this (or my stupidity, who knows...). Thanks a lot in advance :) ----- Forwarded message from DervishD ----- > Date: Thu, 29 Sep 2005 22:07:41 +0200 > From: DervishD > To: Zsh Users > Subject: Exception handling and "trap" vs. TRAPNAL() > > Hi all :) > > Let's say we have this script, using exception handling: > > --- cut here --- > #!/bin/zsh > > emulate -L zsh > > [[ "$1" = "trap" ]] && trap 'throw DEFAULT' ZERR > [[ "$1" = "TRAPZERR" ]] && function TRAPZERR() { throw DEFAULT ; } > > function throw() { > typeset -g EXCEPTION="$1" > readonly THROW=0 > if (( TRY_BLOCK_ERROR == 0 )); then > (( TRY_BLOCK_ERROR = 1 )) > fi > THROW= 2>/dev/null > } > > function catch() { > typeset -g CAUGHT > if [[ $TRY_BLOCK_ERROR -gt 0 && $EXCEPTION = ${~1} ]]; then > (( TRY_BLOCK_ERROR = 0 )) > CAUGHT="$EXCEPTION" > unset EXCEPTION > return 0 > fi > return 1 > } > > alias catch="noglob catch" > > > { > print "Before throwing" > # This should throw "DEFAULT" exception > false > throw EXCEPTION > # This shouldn't be shown > print "After throwing" > } always { > catch * && print "Caught exception $CAUGHT" > return 0 > } > > --- cut here --- > > Sorry for the size, but I want to make sure it is as > self-contained as possible. Well, I do the following: > > $ ./script trap > Before throwing > Caught exception EXCEPTION > > $ ./script TRAPZERR > Before throwing > Caught exception DEFAULT > > $ ./script > Before throwing > Caught exception EXCEPTION > > I'm puzzled. The "trap" trap is executed in the current > environment, so I assume it would throw "DEFAULT", as intended, as > soon as we hit the "false". It doesn't and I don't know why. OTOH, > the TRAPZERR function, which runs "throw" in its own environment, > works ok :??? WHY? > > AFAIK the "trap" builtin runs its arguments in the current > environment, so to say it's like an alias, like cutting and pasting > the code, but anyway that shouldn't be the problem, since "throw" is > using globals and so it affects running in any environment. > > The only thing that could be happening is that the ZERR trap > using "trap" is not setting "TRY_BLOCK_ERROR" to "1", or it sets the > variable but the variable gets reset upon exiting the trap :??? > > Can anybody (and I really mean anybody, not Bart XD) explain this > to me? I would like to use something similar and I need to use "trap" > traps and not "TRAPNAL()" traps because I want to use LINENO and > probably a couple of variables which are local to the functions where > the ZERR trap can be raised. > > Thanks a lot in advance :) > > Raúl Núñez de Arenas Coronado > > -- > Linux Registered User 88736 | http://www.dervishd.net > http://www.pleyades.net & http://www.gotesdelluna.net > It's my PC and I'll cry if I want to... > > ----- End forwarded message ----- Raúl Núñez de Arenas Coronado -- Linux Registered User 88736 | http://www.dervishd.net http://www.pleyades.net & http://www.gotesdelluna.net It's my PC and I'll cry if I want to...