zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: DervishD <zsh@dervishd.net>
Cc: Peter Stephenson <pws@csr.com>, zsh-workers@sunsite.dk
Subject: Re: Exception handling and "trap" vs. TRAPNAL()
Date: Sun, 02 Oct 2005 04:40:52 +0000	[thread overview]
Message-ID: <1051002044052.ZM28373@candle.brasslantern.com> (raw)
In-Reply-To: <20051001202856.GA134@DervishD>

On Oct 1, 10:28pm, DervishD wrote:
}
} I am still not sure whether this is a bug or a feature:
} 
}     trap 'false' DEBUG
} 
}     true
}     [[ $? -eq 1 ]] && print "What?"
} 
}     This snippet doesn't print anything, and won't do even if using
} TRAPDEBUG and "return 1" instead of "false".

Guess what.

The bit of documentation that PWS quoted earlier is significantly at
odds with the actual implementation.

Here's that paragraph again:

  The return status from the function is handled specially.  If it is
  zero, the signal is assumed to have been handled, and execution
  continues normally.

So far we're right on the mark.

  Otherwise, the normal effect of the signal is produced;

This is clearly inaccurate.  If it were accurate, a non-zero return
from TRAPHUP, TRAPABRT, or TRAPALRM (among others) ought to cause the
shell to exit.  This demonstrably does not happen.  What *does* happen
is that all levels of nested loop are broken (as if by the "break N"
command with a sufficiently large N) and the shell is forced to behave
as if a shell-level error occurred.

(I'll note in passing that it actually has to be a GREATER THAN zero
return value, not just non-zero; "return -1" doesn't cause any of the
effects discussed here.  But this bit of doc is already so far wrong
that signedness makes little difference.)

However, this effect is only achieved in two cases: (1) An explicit
"return" command is used; it's a side-effect of the "return" builtin
that the value of $? is recorded as the return value of the trap.
(2) The TRAPNAL function itself exits with a shell-level error, such
as a syntax error or an assignment to a read-only variable.  Merely
falling off the end of the function with $? > 0 is not sufficient.

It's my contention that case (2) above is an oddball.  It ought to
behave the same way for "TRAPNAL() { ... }" and "trap ... NAL".  That
it does not is because the same local variable is overloaded to mean
both "$? > 0 was true" and "error condition was true", not because of
some strange special-case.

  if this causes execution to terminate, the status returned to the
  shell is the status returned from the function.

This is also demonstrably wrong.  Both cases above cause the return
status to be set via the "behave as if a shell-level error occurred"
reaction, so usually $? == 1.  The side-effect of "return" on $? lasts
only long enough to set up the forced error, then is erased as part
of unwinding the trap context.

In fact, ONLY in the case of "trap 'return X' NAL" is $? = X caused
to happen, and that's because the trap executes in the context of the
caller and forces the caller itself to return.  This behavior of the
return builtin in an "inline trap" is not documented anywhere as far
as I can tell, but you can watch it in action (and badly cripple
your shell) by writing --

    trap return DEBUG

-- and then attempting to execute any function that has more than one
command in the body.

A much more accurate description of the behavior of TRAPNAL can be
found under the "return" command entry:

  If return was executed from a trap in a TRAPNAL function, the
  effect is different for zero and non-zero return status.  With zero
  status (or after an implicit return at the end of the trap), the
  shell will return to whatever it was previously processing; with a
  non-zero status, the shell will behave as interrupted except that
  the return status of the trap is retained.

Aha!  "Behave as if interrupted" NOT "the normal effect of the signal."
The doc under "TRAPNAL" needs to be repaired.  (Here's another case of
zsh code overloading a variable; "interrupt" and "shell-level error"
are indistinguishable after a certain point.)


  reply	other threads:[~2005-10-02  4:41 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20050929200741.GA1156@DervishD>
     [not found] ` <20050930124130.45eb0463.pws@csr.com>
     [not found]   ` <20051001153756.GA12183@DervishD>
2005-10-01 18:38     ` Bart Schaefer
2005-10-01 19:10       ` Peter Stephenson
2005-10-01 20:41         ` DervishD
2005-10-01 22:44         ` Bart Schaefer
2005-10-02  8:06           ` DervishD
2005-10-01 20:28       ` DervishD
2005-10-02  4:40         ` Bart Schaefer [this message]
2005-10-02  8:13           ` DervishD
2005-10-02 19:09           ` Peter Stephenson
2005-10-02 19:55             ` Bart Schaefer
2005-10-02 23:00               ` DervishD
2005-10-03  1:37                 ` Bart Schaefer
2005-10-03  8:57                   ` Peter Stephenson
2005-10-03 14:51                     ` Bart Schaefer
2005-10-03 15:10                       ` Peter Stephenson
2005-10-03 16:50                         ` Bart Schaefer
2005-10-03  9:01                   ` DervishD
2005-10-03 16:21                     ` Bart Schaefer
2005-10-03 17:59                       ` DervishD
2005-10-04 16:31                         ` Bart Schaefer
2005-10-04 17:29                           ` DervishD
2005-10-04 17:34                             ` Peter Stephenson
2005-10-04 17:46                               ` DervishD
2005-10-01  8:01 DervishD
  -- strict thread matches above, loose matches on Subject: below --
2005-10-01  7:45 DervishD

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1051002044052.ZM28373@candle.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=pws@csr.com \
    --cc=zsh-workers@sunsite.dk \
    --cc=zsh@dervishd.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).