From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#68075: 30.0.50; New special form `handler-bind` Date: Thu, 28 Dec 2023 13:14:25 -0500 Message-ID: References: <835y0i92kb.fsf@gnu.org> Reply-To: Stefan Monnier Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="2298"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 68075@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Dec 28 19:15:28 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rIuul-0000Ol-RM for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 28 Dec 2023 19:15:28 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rIuuQ-0003QX-RL; Thu, 28 Dec 2023 13:15:07 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rIuuN-0003QC-7n for bug-gnu-emacs@gnu.org; Thu, 28 Dec 2023 13:15:03 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rIuuM-0004To-U5 for bug-gnu-emacs@gnu.org; Thu, 28 Dec 2023 13:15:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rIuuM-0007d4-A5 for bug-gnu-emacs@gnu.org; Thu, 28 Dec 2023 13:15:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 28 Dec 2023 18:15:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 68075 X-GNU-PR-Package: emacs Original-Received: via spool by 68075-submit@debbugs.gnu.org id=B68075.170378727629275 (code B ref 68075); Thu, 28 Dec 2023 18:15:02 +0000 Original-Received: (at 68075) by debbugs.gnu.org; 28 Dec 2023 18:14:36 +0000 Original-Received: from localhost ([127.0.0.1]:40322 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rIutw-0007c6-7Z for submit@debbugs.gnu.org; Thu, 28 Dec 2023 13:14:36 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:7660) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rIutt-0007bs-39 for 68075@debbugs.gnu.org; Thu, 28 Dec 2023 13:14:35 -0500 Original-Received: from pmg2.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 408F683174; Thu, 28 Dec 2023 13:14:28 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1703787266; bh=BCYphzUZjREccasqAgkFowfbN9R4gebyTve99RyIMwU=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=AqxCh9Ipe2uHOf6BKxmDr59N3BYsIlTNlUBh8OKd3MbMngbMGpewKC3H/8KkG0WAR TfnIPxF7C/aChzksRayZfx8Kd2T87dL4/kFp88cpYqo6mIdPFfooKbIyXK2oHeAqTY yOEYB6/37wq8KsPmhRHUi293fXK8KLjMIr8UlD4T1yLj65hzLsluCqcqyyjHLq6DVY 6VwbXahpEUIOD3F/N9fkzY9XGJ10rh+6ng0al4pYwhq3sT1xgQkvg8IwJZyKaXauT4 mgksggv7oua6n3AqQtC/YSpYQIiJ3uq+fMeiVFuz+GSaMjzbj6K+TDwMMQiqzDwclX FasCCJ0dlxELQ== Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id BBB6A831F3; Thu, 28 Dec 2023 13:14:26 -0500 (EST) Original-Received: from pastel (65-110-221-238.cpe.pppoe.ca [65.110.221.238]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 92F1D120FE8; Thu, 28 Dec 2023 13:14:26 -0500 (EST) In-Reply-To: (Stefan Monnier's message of "Thu, 28 Dec 2023 13:12:12 -0500") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:276989 Archived-At: > See patch below for my attempt to clarify. Sigh! Here it is, Stefan diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi index 4107963eed5..e63660206d0 100644 --- a/doc/lispref/control.texi +++ b/doc/lispref/control.texi @@ -2311,24 +2311,100 @@ Handling Errors @code{(@var{conditions} @var{handler})} where @var{conditions} is an error condition name to be handled, or a list of condition names, and @var{handler} should be a form whose evaluation should return a function. +As with @code{condition-case}, condition names are symbols. Before running @var{body}, @code{handler-bind} evaluates all the @var{handler} forms and installs those handlers to be active during -the evaluation of @var{body}. These handlers are searched together -with those installed by @code{condition-case}. When the innermost +the evaluation of @var{body}. When an error is signaled, +Emacs searches all the active @code{condition-case} and +@code{handler-bind} forms for a handler that +specifies one or more of these condition names. When the innermost matching handler is one installed by @code{handler-bind}, the @var{handler} function is called with a single argument holding the error description. -@var{handler} is called in the dynamic context where the error -happened, without first unwinding the stack, meaning that all the -dynamic bindings are still in effect, except that all the error -handlers between the code that signaled the error and the -@code{handler-bind} are temporarily suspended. Like any normal -function, @var{handler} can exit non-locally, typically via -@code{throw}, or it can return normally. If @var{handler} returns -normally, it means the handler @emph{declined} to handle the error and -the search for an error handler is continued where it left off. +Contrary to what happens with @code{condition-case}, @var{handler} is +called in the dynamic context where the error happened, without +unbinding any variable bindings or running any cleanups of +@code{unwind-protect}, so that all those dynamic bindings are still in +effect. There is one exception: while running the @var{handler} +function, all the error handlers between the code that signaled the +error and the @code{handler-bind} are temporarily suspended, meaning +that when an error is signaled, Emacs will only search the active +@code{condition-case} and @code{handler-bind} forms that are inside +the @var{handler} function or outside of the current +@code{handler-bind}. + +Like any normal function, @var{handler} can exit non-locally, +typically via @code{throw}, or it can return normally. +If @var{handler} returns normally, it means the handler +@emph{declined} to handle the error and the search for an error +handler is continued where it left off. + +For example, if we wanted to keep a log of all the errors that occur +during the execution of a particular piece of code together with the +buffer that's current when the error is signaled, but without +otherwise affecting the behavior of that code, we can do it with: + +@example +@group +(handler-bind + ((error + (lambda (err) + (push (cons err (current-buffer)) my-log-of-errors)))) + @var{body-forms}@dots{}) +@end group +@end example + +This will log only those errors that are not caught internally to +@var{body-forms}@dots{}, in other words errors that ``escape'' from +@var{body-forms}@dots{}, and it will not prevent those errors from +being passed on to surrounding @code{condition-case} handlers (or +@code{handler-bind} handlers for that matter) since the above handler +returns normally. + +We can also use @code{handler-bind} to replace an error with another, +as in the code below which turns all errors of type @code{user-error} +that occur during the execution of @var{body-forms}@dots{} into plain +@code{error}: + +@example +@group +(handler-bind + ((user-error + (lambda (err) + (signal 'error (cdr err))))) + @var{body-forms}@dots{}) +@end group +@end example + +We can get almost the same result with @code{condition-case}: + +@example +@group +(condition-case err + (progn @var{body-forms}@dots{}) + (user-error (signal 'error (cdr err)))) +@end group +@end example + +But with the difference that when we (re)signal the new error in +@code{handler-bind} the dynamic environment from the original error is +still active, which means for example that if we were to enter the +debugger at this point, it will show us a complete backtrace including +the point where we signaled the original error: + +@example +@group +Debugger entered--Lisp error: (error "Oops") + signal(error ("Oops")) + (closure (t) (err) (signal 'error (cdr err)))((user-error "Oops")) + user-error("Oops") + @dots{} + eval((handler-bind ((user-error (lambda (err) @dots{} +@end group +@end example + @end defmac @node Error Symbols diff --git a/doc/lispref/eval.texi b/doc/lispref/eval.texi index 45c892ad5a7..c3d980e1717 100644 --- a/doc/lispref/eval.texi +++ b/doc/lispref/eval.texi @@ -848,15 +848,17 @@ Eval @end defopt @defopt lisp-eval-depth-reserve -In order to be able to debug infinite recursion errors, Entry to the -Lisp debugger increases temporarily the value of +In order to be able to debug infinite recursion errors, when invoking the +Lisp debugger, Emacs increases temporarily the value of @code{max-lisp-eval-depth}, if there is little room left, to make sure the debugger itself has room to execute. The same happens when -running the handler of a @code{handler-bind}. +running the handler of a @code{handler-bind}. @xref{Handling Errors}. The variable @code{lisp-eval-depth-reserve} bounds the extra depth that Emacs can add to @code{max-lisp-eval-depth} for those exceptional circumstances. + +The default value of this variable is 200. @end defopt diff --git a/etc/NEWS b/etc/NEWS index 014c32b4d8e..e446f05190a 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1371,6 +1371,7 @@ It puts a limit to the amount by which Emacs can temporarily increase Provides a functionality similar to `condition-case` except it runs the handler code without unwinding the stack, such that we can record the backtrace and other dynamic state at the point of the error. +See the Info node "(elisp) Handling Errors". +++ ** New 'pop-up-frames' action alist entry for 'display-buffer'. diff --git a/lisp/subr.el b/lisp/subr.el index 600b4d27f18..57e2473c2bb 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -7501,13 +7501,13 @@ handler-bind "Setup error HANDLERS around execution of BODY. HANDLERS is a list of (CONDITIONS HANDLER) where CONDITIONS should be a list of condition names (symbols) or -a single condition name and HANDLER is a form whose evaluation +a single condition name, and HANDLER is a form whose evaluation returns a function. When an error is signaled during execution of BODY, if that error matches CONDITIONS, then the associated HANDLER -function is called with the error as argument. +function is called with the error object as argument. HANDLERs can either transfer the control via a non-local exit, -or return normally. If they return normally the search for an +or return normally. If a handler returns normally, the search for an error handler continues from where it left off." ;; FIXME: Completion support as in `condition-case'? (declare (indent 1) (debug ((&rest (sexp form)) body)))